-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.test.ts
More file actions
801 lines (752 loc) · 25.8 KB
/
Copy pathserver.test.ts
File metadata and controls
801 lines (752 loc) · 25.8 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
import { afterEach, describe, expect, it, vi } from "vitest";
import {
createFakePluginHost,
makeThreadResponse,
type FakePluginHost,
} from "@get-bb/plugin-sdk/testing";
import { z } from "zod";
import plugin, {
commentBodySchema,
commentThreadDetailSchema,
renderedTextSelectorSchema,
} from "./server.js";
afterEach(() => vi.restoreAllMocks());
const threadPageSchema = z.object({
threads: z.array(commentThreadDetailSchema.shape.thread),
nextCursor: z.string().nullable(),
});
async function loadPlugin(): Promise<FakePluginHost> {
const host = createFakePluginHost({ pluginId: "timeline-comments" });
await plugin(host.bb);
return host;
}
function createInput(
body: string,
overrides: { bbThreadId?: string; messageId?: string; exact?: string } = {},
) {
const bbThreadId = overrides.bbThreadId ?? "thr_1";
const exact = overrides.exact ?? "stable";
return {
bbThreadId,
message: {
id: overrides.messageId ?? "msg_1",
threadId: bbThreadId,
role: "assistant" as const,
text: exact,
sourceSeqEnd: 4,
},
selector: {
version: 1 as const,
coordinateSpace: "rendered-text-utf16" as const,
start: 0,
end: exact.length,
exact,
prefix: "",
suffix: "",
rects: [{ x: 10, y: 20, width: 40, height: 18 }],
},
body,
};
}
async function createComment(
host: FakePluginHost,
body: string,
overrides?: Parameters<typeof createInput>[1],
) {
return commentThreadDetailSchema.parse(
await host.harness.callRpc("createThread", createInput(body, overrides)),
);
}
describe("timeline comments backend", () => {
it("enforces foreign keys and creates a scoped root thread before publishing", async () => {
const host = await loadPlugin();
const db = host.bb.storage.database();
expect(db.pragma("foreign_keys", { simple: true })).toBe(1);
const created = await createComment(host, "Make the contract explicit.");
expect(created.thread).toMatchObject({
bbThreadId: "thr_1",
messageId: "msg_1",
messageRole: "assistant",
replyCount: 0,
resolvedAt: null,
selector: { exact: "stable", start: 0, end: 6 },
rootComment: { body: "Make the contract explicit.", version: 1 },
});
expect(created.comments).toHaveLength(1);
expect(host.harness.realtimeSignals).toEqual([
{
channel: "comments-changed",
payload: {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
},
},
]);
await expect(
host.harness.callRpc("createThread", {
...createInput("Wrong scope"),
message: { ...createInput("Wrong scope").message, threadId: "thr_2" },
}),
).rejects.toThrow("does not belong");
});
it("validates required bodies, Unicode length, selectors, and strict inputs", async () => {
const host = await loadPlugin();
expect(() => commentBodySchema.parse(" ")).toThrow(
"Comment body is required",
);
expect(() => commentBodySchema.parse("😀".repeat(10_001))).toThrow(
"10,000 Unicode code points",
);
expect(() =>
renderedTextSelectorSchema.parse({
...createInput("Valid body").selector,
end: 2,
}),
).toThrow("UTF-16");
await expect(
host.harness.callRpc("listCommentThreads", {
bbThreadId: "thr_1",
filter: "open",
unexpected: true,
}),
).rejects.toThrow("rpc input validation failed");
});
it("persists reply, edit, resolve, reopen, reply deletion, and root cascade with optimistic versions", async () => {
const host = await loadPlugin();
const created = await createComment(host, "Root");
const replied = commentThreadDetailSchema.parse(
await host.harness.callRpc("reply", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
body: "Reply",
}),
);
const reply = replied.comments.find(
(comment) => comment.parentId !== null,
)!;
expect(replied.thread.replyCount).toBe(1);
const edited = commentThreadDetailSchema.parse(
await host.harness.callRpc("updateComment", {
bbThreadId: "thr_1",
commentId: reply.id,
expectedVersion: reply.version,
body: "Edited reply",
}),
);
expect(edited.comments.at(-1)?.body).toBe("Edited reply");
await expect(
host.harness.callRpc("updateComment", {
bbThreadId: "thr_1",
commentId: reply.id,
expectedVersion: reply.version,
body: "Stale edit",
}),
).rejects.toThrow("changed");
const resolved = commentThreadDetailSchema.parse(
await host.harness.callRpc("setThreadResolved", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
expectedVersion: edited.thread.version,
resolved: true,
}),
);
expect(resolved.thread.resolvedAt).not.toBeNull();
await expect(
host.harness.callRpc("reply", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
body: "Blocked",
}),
).rejects.toThrow("Resolved");
const reopened = commentThreadDetailSchema.parse(
await host.harness.callRpc("setThreadResolved", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
expectedVersion: resolved.thread.version,
resolved: false,
}),
);
const editedReply = reopened.comments.find(
(comment) => comment.parentId !== null,
)!;
const afterReplyDelete = await host.harness.callRpc("deleteComment", {
bbThreadId: "thr_1",
commentId: editedReply.id,
expectedVersion: editedReply.version,
expectedThreadVersion: reopened.thread.version,
});
expect(afterReplyDelete).toMatchObject({
deletedThreadId: null,
thread: { thread: { replyCount: 0 } },
});
const latest = commentThreadDetailSchema.parse(
await host.harness.callRpc("getCommentThread", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
}),
);
await host.harness.callRpc("deleteComment", {
bbThreadId: "thr_1",
commentId: latest.thread.rootComment.id,
expectedVersion: latest.thread.rootComment.version,
expectedThreadVersion: latest.thread.version,
});
const db = host.bb.storage.database();
expect(
db.prepare("SELECT COUNT(*) AS count FROM comment_threads").get(),
).toEqual({ count: 0 });
expect(db.prepare("SELECT COUNT(*) AS count FROM comments").get()).toEqual({
count: 0,
});
});
it("rejects a stale root deletion after a concurrent reply changes the thread", async () => {
const host = await loadPlugin();
const created = await createComment(host, "Root");
const replied = commentThreadDetailSchema.parse(
await host.harness.callRpc("reply", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
body: "Concurrent reply",
}),
);
await expect(
host.harness.callRpc("deleteComment", {
bbThreadId: "thr_1",
commentId: created.thread.rootComment.id,
expectedVersion: created.thread.rootComment.version,
expectedThreadVersion: created.thread.version,
}),
).rejects.toThrow("Comment thread changed");
const retained = commentThreadDetailSchema.parse(
await host.harness.callRpc("getCommentThread", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
}),
);
expect(retained.comments.map((comment) => comment.body)).toEqual([
"Root",
"Concurrent reply",
]);
await host.harness.callRpc("deleteComment", {
bbThreadId: "thr_1",
commentId: retained.thread.rootComment.id,
expectedVersion: retained.thread.rootComment.version,
expectedThreadVersion: replied.thread.version,
});
await expect(
host.harness.callRpc("getCommentThread", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
}),
).rejects.toThrow("not found");
});
it("keeps root and reply pagination independent and cursor-bound", async () => {
vi.spyOn(Date, "now").mockImplementation(() => 1_000);
const host = await loadPlugin();
for (let index = 0; index < 51; index += 1) {
await createComment(host, `Root ${index}`, {
messageId: `msg_${index}`,
});
}
const firstRoots = threadPageSchema.parse(
await host.harness.callRpc("listCommentThreads", {
bbThreadId: "thr_1",
filter: "open",
}),
);
expect(firstRoots.threads).toHaveLength(50);
expect(firstRoots.nextCursor).not.toBeNull();
const secondRoots = threadPageSchema.parse(
await host.harness.callRpc("listCommentThreads", {
bbThreadId: "thr_1",
filter: "open",
cursor: firstRoots.nextCursor ?? undefined,
}),
);
expect(secondRoots.threads).toHaveLength(1);
await expect(
host.harness.callRpc("listCommentThreads", {
bbThreadId: "thr_other",
filter: "open",
cursor: firstRoots.nextCursor,
}),
).rejects.toThrow("does not match");
const target = firstRoots.threads[0]!;
for (let index = 0; index < 100; index += 1) {
await host.harness.callRpc("reply", {
bbThreadId: "thr_1",
commentThreadId: target.id,
body: `Reply ${index}`,
});
}
const firstComments = commentThreadDetailSchema.parse(
await host.harness.callRpc("getCommentThread", {
bbThreadId: "thr_1",
commentThreadId: target.id,
}),
);
expect(firstComments.comments).toHaveLength(100);
expect(firstComments.comments[0]?.parentId).toBeNull();
expect(
firstComments.comments.slice(1).every(({ parentId }) => parentId !== null),
).toBe(true);
expect(firstComments.nextCursor).not.toBeNull();
const secondComments = commentThreadDetailSchema.parse(
await host.harness.callRpc("getCommentThread", {
bbThreadId: "thr_1",
commentThreadId: target.id,
cursor: firstComments.nextCursor ?? undefined,
}),
);
expect(secondComments.comments).toHaveLength(1);
expect(secondComments.comments[0]?.parentId).not.toBeNull();
const allComments = [
...firstComments.comments,
...secondComments.comments,
];
expect(allComments).toHaveLength(101);
expect(new Set(allComments.map(({ id }) => id)).size).toBe(101);
expect(
allComments.filter(({ parentId }) => parentId === null),
).toHaveLength(1);
const firstCliPage = await host.harness.runCli(
["get", target.id, "--limit", "1", "--json"],
{ threadId: "thr_1" },
);
const firstCliComment = JSON.parse(firstCliPage.stdout ?? "").comments[0];
expect(firstCliComment.parentId).toBeNull();
const firstHumanCliPage = await host.harness.runCli(
["get", target.id, "--limit", "1"],
{ threadId: "thr_1" },
);
expect(firstHumanCliPage.stdout).toContain(
`Comment: ${target.rootComment.body}`,
);
const secondCliPage = await host.harness.runCli(
[
"get",
target.id,
"--limit",
"1",
"--cursor",
JSON.parse(firstCliPage.stdout ?? "").nextCursor,
"--json",
],
{ threadId: "thr_1" },
);
const secondCliResult = JSON.parse(secondCliPage.stdout ?? "");
expect(secondCliResult.comments[0].parentId).not.toBeNull();
const secondHumanCliPage = await host.harness.runCli(
[
"get",
target.id,
"--limit",
"1",
"--cursor",
JSON.parse(firstCliPage.stdout ?? "").nextCursor,
],
{ threadId: "thr_1" },
);
expect(secondHumanCliPage.stdout).toContain(
`Reply: ${secondCliResult.comments[0].body}`,
);
const context = (
await host.harness.registrations.mentionProviders[0]!.resolve("thr_1")
).context;
expect(context.indexOf(`Comment: ${target.rootComment.body}`)).toBeLessThan(
context.indexOf("Reply: "),
);
});
it("serializes every currently open comment for mention resolution and omits resolved threads", async () => {
const host = await loadPlugin();
const first = await createComment(host, "First root", {
exact: "first source",
messageId: "msg_first",
});
const second = await createComment(host, "Second root", {
exact: "second source",
messageId: "msg_second",
});
await host.harness.callRpc("reply", {
bbThreadId: "thr_1",
commentThreadId: first.thread.id,
body: "First reply",
});
const summary = await host.harness.callRpc("getThreadHandoffSummary", {
bbThreadId: "thr_1",
});
expect(summary).toMatchObject({ threadCount: 2, commentCount: 3 });
const provider = host.harness.registrations.mentionProviders[0]!;
const db = host.bb.storage.database();
const prepare = vi.spyOn(db, "prepare");
prepare.mockClear();
await host.harness.callRpc("getThreadHandoffSummary", {
bbThreadId: "thr_1",
});
expect(
prepare.mock.calls.filter(([sql]) =>
String(sql).includes("JOIN comments c ON c.thread_id = t.id"),
),
).toHaveLength(1);
prepare.mockClear();
provider.resolve("thr_1");
expect(
prepare.mock.calls.filter(([sql]) =>
String(sql).includes("JOIN comments c ON c.thread_id = t.id"),
),
).toHaveLength(1);
prepare.mockRestore();
expect(
provider.search({
trigger: "@",
query: "comments",
projectId: "proj_1",
threadId: "thr_1",
}),
).toMatchObject([{ title: "3 comments from 2 open threads" }]);
const resolved = await provider.resolve("thr_1");
expect(resolved.context).toContain("first source");
expect(resolved.context).toContain("First reply");
expect(resolved.context).toContain("second source");
await host.harness.callRpc("setThreadResolved", {
bbThreadId: "thr_1",
commentThreadId: second.thread.id,
expectedVersion: second.thread.version,
resolved: true,
});
expect(
provider.search({
trigger: "@",
query: "comments",
projectId: "proj_1",
threadId: "thr_1",
}),
).toMatchObject([{ title: "2 comments from 1 open threads" }]);
const refreshed = await provider.resolve("thr_1");
expect(refreshed.context).not.toContain("second source");
});
it("removes plugin-owned comments when their BB thread is deleted", async () => {
const host = await loadPlugin();
await createComment(host, "Delete with the thread");
await createComment(host, "Keep another thread", {
bbThreadId: "thr_2",
});
await host.harness.emitThreadEvent("thread.deleted", {
thread: makeThreadResponse({ id: "thr_1" }),
});
const db = host.bb.storage.database();
expect(
db
.prepare(
"SELECT bb_thread_id AS bbThreadId, COUNT(*) AS count FROM comment_threads GROUP BY bb_thread_id",
)
.all(),
).toEqual([{ bbThreadId: "thr_2", count: 1 }]);
expect(db.prepare("SELECT COUNT(*) AS count FROM comments").get()).toEqual({
count: 1,
});
});
it("rejects bulk handoff before insertion and at mention resolution above 64k code points", async () => {
const host = await loadPlugin();
const body = "x".repeat(10_000);
const created = await createComment(host, body);
for (let index = 0; index < 6; index += 1) {
await host.harness.callRpc("reply", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
body,
});
}
host.bb.storage
.database()
.prepare(
`INSERT INTO comments (
id, thread_id, parent_id, body, version, created_at, updated_at
) VALUES (?, ?, ?, ?, 1, ?, ?)`,
)
.run(
"invalid_after_limit",
created.thread.id,
created.thread.rootComment.id,
Buffer.from([1]),
9_999_999_999_999,
9_999_999_999_999,
);
await expect(
host.harness.callRpc("getThreadHandoffSummary", { bbThreadId: "thr_1" }),
).rejects.toThrow("64,000-code-point handoff limit");
expect(() =>
host.harness.registrations.mentionProviders[0]!.resolve("thr_1"),
).toThrow("64,000-code-point handoff limit");
});
it("exposes bounded read-only CLI discovery for agents", async () => {
const host = await loadPlugin();
const created = await createComment(host, "Read me");
await createComment(host, "Read me next", { messageId: "msg_2" });
const listed = await host.harness.runCli(
["list", "--limit", "1", "--json"],
{
threadId: "thr_1",
},
);
expect(listed.exitCode).toBe(0);
const firstListPage = JSON.parse(listed.stdout ?? "");
expect(firstListPage.threads).toHaveLength(1);
expect(firstListPage.nextCursor).toEqual(expect.any(String));
const nextListed = await host.harness.runCli(
["list", "--limit", "1", "--cursor", firstListPage.nextCursor, "--json"],
{ threadId: "thr_1" },
);
expect(JSON.parse(nextListed.stdout ?? "").threads).toHaveLength(1);
await host.harness.callRpc("reply", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
body: "Reply",
});
const fetched = await host.harness.runCli(
["get", created.thread.id, "--limit", "1", "--json"],
{ threadId: "thr_1" },
);
expect(fetched.exitCode).toBe(0);
const firstCommentPage = JSON.parse(fetched.stdout ?? "");
expect(firstCommentPage.comments).toHaveLength(1);
expect(firstCommentPage.nextCursor).toEqual(expect.any(String));
const nextFetched = await host.harness.runCli(
[
"get",
created.thread.id,
"--limit",
"1",
"--cursor",
firstCommentPage.nextCursor,
"--json",
],
{ threadId: "thr_1" },
);
const secondCommentPage = JSON.parse(nextFetched.stdout ?? "");
expect(
[
firstCommentPage.comments[0].body,
secondCommentPage.comments[0].body,
].sort(),
).toEqual(["Read me", "Reply"]);
const humanList = await host.harness.runCli(["list", "--limit", "1"], {
threadId: "thr_1",
});
expect(humanList.stdout).toContain(
"More results available. Continue with --cursor ",
);
const invalidLimit = await host.harness.runCli(
["get", created.thread.id, "--limit", "101"],
{ threadId: "thr_1" },
);
expect(invalidLimit).toMatchObject({ exitCode: 1 });
expect(invalidLimit.stderr).toContain("integer from 1 to 100");
});
it("lets agents reply, resolve idempotently, and reopen through the CLI", async () => {
const host = await loadPlugin();
const created = await createComment(host, "Address this feedback");
const replied = await host.harness.runCli(
[
"reply",
created.thread.id,
"--body",
"Fixed in server.ts; focused tests pass.",
"--json",
],
{ threadId: "thr_1" },
);
expect(replied.exitCode).toBe(0);
expect(JSON.parse(replied.stdout ?? "")).toMatchObject({
id: created.thread.id,
state: "open",
replyCount: 1,
});
const resolved = await host.harness.runCli(
["resolve", created.thread.id, "--json"],
{ threadId: "thr_1" },
);
expect(resolved.exitCode).toBe(0);
const resolvedResult = JSON.parse(resolved.stdout ?? "");
expect(resolvedResult).toMatchObject({
id: created.thread.id,
state: "resolved",
replyCount: 1,
});
const resolvedAgain = await host.harness.runCli(
["resolve", created.thread.id, "--json"],
{ threadId: "thr_1" },
);
expect(JSON.parse(resolvedAgain.stdout ?? "").version).toBe(
resolvedResult.version,
);
const rejectedReply = await host.harness.runCli(
["reply", created.thread.id, "--body", "Too late"],
{ threadId: "thr_1" },
);
expect(rejectedReply).toMatchObject({ exitCode: 1 });
expect(rejectedReply.stderr).toContain("cannot receive replies");
const reopened = await host.harness.runCli(
["reopen", created.thread.id],
{ threadId: "thr_1" },
);
expect(reopened).toMatchObject({ exitCode: 0 });
expect(reopened.stdout).toContain("is open");
const detail = commentThreadDetailSchema.parse(
await host.harness.callRpc("getCommentThread", {
bbThreadId: "thr_1",
commentThreadId: created.thread.id,
}),
);
expect(detail.thread.resolvedAt).toBeNull();
expect(detail.comments.at(-1)?.body).toBe(
"Fixed in server.ts; focused tests pass.",
);
});
it("rejects unknown CLI commands, options, arguments, and missing option values", async () => {
const host = await loadPlugin();
const cases: Array<{ argv: string[]; message: string }> = [
{ argv: [], message: "A command is required" },
{ argv: ["ls"], message: "Unknown comments command" },
{ argv: ["list", "extra"], message: "Unexpected argument for list" },
{ argv: ["list", "--wat"], message: "Unknown option for list" },
{ argv: ["get", "--json"], message: "comment-thread-id" },
{
argv: ["reply", "comment_1"],
message: "reply requires --body <text>",
},
{
argv: ["resolve", "comment_1", "--cursor", "next"],
message: "Unknown option for resolve",
},
{
argv: ["get", "comment_1", "--state", "open"],
message: "Unknown option for get",
},
{ argv: ["list", "--thread"], message: "--thread requires a value" },
{
argv: ["list", "--thread", "--json"],
message: "--thread requires a value",
},
{ argv: ["list", "--cursor"], message: "--cursor requires a value" },
{
argv: ["list", "--cursor", "--json"],
message: "--cursor requires a value",
},
{ argv: ["list", "--limit"], message: "--limit requires a value" },
{
argv: ["list", "--limit", "--json"],
message: "--limit requires a value",
},
{ argv: ["list", "--state"], message: "--state requires a value" },
{
argv: ["list", "--state", "--json"],
message: "--state requires a value",
},
];
for (const { argv, message } of cases) {
const result = await host.harness.runCli(argv, { threadId: "thr_1" });
expect(result.exitCode, argv.join(" ")).toBe(1);
expect(result.stderr, argv.join(" ")).toContain(message);
}
});
it("accepts an escaped generated comment-thread ID that begins with dashes", async () => {
const host = await loadPlugin();
const created = await createComment(host, "Escaped ID");
const escapedId = "--generated-comment-thread";
const db = host.bb.storage.database();
db.transaction(() => {
db.prepare(
`INSERT INTO comment_threads (
id, bb_thread_id, message_id, message_role,
selector_version, selector_start, selector_end, selector_exact,
selector_prefix, selector_suffix, version, created_at, updated_at,
resolved_at
)
SELECT
?, bb_thread_id, message_id, message_role,
selector_version, selector_start, selector_end, selector_exact,
selector_prefix, selector_suffix, version, created_at, updated_at,
resolved_at
FROM comment_threads
WHERE id = ?`,
).run(escapedId, created.thread.id);
db.prepare("UPDATE comments SET thread_id = ? WHERE thread_id = ?").run(
escapedId,
created.thread.id,
);
db.prepare("DELETE FROM comment_threads WHERE id = ?").run(
created.thread.id,
);
})();
const fetched = await host.harness.runCli(
["get", "--", escapedId, "--json"],
{ threadId: "thr_1" },
);
expect(fetched.exitCode).toBe(0);
expect(JSON.parse(fetched.stdout ?? "")).toMatchObject({
thread: { id: escapedId },
comments: [{ body: "Escaped ID" }],
});
const unescapedFlag = await host.harness.runCli(["get", "--json"], {
threadId: "thr_1",
});
expect(unescapedFlag.exitCode).toBe(1);
expect(unescapedFlag.stderr).toContain("comment-thread-id");
});
it("keeps Unicode-heavy CLI pages below the host limit and fails clearly for one oversized record", async () => {
const host = await loadPlugin();
const body = "😀".repeat(10_000);
for (let index = 0; index < 30; index += 1) {
await createComment(host, body, { messageId: `msg_${index}` });
}
const listed = await host.harness.runCli(["list", "--json"], {
threadId: "thr_1",
});
expect(listed.exitCode).toBe(0);
expect(Buffer.byteLength(listed.stdout ?? "", "utf8")).toBeLessThan(
1_048_576,
);
const page = JSON.parse(listed.stdout ?? "");
expect(page.threads.length).toBeGreaterThan(0);
expect(page.threads.length).toBeLessThan(30);
expect(page.nextCursor).toEqual(expect.any(String));
const oversized = page.threads[0];
for (let index = 0; index < 24; index += 1) {
await host.harness.callRpc("reply", {
bbThreadId: "thr_1",
commentThreadId: oversized.id,
body,
});
}
const fetched = await host.harness.runCli(
["get", oversized.id, "--json"],
{ threadId: "thr_1" },
);
expect(fetched.exitCode).toBe(0);
expect(Buffer.byteLength(fetched.stdout ?? "", "utf8")).toBeLessThan(
1_048_576,
);
const commentPage = JSON.parse(fetched.stdout ?? "");
expect(commentPage.comments.length).toBeGreaterThan(0);
expect(commentPage.comments.length).toBeLessThan(25);
expect(commentPage.nextCursor).toEqual(expect.any(String));
const exact = "😀".repeat(240_000);
host.bb.storage
.database()
.prepare(
`UPDATE comment_threads
SET selector_start = 0, selector_end = ?, selector_exact = ?
WHERE id = ?`,
)
.run(exact.length, exact, oversized.id);
const oneRecord = await host.harness.runCli(
["list", "--limit", "1", "--json"],
{ threadId: "thr_1" },
);
expect(oneRecord.exitCode).toBe(1);
expect(oneRecord.stderr).toContain(
"A single comment thread exceeds the CLI output size limit",
);
});
});