|
| 1 | +import type { SessionService } from "@posthog/core/sessions/sessionService"; |
| 2 | +import type { RootLogger } from "@posthog/di/logger"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import type { TaskCreationEffects } from "./taskCreationEffects"; |
| 5 | +import type { ITaskCreationHost } from "./taskCreationHost"; |
| 6 | +import { buildWorktreeAdoptionInput } from "./taskInput"; |
| 7 | +import { TaskService } from "./taskService"; |
| 8 | + |
| 9 | +const scopedLog = { |
| 10 | + debug: vi.fn(), |
| 11 | + info: vi.fn(), |
| 12 | + warn: vi.fn(), |
| 13 | + error: vi.fn(), |
| 14 | +}; |
| 15 | +const rootLogger = { |
| 16 | + ...scopedLog, |
| 17 | + scope: () => scopedLog, |
| 18 | +} as unknown as RootLogger; |
| 19 | + |
| 20 | +function makeService(): TaskService { |
| 21 | + const host = { |
| 22 | + // The API client's createTask rejects so tests can observe that an input |
| 23 | + // made it past validation (failedStep lands on task_creation, not |
| 24 | + // validation) without faking the whole saga. |
| 25 | + getAuthenticatedClient: vi.fn(async () => ({ |
| 26 | + createTask: vi.fn().mockRejectedValue(new Error("api down")), |
| 27 | + deleteTask: vi.fn(), |
| 28 | + getTask: vi.fn(), |
| 29 | + createTaskRun: vi.fn(), |
| 30 | + startTaskRun: vi.fn(), |
| 31 | + sendRunCommand: vi.fn(), |
| 32 | + updateTask: vi.fn(), |
| 33 | + })), |
| 34 | + detectRepo: vi.fn(async () => null), |
| 35 | + getFolders: vi.fn(async () => []), |
| 36 | + addFolder: vi.fn(async () => ({ id: "folder-1", path: "/repo" })), |
| 37 | + track: vi.fn(), |
| 38 | + } as unknown as ITaskCreationHost; |
| 39 | + const sessionService = { |
| 40 | + markTaskCreationInFlight: vi.fn(), |
| 41 | + connectToTask: vi.fn(), |
| 42 | + disconnectFromTask: vi.fn(), |
| 43 | + } as unknown as SessionService; |
| 44 | + const effects = { |
| 45 | + onWorkspaceCreated: vi.fn(), |
| 46 | + onCreateSuccess: vi.fn(), |
| 47 | + } as unknown as TaskCreationEffects; |
| 48 | + return new TaskService(host, sessionService, effects, rootLogger); |
| 49 | +} |
| 50 | + |
| 51 | +describe("TaskService.createTask validation", () => { |
| 52 | + it("rejects an input with neither content nor a taskDescription", async () => { |
| 53 | + const result = await makeService().createTask({ |
| 54 | + content: " ", |
| 55 | + repoPath: "/repo", |
| 56 | + workspaceMode: "worktree", |
| 57 | + }); |
| 58 | + |
| 59 | + expect(result.success).toBe(false); |
| 60 | + if (result.success) throw new Error("expected validation failure"); |
| 61 | + expect(result.failedStep).toBe("validation"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("accepts a promptless worktree-adoption input", async () => { |
| 65 | + const result = await makeService().createTask( |
| 66 | + buildWorktreeAdoptionInput({ |
| 67 | + repoPath: "/repo", |
| 68 | + branch: "feature/orphan", |
| 69 | + }), |
| 70 | + ); |
| 71 | + |
| 72 | + // The stubbed API call fails, proving the input got past validation. |
| 73 | + expect(result.success).toBe(false); |
| 74 | + if (result.success) throw new Error("expected task_creation failure"); |
| 75 | + expect(result.failedStep).toBe("task_creation"); |
| 76 | + }); |
| 77 | +}); |
0 commit comments