summaryrefslogtreecommitdiffhomepage
path: root/packages/app/src/context/comments.test.ts
diff options
context:
space:
mode:
authorAdam <[email protected]>2026-02-06 09:37:49 -0600
committerGitHub <[email protected]>2026-02-06 09:37:49 -0600
commita4bc883595df9ea0f752079519081bc602408553 (patch)
tree583f21642f431899abe1dfb1f6bd9b2c01dc0206 /packages/app/src/context/comments.test.ts
parentc07077f96c0019b2e18e0e8e1e0383deda08b3e6 (diff)
downloadopencode-a4bc883595df9ea0f752079519081bc602408553.tar.gz
opencode-a4bc883595df9ea0f752079519081bc602408553.zip
chore: refactoring and tests (#12468)
Diffstat (limited to 'packages/app/src/context/comments.test.ts')
-rw-r--r--packages/app/src/context/comments.test.ts111
1 files changed, 111 insertions, 0 deletions
diff --git a/packages/app/src/context/comments.test.ts b/packages/app/src/context/comments.test.ts
new file mode 100644
index 000000000..13cb132c4
--- /dev/null
+++ b/packages/app/src/context/comments.test.ts
@@ -0,0 +1,111 @@
+import { beforeAll, describe, expect, mock, test } from "bun:test"
+import { createRoot } from "solid-js"
+import type { LineComment } from "./comments"
+
+let createCommentSessionForTest: typeof import("./comments").createCommentSessionForTest
+
+beforeAll(async () => {
+ mock.module("@solidjs/router", () => ({
+ useParams: () => ({}),
+ }))
+ mock.module("@opencode-ai/ui/context", () => ({
+ createSimpleContext: () => ({
+ use: () => undefined,
+ provider: () => undefined,
+ }),
+ }))
+ const mod = await import("./comments")
+ createCommentSessionForTest = mod.createCommentSessionForTest
+})
+
+function line(file: string, id: string, time: number): LineComment {
+ return {
+ id,
+ file,
+ comment: id,
+ time,
+ selection: { start: 1, end: 1 },
+ }
+}
+
+describe("comments session indexing", () => {
+ test("keeps file list behavior and aggregate chronological order", () => {
+ createRoot((dispose) => {
+ const now = Date.now()
+ const comments = createCommentSessionForTest({
+ "a.ts": [line("a.ts", "a-late", now + 20_000), line("a.ts", "a-early", now + 1_000)],
+ "b.ts": [line("b.ts", "b-mid", now + 10_000)],
+ })
+
+ expect(comments.list("a.ts").map((item) => item.id)).toEqual(["a-late", "a-early"])
+ expect(comments.all().map((item) => item.id)).toEqual(["a-early", "b-mid", "a-late"])
+
+ const next = comments.add({
+ file: "b.ts",
+ comment: "next",
+ selection: { start: 2, end: 2 },
+ })
+
+ expect(comments.list("b.ts").at(-1)?.id).toBe(next.id)
+ expect(comments.all().map((item) => item.time)).toEqual(
+ comments
+ .all()
+ .map((item) => item.time)
+ .slice()
+ .sort((a, b) => a - b),
+ )
+
+ dispose()
+ })
+ })
+
+ test("remove updates file and aggregate indexes consistently", () => {
+ createRoot((dispose) => {
+ const comments = createCommentSessionForTest({
+ "a.ts": [line("a.ts", "a1", 10), line("a.ts", "shared", 20)],
+ "b.ts": [line("b.ts", "shared", 30)],
+ })
+
+ comments.setFocus({ file: "a.ts", id: "shared" })
+ comments.setActive({ file: "a.ts", id: "shared" })
+ comments.remove("a.ts", "shared")
+
+ expect(comments.list("a.ts").map((item) => item.id)).toEqual(["a1"])
+ expect(
+ comments
+ .all()
+ .filter((item) => item.id === "shared")
+ .map((item) => item.file),
+ ).toEqual(["b.ts"])
+ expect(comments.focus()).toBeNull()
+ expect(comments.active()).toEqual({ file: "a.ts", id: "shared" })
+
+ dispose()
+ })
+ })
+
+ test("clear resets file and aggregate indexes plus focus state", () => {
+ createRoot((dispose) => {
+ const comments = createCommentSessionForTest({
+ "a.ts": [line("a.ts", "a1", 10)],
+ })
+
+ const next = comments.add({
+ file: "b.ts",
+ comment: "next",
+ selection: { start: 2, end: 2 },
+ })
+
+ comments.setActive({ file: "b.ts", id: next.id })
+ comments.clear()
+
+ expect(comments.list("a.ts")).toEqual([])
+ expect(comments.list("b.ts")).toEqual([])
+ expect(comments.all()).toEqual([])
+ expect(comments.focus()).toBeNull()
+ expect(comments.active()).toBeNull()
+
+ dispose()
+ })
+ })
+})