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 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: "active", host: "github.com", repository: "acme/web", number: 7, source: "manual" },
50 {
51 id: "archived",
52 host: "github.com",
53 repository: "acme/web",
54 number: 7,
55 source: "created",
56 },
57 { id: "deleted", host: "github.com", repository: "acme/web", number: 7, source: "manual" },
58 {
59 id: "side-chat",
60 host: "github.com",
61 repository: "acme/web",
62 number: 7,
63 source: "manual",
64 },
65 {
66 id: "dismissed",
67 host: "github.com",
68 repository: "acme/web",
69 number: 7,
70 source: "stack-dismissed",
71 },
72 {
73 id: "other-host",
74 host: "github.example.com",
75 repository: "acme/web",
76 number: 7,
77 source: "manual",
78 },
79 {
80 id: "other-repository",
81 host: "github.com",
82 repository: "acme/api",
83 number: 7,
84 source: "manual",
85 },
86 {
87 id: "other-number",
88 host: "github.com",
89 repository: "acme/web",
90 number: 8,
91 source: "manual",
92 },
93 ];
94 for (const fixture of fixtures) {
95 const payload = yield* encodePayload({
96 linkedPullRequest: null,
97 ...(fixture.id === "side-chat" ? { lineage: { sideChat: true } } : {}),
98 pullRequests: [{ ...fixture, linkedAt: createdAt, snapshot: null, stack: null }],
99 });
100 yield* sql`
101 INSERT INTO orchestration_v2_projection_threads (
102 thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
103 created_at, updated_at, archived_at, deleted_at, payload_json
104 ) VALUES (
105 ${fixture.id}, 'project-1', ${fixture.id}, 'codex', 'full-access', 'default',
106 ${createdAt}, ${fixture.id === "archived" ? archivedAt : createdAt},
107 ${fixture.id === "archived" ? archivedAt : null},
108 ${fixture.id === "deleted" ? archivedAt : null}, ${payload}
109 )
110 `;
111 }
112 yield* sql`
113 INSERT INTO orchestration_v2_projection_threads (
114 thread_id, project_id, title, default_provider, runtime_mode, interaction_mode,
115 created_at, updated_at, payload_json
116 ) VALUES (
117 'explicitly-unlinked', 'project-1', 'explicitly-unlinked', 'codex', 'full-access', 'default',
118 ${createdAt}, ${createdAt},
119 '{"linkedPullRequest":{"projectId":"project-1","repository":"acme/web","number":7,"url":"https://github.com/acme/web/pull/7"},"pullRequests":[]}'
120 )
121 `;
122 yield* sql`
123 INSERT INTO projection_projects (
124 project_id, title, workspace_root, scripts_json, created_at, updated_at
125 ) VALUES ('project-1', 'Project', '/tmp/project', '[]', ${createdAt}, ${createdAt})
126 `;
127 yield* sql`
128 INSERT INTO projection_threads (
129 thread_id, project_id, title, model_selection_json, created_at, updated_at
130 ) VALUES ('v1-only', 'project-1', 'v1-only',
131 '{"instanceId":"codex","model":"gpt-5.4"}', ${createdAt}, ${createdAt})
132 `;
133 yield* sql`
134 INSERT INTO projection_thread_pull_requests (
135 thread_id, host, repository, number, url, source, linked_at
136 ) VALUES ('v1-only', 'github.com', 'acme/web', 7,
137 'https://github.com/acme/web/pull/7', 'manual', ${createdAt})
138 `;
139
140 expect(
141 (yield* listLinkedPullRequestThreads({
142 host: "forge.example:3000",
143 repository: "acme/web",
144 number: 7,
145 })).threads.map((thread) => thread.id),
146 ).toEqual(["forgejo-old"]);
147 expect(
148 (yield* listLinkedPullRequestThreads({
149 host: "forge.example:4000",
150 repository: "acme/web",
151 number: 7,
152 })).threads.map((thread) => thread.id),
153 ).toEqual(["forgejo-other-port"]);
154 expect(
155 (yield* listLinkedPullRequestThreads({
156 host: "org.visualstudio.com",
157 repository: "project/_git/web",
158 number: 7,
159 })).threads.map((thread) => thread.id),
160 ).toEqual(["azure"]);
161 const result = yield* listLinkedPullRequestThreads({
162 host: "GitHub.Com",
163 repository: "ACME/WEB",
164 number: 7,
165 });
166 expect(result).toEqual({
167 threads: [
168 { id: "archived", projectId: "project-1", title: "archived", archivedAt },
169 { id: "active", projectId: "project-1", title: "active", archivedAt: null },
170 ],
171 });
172 assert.deepStrictEqual(
173 yield* listLinkedPullRequestThreads({
174 host: "github.com",
175 repository: "acme/web",
176 number: 99,
177 }),
178 { threads: [] },
179 );
180 }).pipe(Effect.provide(SqlitePersistence.layerMemory)),
181);