Open History to see the last commit
1import { assert, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Schema from "effect/Schema";
4import * as SqlClient from "effect/sql/SqlClient";
5
6import * as SqlitePersistence from "../persistence/Sqlite.ts";
7import { listLinkedPullRequestThreads } from "./linkedThreads.ts";
8
9const encodePayload = Schema.encodeEffect(Schema.fromJsonString(Schema.Unknown));
10
11it.effect(
12 "finds active, settled, and archived threads for exactly one pull request, excluding deleted, dismissed, and side chat links",
13 () =>
14 Effect.gen(function* () {
15 const sql = yield* SqlClient.SqlClient;
16 const createdAt = "2026-09-01T00:00:00.000Z";
17 const archivedAt = "2026-09-03T00:00:00.000Z";
18 const fixtures = [
19 {
20 id: "forgejo-old",
21 host: "forge.example",
22 repository: "acme/web",
23 number: 7,
24 source: "manual",
25 url: "http://forge.example:3000/acme/web/pulls/7",
26 },
27 {
28 id: "forgejo-other-port",
29 host: "forge.example:4000",
30 repository: "acme/web",
31 number: 7,
32 source: "manual",
33 url: "http://forge.example:4000/acme/web/pulls/7",
34 },
35 {
36 id: "azure",
37 host: "dev.azure.com",
38 repository: "org/project/_git/web",
39 number: 7,
40 source: "manual",
41 },
42 {
43 id: "other-org",
44 host: "dev.azure.com",
45 repository: "other/project/_git/web",
46 number: 7,
47 source: "manual",
48 },
49 { id: "settled", host: "github.com", repository: "acme/web", number: 7, source: "manual" },
50 {
51 id: "auto-settled",
52 host: "github.com",
53 repository: "acme/web",
54 number: 7,
55 source: "manual",
56 },
57 { id: "active", host: "github.com", repository: "acme/web", number: 7, source: "manual" },
58 {
59 id: "archived",
60 host: "github.com",
61 repository: "acme/web",
62 number: 7,
63 source: "created",
64 },
65 { id: "deleted", host: "github.com", repository: "acme/web", number: 7, source: "manual" },
66 {
67 id: "side-chat",
68 host: "github.com",
69 repository: "acme/web",
70 number: 7,
71 source: "manual",
72 },
73 {
74 id: "dismissed",
75 host: "github.com",
76 repository: "acme/web",
77 number: 7,
78 source: "stack-dismissed",
79 },
80 {
81 id: "other-host",
82 host: "github.example.com",
83 repository: "acme/web",
84 number: 7,
85 source: "manual",
86 },
87 {
88 id: "other-repository",
89 host: "github.com",
90 repository: "acme/api",
91 number: 7,
92 source: "manual",
93 },
94 {
95 id: "other-number",
96 host: "github.com",
97 repository: "acme/web",
98 number: 8,
99 source: "manual",
100 },
101 ];
102 for (const fixture of fixtures) {
103 const payload = yield* encodePayload({
104 linkedPullRequest: null,
105 settledOverride: ["settled", "archived"].includes(fixture.id) ? "settled" : null,
106 settledAt: fixture.id === "auto-settled" ? archivedAt : null,
107 ...(fixture.id === "side-chat" ? { lineage: { sideChat: true } } : {}),
108 pullRequests: [{ ...fixture, linkedAt: createdAt, snapshot: null, stack: null }],
109 });
110 yield* sql`
111 INSERT INTO orchestration_v2_projection_threads (
112 thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
113 created_at, updated_at, archived_at, deleted_at, payload_json
114 ) VALUES (
115 ${fixture.id}, 'project-1', ${fixture.id}, 'codex', 'full-access', 'default',
116 ${createdAt}, ${fixture.id === "archived" ? archivedAt : createdAt},
117 ${fixture.id === "archived" ? archivedAt : null},
118 ${fixture.id === "deleted" ? archivedAt : null}, ${payload}
119 )
120 `;
121 }
122 yield* sql`
123 INSERT INTO orchestration_v2_projection_threads (
124 thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
125 created_at, updated_at, payload_json
126 ) VALUES (
127 'explicitly-unlinked', 'project-1', 'explicitly-unlinked', 'codex', 'full-access', 'default',
128 ${createdAt}, ${createdAt},
129 '{"linkedPullRequest":{"projectId":"project-1","repository":"acme/web","number":7,"url":"https://github.com/acme/web/pull/7"},"pullRequests":[]}'
130 )
131 `;
132 yield* sql`
133 INSERT INTO projection_projects (
134 project_id, title, workspace_root, scripts_json, created_at, updated_at
135 ) VALUES ('project-1', 'Project', '/tmp/project', '[]', ${createdAt}, ${createdAt})
136 `;
137 yield* sql`
138 INSERT INTO projection_threads (
139 thread_id, project_id, title, model_selection_json, created_at, updated_at
140 ) VALUES ('v1-only', 'project-1', 'v1-only',
141 '{"instanceId":"codex","model":"gpt-5.4"}', ${createdAt}, ${createdAt})
142 `;
143 yield* sql`
144 INSERT INTO projection_thread_pull_requests (
145 thread_id, host, repository, number, url, source, linked_at
146 ) VALUES ('v1-only', 'github.com', 'acme/web', 7,
147 'https://github.com/acme/web/pull/7', 'manual', ${createdAt})
148 `;
149
150 expect(
151 (yield* listLinkedPullRequestThreads({
152 host: "forge.example:3000",
153 repository: "acme/web",
154 number: 7,
155 })).threads.map((thread) => thread.id),
156 ).toEqual(["forgejo-old"]);
157 expect(
158 (yield* listLinkedPullRequestThreads({
159 host: "forge.example:4000",
160 repository: "acme/web",
161 number: 7,
162 })).threads.map((thread) => thread.id),
163 ).toEqual(["forgejo-other-port"]);
164 expect(
165 (yield* listLinkedPullRequestThreads({
166 host: "org.visualstudio.com",
167 repository: "project/_git/web",
168 number: 7,
169 })).threads.map((thread) => thread.id),
170 ).toEqual(["azure"]);
171 const result = yield* listLinkedPullRequestThreads({
172 host: "GitHub.Com",
173 repository: "ACME/WEB",
174 number: 7,
175 });
176 expect(result).toEqual({
177 threads: [
178 { id: "archived", projectId: "project-1", title: "archived", archivedAt, settled: true },
179 {
180 id: "active",
181 projectId: "project-1",
182 title: "active",
183 archivedAt: null,
184 settled: false,
185 },
186 {
187 id: "auto-settled",
188 projectId: "project-1",
189 title: "auto-settled",
190 archivedAt: null,
191 settled: true,
192 },
193 {
194 id: "settled",
195 projectId: "project-1",
196 title: "settled",
197 archivedAt: null,
198 settled: true,
199 },
200 ],
201 });
202 assert.deepStrictEqual(
203 yield* listLinkedPullRequestThreads({
204 host: "github.com",
205 repository: "acme/web",
206 number: 99,
207 }),
208 { threads: [] },
209 );
210 }).pipe(Effect.provide(SqlitePersistence.layerMemory)),
211);