|
| 1 | +/** |
| 2 | + * Tests for createSourceFromState |
| 3 | + * |
| 4 | + * These tests verify that createSourceFromState correctly uses resolvedRef |
| 5 | + * from state metadata when creating source instances. |
| 6 | + * |
| 7 | + * We mock GitHub and Website sources to capture what config gets passed |
| 8 | + * to the constructors, without needing API credentials. |
| 9 | + * |
| 10 | + * Since all VCS sources (GitHub, GitLab, BitBucket) use the same getRef() logic, |
| 11 | + * we only test GitHub as the representative case. |
| 12 | + */ |
| 13 | + |
| 14 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 15 | +import type { IndexStateSearchOnly, SourceMetadata } from "../core/types.js"; |
| 16 | + |
| 17 | +// Mock only the sources we actually test |
| 18 | +vi.mock("../sources/github.js", () => ({ |
| 19 | + GitHubSource: vi.fn().mockImplementation((config) => ({ |
| 20 | + type: "github" as const, |
| 21 | + config, |
| 22 | + })), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock("../sources/website.js", () => ({ |
| 26 | + WebsiteSource: vi.fn().mockImplementation((config) => ({ |
| 27 | + type: "website" as const, |
| 28 | + config, |
| 29 | + })), |
| 30 | +})); |
| 31 | + |
| 32 | +// Import the function under test and mocked sources |
| 33 | +import { createSourceFromState } from "./multi-index-runner.js"; |
| 34 | +import { GitHubSource } from "../sources/github.js"; |
| 35 | +import { WebsiteSource } from "../sources/website.js"; |
| 36 | + |
| 37 | +// Create mock state with specific source metadata |
| 38 | +const createMockState = (source: SourceMetadata): IndexStateSearchOnly => ({ |
| 39 | + version: 1, |
| 40 | + contextState: { |
| 41 | + version: 1, |
| 42 | + } as any, |
| 43 | + source, |
| 44 | +}); |
| 45 | + |
| 46 | +describe("createSourceFromState", () => { |
| 47 | + beforeEach(() => { |
| 48 | + vi.clearAllMocks(); |
| 49 | + }); |
| 50 | + |
| 51 | + // All VCS sources (GitHub, GitLab, BitBucket) use the same getRef() logic: |
| 52 | + // resolvedRef ?? config.ref |
| 53 | + // We test this once with GitHub as the representative case. |
| 54 | + |
| 55 | + it("uses resolvedRef when present", async () => { |
| 56 | + const state = createMockState({ |
| 57 | + type: "github", |
| 58 | + config: { owner: "test-owner", repo: "test-repo", ref: "main" }, |
| 59 | + resolvedRef: "abc123sha", |
| 60 | + syncedAt: new Date().toISOString(), |
| 61 | + }); |
| 62 | + |
| 63 | + await createSourceFromState(state); |
| 64 | + |
| 65 | + expect(GitHubSource).toHaveBeenCalledWith({ |
| 66 | + owner: "test-owner", |
| 67 | + repo: "test-repo", |
| 68 | + ref: "abc123sha", |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it("falls back to config.ref when resolvedRef is missing", async () => { |
| 73 | + const state = createMockState({ |
| 74 | + type: "github", |
| 75 | + config: { owner: "test-owner", repo: "test-repo", ref: "main" }, |
| 76 | + // No resolvedRef |
| 77 | + syncedAt: new Date().toISOString(), |
| 78 | + }); |
| 79 | + |
| 80 | + await createSourceFromState(state); |
| 81 | + |
| 82 | + expect(GitHubSource).toHaveBeenCalledWith({ |
| 83 | + owner: "test-owner", |
| 84 | + repo: "test-repo", |
| 85 | + ref: "main", |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it("website source works without resolvedRef", async () => { |
| 90 | + const state = createMockState({ |
| 91 | + type: "website", |
| 92 | + config: { url: "https://example.com", maxDepth: 2 }, |
| 93 | + syncedAt: new Date().toISOString(), |
| 94 | + }); |
| 95 | + |
| 96 | + await createSourceFromState(state); |
| 97 | + |
| 98 | + expect(WebsiteSource).toHaveBeenCalledWith({ |
| 99 | + url: "https://example.com", |
| 100 | + maxDepth: 2, |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it("throws error for unknown source type", async () => { |
| 105 | + const state = createMockState({ |
| 106 | + type: "unknown" as any, |
| 107 | + config: {} as any, |
| 108 | + syncedAt: new Date().toISOString(), |
| 109 | + }); |
| 110 | + |
| 111 | + await expect(createSourceFromState(state)).rejects.toThrow( |
| 112 | + "Unknown source type: unknown" |
| 113 | + ); |
| 114 | + }); |
| 115 | +}); |
0 commit comments