-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathnewTaskLinkResolver.test.ts
More file actions
175 lines (152 loc) · 6.06 KB
/
Copy pathnewTaskLinkResolver.test.ts
File metadata and controls
175 lines (152 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import type { GithubRef, NewTaskLinkPayload } from "@posthog/shared";
import { ANALYTICS_EVENTS } from "@posthog/shared/analytics-events";
import { describe, expect, it, vi } from "vitest";
import type { GitHubIssueClient } from "./identifiers";
import { NewTaskLinkResolver } from "./newTaskLinkResolver";
function makeIssue(overrides: Partial<GithubRef> = {}): GithubRef {
return {
kind: "issue",
number: 7,
title: "Fix the bug",
state: "OPEN",
labels: [],
url: "https://github.com/acme/web/issues/7",
repo: "acme/web",
...overrides,
};
}
function makeResolver(
getGithubIssue: GitHubIssueClient["getGithubIssue"],
): NewTaskLinkResolver {
return new NewTaskLinkResolver({ getGithubIssue });
}
describe("NewTaskLinkResolver", () => {
it("maps a new-action payload to navigation options", async () => {
const resolver = makeResolver(vi.fn());
const payload: NewTaskLinkPayload = {
action: "new",
prompt: "do a thing",
repo: "acme/web",
model: "sonnet",
mode: "plan",
};
const result = await resolver.resolve(payload);
expect(result.kind).toBe("navigate");
if (result.kind !== "navigate") return;
expect(result.navigation).toEqual({
initialPrompt: "do a thing",
initialCloudRepository: "acme/web",
initialModel: "sonnet",
initialMode: "plan",
});
expect(result.analytics.event).toBe(ANALYTICS_EVENTS.DEEP_LINK_NEW_TASK);
if (result.analytics.event !== ANALYTICS_EVENTS.DEEP_LINK_NEW_TASK) return;
expect(result.analytics.properties.has_prompt).toBe(true);
expect(result.analytics.properties.source).toBeUndefined();
expect(result.analytics.properties.issue_identifier).toBeUndefined();
});
it("carries source and issue identifier into analytics without touching navigation", async () => {
const resolver = makeResolver(vi.fn());
const payload: NewTaskLinkPayload = {
action: "new",
prompt: "do a thing",
repo: "acme/web",
source: "linear",
issueIdentifier: "ENG-123",
};
const result = await resolver.resolve(payload);
expect(result.kind).toBe("navigate");
if (result.kind !== "navigate") return;
expect(result.navigation).toEqual({
initialPrompt: "do a thing",
initialCloudRepository: "acme/web",
initialModel: undefined,
initialMode: undefined,
});
if (result.analytics.event !== ANALYTICS_EVENTS.DEEP_LINK_NEW_TASK) return;
expect(result.analytics.properties.source).toBe("linear");
expect(result.analytics.properties.issue_identifier).toBe("ENG-123");
expect(result.analytics.properties.prompt_length_chars).toBe(10);
});
it("uses the decoded plan as the prompt for a plan-action payload", async () => {
const resolver = makeResolver(vi.fn());
const payload: NewTaskLinkPayload = { action: "plan", plan: "step one" };
const result = await resolver.resolve(payload);
expect(result.kind).toBe("navigate");
if (result.kind !== "navigate") return;
expect(result.navigation.initialPrompt).toBe("step one");
expect(result.analytics.event).toBe(ANALYTICS_EVENTS.DEEP_LINK_PLAN);
if (result.analytics.event !== ANALYTICS_EVENTS.DEEP_LINK_PLAN) return;
expect(result.analytics.properties.plan_length_chars).toBe(8);
});
it("derives prompt, repo and labels for a found issue", async () => {
const getGithubIssue = vi
.fn<GitHubIssueClient["getGithubIssue"]>()
.mockResolvedValue(makeIssue({ labels: ["bug", "p1"] }));
const resolver = makeResolver(getGithubIssue);
const payload: NewTaskLinkPayload = {
action: "issue",
url: "https://github.com/acme/web/issues/7",
owner: "acme",
issueRepo: "web",
issueNumber: 7,
};
const result = await resolver.resolve(payload);
expect(getGithubIssue).toHaveBeenCalledWith("acme", "web", 7);
expect(result.kind).toBe("navigate");
if (result.kind !== "navigate") return;
expect(result.navigation.initialPrompt).toBe(
"GitHub Issue: Fix the bug\nhttps://github.com/acme/web/issues/7\nLabels: bug, p1",
);
expect(result.navigation.initialCloudRepository).toBe("acme/web");
expect(result.analytics.event).toBe(ANALYTICS_EVENTS.DEEP_LINK_ISSUE);
});
it("prefers an explicit repo over the issue owner/repo default", async () => {
const resolver = makeResolver(vi.fn().mockResolvedValue(makeIssue()));
const payload: NewTaskLinkPayload = {
action: "issue",
url: "https://github.com/acme/web/issues/7",
owner: "acme",
issueRepo: "web",
issueNumber: 7,
repo: "acme/override",
};
const result = await resolver.resolve(payload);
if (result.kind !== "navigate") throw new Error("expected navigate");
expect(result.navigation.initialCloudRepository).toBe("acme/override");
});
it("classifies a missing issue as not_found", async () => {
const resolver = makeResolver(vi.fn().mockResolvedValue(null));
const payload: NewTaskLinkPayload = {
action: "issue",
url: "https://github.com/acme/web/issues/7",
owner: "acme",
issueRepo: "web",
issueNumber: 7,
};
const result = await resolver.resolve(payload);
expect(result.kind).toBe("not_found");
if (result.analytics.event !== ANALYTICS_EVENTS.DEEP_LINK_ISSUE_FAILED)
return;
expect(result.analytics.properties.reason).toBe("not_found");
});
it("classifies a thrown fetch as fetch_failed and carries the message", async () => {
const resolver = makeResolver(
vi.fn().mockRejectedValue(new Error("network down")),
);
const payload: NewTaskLinkPayload = {
action: "issue",
url: "https://github.com/acme/web/issues/7",
owner: "acme",
issueRepo: "web",
issueNumber: 7,
};
const result = await resolver.resolve(payload);
expect(result.kind).toBe("fetch_failed");
if (result.kind !== "fetch_failed") return;
expect(result.description).toBe("network down");
if (result.analytics.event !== ANALYTICS_EVENTS.DEEP_LINK_ISSUE_FAILED)
return;
expect(result.analytics.properties.error_message).toBe("network down");
});
});