|
| 1 | +import { execFile } from "node:child_process"; |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import { mkdir, readFile, rm, writeFile } from "node:fs/promises"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { join } from "node:path"; |
| 6 | +import { promisify } from "node:util"; |
| 7 | +import { vi } from "vitest"; |
| 8 | +import type { PostHogAPIClient } from "../../posthog-api.js"; |
| 9 | +import type { TaskRun, TreeSnapshot } from "../../types.js"; |
| 10 | + |
| 11 | +const execFileAsync = promisify(execFile); |
| 12 | + |
| 13 | +export interface TestRepo { |
| 14 | + path: string; |
| 15 | + cleanup: () => Promise<void>; |
| 16 | + git: (args: string[]) => Promise<string>; |
| 17 | + writeFile: (relativePath: string, content: string) => Promise<void>; |
| 18 | + readFile: (relativePath: string) => Promise<string>; |
| 19 | + deleteFile: (relativePath: string) => Promise<void>; |
| 20 | + exists: (relativePath: string) => boolean; |
| 21 | +} |
| 22 | + |
| 23 | +export async function createTestRepo(prefix = "test-repo"): Promise<TestRepo> { |
| 24 | + const repoPath = join( |
| 25 | + tmpdir(), |
| 26 | + `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2)}`, |
| 27 | + ); |
| 28 | + await mkdir(repoPath, { recursive: true }); |
| 29 | + |
| 30 | + const git = async (args: string[]): Promise<string> => { |
| 31 | + const { stdout } = await execFileAsync("git", args, { cwd: repoPath }); |
| 32 | + return stdout.trim(); |
| 33 | + }; |
| 34 | + |
| 35 | + await git(["init"]); |
| 36 | + await git(["config", "user.email", "test@test.com"]); |
| 37 | + await git(["config", "user.name", "Test"]); |
| 38 | + |
| 39 | + await writeFile(join(repoPath, ".gitignore"), ".posthog/\n"); |
| 40 | + await writeFile(join(repoPath, "README.md"), "# Test Repo"); |
| 41 | + await git(["add", "."]); |
| 42 | + await git(["commit", "-m", "Initial commit"]); |
| 43 | + |
| 44 | + return { |
| 45 | + path: repoPath, |
| 46 | + cleanup: () => rm(repoPath, { recursive: true, force: true }), |
| 47 | + git, |
| 48 | + writeFile: async (relativePath: string, content: string) => { |
| 49 | + const fullPath = join(repoPath, relativePath); |
| 50 | + const dir = join(fullPath, ".."); |
| 51 | + await mkdir(dir, { recursive: true }); |
| 52 | + await writeFile(fullPath, content); |
| 53 | + }, |
| 54 | + readFile: async (relativePath: string) => { |
| 55 | + return readFile(join(repoPath, relativePath), "utf-8"); |
| 56 | + }, |
| 57 | + deleteFile: async (relativePath: string) => { |
| 58 | + await rm(join(repoPath, relativePath), { force: true }); |
| 59 | + }, |
| 60 | + exists: (relativePath: string) => { |
| 61 | + return existsSync(join(repoPath, relativePath)); |
| 62 | + }, |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +export function createMockApiClient( |
| 67 | + overrides: Partial<PostHogAPIClient> = {}, |
| 68 | +): PostHogAPIClient { |
| 69 | + return { |
| 70 | + uploadTaskArtifacts: vi |
| 71 | + .fn() |
| 72 | + .mockResolvedValue([{ storage_path: "gs://bucket/trees/test.tar.gz" }]), |
| 73 | + downloadArtifact: vi.fn(), |
| 74 | + getTaskRun: vi.fn(), |
| 75 | + fetchTaskRunLogs: vi.fn(), |
| 76 | + ...overrides, |
| 77 | + } as unknown as PostHogAPIClient; |
| 78 | +} |
| 79 | + |
| 80 | +export function createTaskRun(overrides: Partial<TaskRun> = {}): TaskRun { |
| 81 | + return { |
| 82 | + id: "run-1", |
| 83 | + task: "task-1", |
| 84 | + team: 1, |
| 85 | + branch: null, |
| 86 | + stage: null, |
| 87 | + environment: "local", |
| 88 | + status: "in_progress", |
| 89 | + log_url: "https://logs.example.com/run-1", |
| 90 | + error_message: null, |
| 91 | + output: null, |
| 92 | + state: {}, |
| 93 | + created_at: new Date().toISOString(), |
| 94 | + updated_at: new Date().toISOString(), |
| 95 | + completed_at: null, |
| 96 | + ...overrides, |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +export function createSnapshot( |
| 101 | + overrides: Partial<TreeSnapshot> = {}, |
| 102 | +): TreeSnapshot { |
| 103 | + return { |
| 104 | + treeHash: "test-tree-hash", |
| 105 | + baseCommit: null, |
| 106 | + archiveUrl: "gs://bucket/trees/test.tar.gz", |
| 107 | + changes: [], |
| 108 | + timestamp: new Date().toISOString(), |
| 109 | + ...overrides, |
| 110 | + }; |
| 111 | +} |
0 commit comments