|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { WorkflowAction } from "../workflow/schemas"; |
| 3 | +import type { PrSnapshot } from "./prSnapshot"; |
| 4 | +import type { HomeWorkstream } from "./schemas"; |
| 5 | +import { |
| 6 | + buildQuickActionPrompt, |
| 7 | + buildSkillPrompt, |
| 8 | + buildWorkstreamContext, |
| 9 | +} from "./workstreamPrompt"; |
| 10 | + |
| 11 | +function makeAction(overrides: Partial<WorkflowAction> = {}): WorkflowAction { |
| 12 | + return { |
| 13 | + id: "a1", |
| 14 | + label: "Fix CI", |
| 15 | + skillId: "fix-ci", |
| 16 | + prompt: "Get the checks green.", |
| 17 | + ...overrides, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +function makePr(overrides: Partial<PrSnapshot> = {}): PrSnapshot { |
| 22 | + return { |
| 23 | + url: "https://github.com/posthog/code/pull/2910", |
| 24 | + number: 2910, |
| 25 | + title: "Add the thing", |
| 26 | + state: "open", |
| 27 | + ciStatus: "failing", |
| 28 | + reviewDecision: null, |
| 29 | + unresolvedThreads: 0, |
| 30 | + mergeable: true, |
| 31 | + isCurrentUserRequestedReviewer: false, |
| 32 | + isCurrentUserAuthor: true, |
| 33 | + author: "peter", |
| 34 | + lastUpdatedAt: 0, |
| 35 | + ...overrides, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function makeWs(overrides: Partial<HomeWorkstream> = {}): HomeWorkstream { |
| 40 | + return { |
| 41 | + id: "ws_1", |
| 42 | + repoName: "code", |
| 43 | + repoFullPath: "PostHog/code", |
| 44 | + branch: "feat/the-thing", |
| 45 | + prUrl: null, |
| 46 | + pr: null, |
| 47 | + tasks: [], |
| 48 | + situations: [], |
| 49 | + primarySituation: null, |
| 50 | + lastActivityAt: 0, |
| 51 | + ...overrides, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +describe("buildSkillPrompt", () => { |
| 56 | + it.each([ |
| 57 | + { |
| 58 | + name: "prefixes the skill command and keeps the body", |
| 59 | + action: makeAction({ skillId: "fix-ci", prompt: "Get it green." }), |
| 60 | + expected: "/fix-ci\n\nGet it green.", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "emits just the command when there is no body", |
| 64 | + action: makeAction({ skillId: "fix-ci", prompt: " " }), |
| 65 | + expected: "/fix-ci", |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "sends the body alone when no skill is bound", |
| 69 | + action: makeAction({ skillId: "", prompt: "Do the work." }), |
| 70 | + expected: "Do the work.", |
| 71 | + }, |
| 72 | + ])("$name", ({ action, expected }) => { |
| 73 | + expect(buildSkillPrompt(action)).toBe(expected); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +interface PromptCase { |
| 78 | + name: string; |
| 79 | + workstream: HomeWorkstream; |
| 80 | + contains: string[]; |
| 81 | + notContains: string[]; |
| 82 | +} |
| 83 | + |
| 84 | +const contextCases: PromptCase[] = [ |
| 85 | + { |
| 86 | + name: "includes repo, branch, and PR number/url/CI when a PR is present", |
| 87 | + workstream: makeWs({ pr: makePr() }), |
| 88 | + contains: [ |
| 89 | + "- Repository: PostHog/code", |
| 90 | + "- Branch: feat/the-thing", |
| 91 | + "- Pull request #2910: Add the thing", |
| 92 | + "https://github.com/posthog/code/pull/2910", |
| 93 | + "CI: failing", |
| 94 | + ], |
| 95 | + notContains: [], |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "includes review decision and unresolved threads when set", |
| 99 | + workstream: makeWs({ |
| 100 | + pr: makePr({ reviewDecision: "changes_requested", unresolvedThreads: 3 }), |
| 101 | + }), |
| 102 | + contains: ["Review: changes_requested", "Unresolved review threads: 3"], |
| 103 | + notContains: [], |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "omits review decision and unresolved threads when unset", |
| 107 | + workstream: makeWs({ pr: makePr() }), |
| 108 | + contains: [], |
| 109 | + notContains: ["Review:", "Unresolved review threads"], |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "falls back to the bare PR url when there is no PR snapshot", |
| 113 | + workstream: makeWs({ |
| 114 | + pr: null, |
| 115 | + prUrl: "https://github.com/posthog/code/pull/42", |
| 116 | + }), |
| 117 | + contains: ["- Pull request: https://github.com/posthog/code/pull/42"], |
| 118 | + notContains: [], |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "emits a branch-only block when there is no PR at all", |
| 122 | + workstream: makeWs({ pr: null, prUrl: null, branch: "wip" }), |
| 123 | + contains: ["- Branch: wip"], |
| 124 | + notContains: ["Pull request"], |
| 125 | + }, |
| 126 | +]; |
| 127 | + |
| 128 | +describe("buildWorkstreamContext", () => { |
| 129 | + it.each(contextCases)("$name", ({ workstream, contains, notContains }) => { |
| 130 | + const context = buildWorkstreamContext(workstream); |
| 131 | + for (const text of contains) expect(context).toContain(text); |
| 132 | + for (const text of notContains) expect(context).not.toContain(text); |
| 133 | + }); |
| 134 | + |
| 135 | + // Exact-match case (asserts emptiness, not substrings), kept separate. |
| 136 | + it("returns an empty string when there is nothing to anchor to", () => { |
| 137 | + expect( |
| 138 | + buildWorkstreamContext( |
| 139 | + makeWs({ repoFullPath: null, branch: null, pr: null, prUrl: null }), |
| 140 | + ), |
| 141 | + ).toBe(""); |
| 142 | + }); |
| 143 | +}); |
| 144 | + |
| 145 | +const SKILL_PREFIX = "/fix-ci\n\nGet the checks green."; |
| 146 | + |
| 147 | +const quickActionCases: PromptCase[] = [ |
| 148 | + { |
| 149 | + name: "appends the workstream context after the skill prompt", |
| 150 | + workstream: makeWs({ pr: makePr() }), |
| 151 | + contains: [SKILL_PREFIX, "- Pull request #2910: Add the thing"], |
| 152 | + notContains: [], |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "is just the skill prompt when the workstream has no context", |
| 156 | + workstream: makeWs({ |
| 157 | + repoFullPath: null, |
| 158 | + branch: null, |
| 159 | + pr: null, |
| 160 | + prUrl: null, |
| 161 | + }), |
| 162 | + contains: [SKILL_PREFIX], |
| 163 | + notContains: ["Context for this task"], |
| 164 | + }, |
| 165 | +]; |
| 166 | + |
| 167 | +describe("buildQuickActionPrompt", () => { |
| 168 | + it.each(quickActionCases)( |
| 169 | + "$name", |
| 170 | + ({ workstream, contains, notContains }) => { |
| 171 | + const prompt = buildQuickActionPrompt(makeAction(), workstream); |
| 172 | + expect(prompt.startsWith(SKILL_PREFIX)).toBe(true); |
| 173 | + for (const text of contains) expect(prompt).toContain(text); |
| 174 | + for (const text of notContains) expect(prompt).not.toContain(text); |
| 175 | + }, |
| 176 | + ); |
| 177 | +}); |
0 commit comments