Skip to content

Commit 7e64c74

Browse files
committed
test: session utilities — isDefaultTitle, fromRow/toRow, createObservationMask
Add 17 tests covering two untested modules in the session subsystem: session identity helpers and compaction observation masks. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 9ba2114 commit 7e64c74

2 files changed

Lines changed: 318 additions & 0 deletions

File tree

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
import { describe, test, expect } from "bun:test"
2+
import { SessionCompaction } from "../../src/session/compaction"
3+
import type { MessageV2 } from "../../src/session/message-v2"
4+
5+
function makeToolPart(overrides: {
6+
tool?: string
7+
input?: Record<string, any>
8+
output?: string
9+
status?: "completed" | "running" | "error" | "pending"
10+
}): MessageV2.ToolPart {
11+
const status = overrides.status ?? "completed"
12+
const base = {
13+
id: "part_1" as any,
14+
sessionID: "sess_1" as any,
15+
messageID: "msg_1" as any,
16+
type: "tool" as const,
17+
callID: "call_1",
18+
tool: overrides.tool ?? "bash",
19+
}
20+
21+
if (status === "completed") {
22+
return {
23+
...base,
24+
state: {
25+
status: "completed",
26+
input: overrides.input ?? {},
27+
output: overrides.output ?? "",
28+
title: "test",
29+
metadata: {},
30+
time: { start: 1, end: 2 },
31+
},
32+
} as MessageV2.ToolPart
33+
}
34+
35+
if (status === "running") {
36+
return {
37+
...base,
38+
state: {
39+
status: "running",
40+
input: overrides.input ?? {},
41+
time: { start: 1 },
42+
},
43+
} as MessageV2.ToolPart
44+
}
45+
46+
if (status === "error") {
47+
return {
48+
...base,
49+
state: {
50+
status: "error",
51+
input: overrides.input ?? {},
52+
error: "something failed",
53+
time: { start: 1, end: 2 },
54+
},
55+
} as MessageV2.ToolPart
56+
}
57+
58+
return {
59+
...base,
60+
state: { status: "pending" },
61+
} as MessageV2.ToolPart
62+
}
63+
64+
describe("SessionCompaction.createObservationMask", () => {
65+
test("produces correct mask for a completed tool with normal output", () => {
66+
const part = makeToolPart({
67+
tool: "read",
68+
input: { file_path: "/src/index.ts" },
69+
output: "line1\nline2\nline3",
70+
})
71+
const mask = SessionCompaction.createObservationMask(part)
72+
73+
expect(mask).toContain("[Tool output cleared")
74+
expect(mask).toContain("read(")
75+
expect(mask).toContain("file_path:")
76+
expect(mask).toContain("returned 3 lines")
77+
// "line1\nline2\nline3" = 17 bytes ASCII
78+
expect(mask).toContain("17 B")
79+
// First line fingerprint
80+
expect(mask).toContain('— "line1"')
81+
})
82+
83+
test("handles empty output gracefully", () => {
84+
const part = makeToolPart({
85+
tool: "bash",
86+
input: { command: "echo hello" },
87+
output: "",
88+
})
89+
const mask = SessionCompaction.createObservationMask(part)
90+
91+
expect(mask).toContain("returned 1 lines")
92+
expect(mask).toContain("0 B")
93+
// No fingerprint for empty output
94+
expect(mask).not.toContain('— "')
95+
})
96+
97+
test("counts lines and bytes correctly for multi-line output", () => {
98+
// Generate enough lines to exceed 1 KB
99+
const lines = Array.from({ length: 200 }, (_, i) => `output line number ${i}`)
100+
const output = lines.join("\n")
101+
const part = makeToolPart({ tool: "grep", output })
102+
const mask = SessionCompaction.createObservationMask(part)
103+
104+
expect(mask).toContain("returned 200 lines")
105+
const expectedBytes = Buffer.byteLength(output, "utf8")
106+
expect(expectedBytes).toBeGreaterThan(1024)
107+
expect(mask).toMatch(/\d+\.\d+ KB/)
108+
expect(mask).toContain('— "output line number 0"')
109+
})
110+
111+
test("formats bytes as KB and MB for larger outputs", () => {
112+
// ~1 MB output
113+
const bigOutput = "x".repeat(1024 * 1024 + 512)
114+
const part = makeToolPart({ tool: "cat", output: bigOutput })
115+
const mask = SessionCompaction.createObservationMask(part)
116+
117+
expect(mask).toMatch(/1\.0 MB/)
118+
})
119+
120+
test("truncates long args without breaking surrogate pairs", () => {
121+
// Create input with a value containing emoji (surrogate pairs in JS)
122+
// U+1F600 = 😀 is represented as \uD83D\uDE00 in UTF-16
123+
const emoji = "😀"
124+
// Build a value string where the emoji sits right near the truncation boundary
125+
const padding = "a".repeat(70)
126+
const part = makeToolPart({
127+
tool: "write",
128+
input: { content: padding + emoji + "after" },
129+
})
130+
const mask = SessionCompaction.createObservationMask(part)
131+
132+
// The mask should not contain a lone high surrogate
133+
// Verify the args portion is well-formed by checking the whole mask is valid
134+
expect(mask).toBeDefined()
135+
// Ensure no lone surrogates by round-tripping through TextEncoder
136+
const encoded = new TextEncoder().encode(mask)
137+
const decoded = new TextDecoder().decode(encoded)
138+
expect(decoded).toBe(mask)
139+
})
140+
141+
test("uses empty input for pending tool parts", () => {
142+
const part = makeToolPart({ tool: "bash", status: "pending" })
143+
const mask = SessionCompaction.createObservationMask(part)
144+
145+
expect(mask).toContain("bash()")
146+
expect(mask).toContain("returned 1 lines")
147+
expect(mask).toContain("0 B")
148+
})
149+
150+
test("uses input from running tool parts", () => {
151+
const part = makeToolPart({
152+
tool: "edit",
153+
input: { file: "main.ts" },
154+
status: "running",
155+
})
156+
const mask = SessionCompaction.createObservationMask(part)
157+
158+
expect(mask).toContain("edit(file:")
159+
// Running parts have no output
160+
expect(mask).toContain("0 B")
161+
})
162+
163+
test("uses input from error tool parts", () => {
164+
const part = makeToolPart({
165+
tool: "bash",
166+
input: { command: "rm -rf /" },
167+
status: "error",
168+
})
169+
const mask = SessionCompaction.createObservationMask(part)
170+
171+
expect(mask).toContain("bash(command:")
172+
expect(mask).toContain("0 B")
173+
})
174+
})
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { describe, test, expect } from "bun:test"
2+
import { Session } from "../../src/session"
3+
4+
describe("Session.isDefaultTitle", () => {
5+
test("recognises parent session titles", () => {
6+
expect(Session.isDefaultTitle("New session - 2024-01-15T08:30:00.000Z")).toBe(true)
7+
})
8+
9+
test("recognises child session titles", () => {
10+
expect(Session.isDefaultTitle("Child session - 2024-06-01T23:59:59.999Z")).toBe(true)
11+
})
12+
13+
test("rejects arbitrary strings", () => {
14+
expect(Session.isDefaultTitle("My custom title")).toBe(false)
15+
expect(Session.isDefaultTitle("")).toBe(false)
16+
expect(Session.isDefaultTitle("hello world")).toBe(false)
17+
})
18+
19+
test("rejects prefix without valid ISO timestamp", () => {
20+
expect(Session.isDefaultTitle("New session - not-a-date")).toBe(false)
21+
expect(Session.isDefaultTitle("New session - ")).toBe(false)
22+
expect(Session.isDefaultTitle("New session - 2024-01-15")).toBe(false)
23+
})
24+
25+
test("rejects partial prefix matches", () => {
26+
expect(Session.isDefaultTitle("New session 2024-01-15T08:30:00.000Z")).toBe(false)
27+
expect(Session.isDefaultTitle("New session -2024-01-15T08:30:00.000Z")).toBe(false)
28+
})
29+
30+
test("rejects titles with extra content after timestamp", () => {
31+
expect(Session.isDefaultTitle("New session - 2024-01-15T08:30:00.000Z extra")).toBe(false)
32+
})
33+
})
34+
35+
describe("Session.fromRow / toRow", () => {
36+
test("roundtrip preserves fields with full summary", () => {
37+
const info: Session.Info = {
38+
id: "sess_123" as any,
39+
slug: "abc-def",
40+
projectID: "proj_456" as any,
41+
workspaceID: "ws_789" as any,
42+
directory: "/home/user/project",
43+
parentID: "sess_parent" as any,
44+
title: "Test session",
45+
version: "0.5.13",
46+
summary: {
47+
additions: 10,
48+
deletions: 5,
49+
files: 3,
50+
diffs: [{ file: "src/index.ts", additions: 10, deletions: 5 }] as any,
51+
},
52+
share: { url: "https://example.com/share/123" },
53+
revert: "snapshot_abc" as any,
54+
permission: "plan" as any,
55+
time: {
56+
created: 1700000000000,
57+
updated: 1700001000000,
58+
compacting: 1700002000000,
59+
archived: 1700003000000,
60+
},
61+
}
62+
63+
const row = Session.toRow(info)
64+
const restored = Session.fromRow(row as any)
65+
66+
expect(restored.id).toBe(info.id)
67+
expect(restored.slug).toBe(info.slug)
68+
expect(restored.projectID).toBe(info.projectID)
69+
expect(restored.workspaceID).toBe(info.workspaceID)
70+
expect(restored.directory).toBe(info.directory)
71+
expect(restored.parentID).toBe(info.parentID)
72+
expect(restored.title).toBe(info.title)
73+
expect(restored.version).toBe(info.version)
74+
expect(restored.summary).toEqual(info.summary)
75+
expect(restored.share).toEqual(info.share)
76+
expect(restored.revert).toBe(info.revert)
77+
expect(restored.permission).toBe(info.permission)
78+
expect(restored.time.created).toBe(info.time.created)
79+
expect(restored.time.updated).toBe(info.time.updated)
80+
expect(restored.time.compacting).toBe(info.time.compacting)
81+
expect(restored.time.archived).toBe(info.time.archived)
82+
})
83+
84+
test("fromRow produces undefined summary when all summary columns are null", () => {
85+
const row = {
86+
id: "sess_1",
87+
slug: "x",
88+
project_id: "p1",
89+
workspace_id: null,
90+
directory: "/tmp",
91+
parent_id: null,
92+
title: "t",
93+
version: "1",
94+
summary_additions: null,
95+
summary_deletions: null,
96+
summary_files: null,
97+
summary_diffs: null,
98+
share_url: null,
99+
revert: null,
100+
permission: null,
101+
time_created: 1,
102+
time_updated: 2,
103+
time_compacting: null,
104+
time_archived: null,
105+
}
106+
const info = Session.fromRow(row as any)
107+
expect(info.summary).toBeUndefined()
108+
expect(info.share).toBeUndefined()
109+
expect(info.revert).toBeUndefined()
110+
expect(info.parentID).toBeUndefined()
111+
expect(info.workspaceID).toBeUndefined()
112+
expect(info.time.compacting).toBeUndefined()
113+
expect(info.time.archived).toBeUndefined()
114+
})
115+
116+
test("fromRow constructs summary when at least one summary column is non-null", () => {
117+
const row = {
118+
id: "sess_2",
119+
slug: "y",
120+
project_id: "p2",
121+
workspace_id: null,
122+
directory: "/tmp",
123+
parent_id: null,
124+
title: "t",
125+
version: "1",
126+
summary_additions: 5,
127+
summary_deletions: null,
128+
summary_files: null,
129+
summary_diffs: null,
130+
share_url: null,
131+
revert: null,
132+
permission: null,
133+
time_created: 1,
134+
time_updated: 2,
135+
time_compacting: null,
136+
time_archived: null,
137+
}
138+
const info = Session.fromRow(row as any)
139+
expect(info.summary).toBeDefined()
140+
expect(info.summary!.additions).toBe(5)
141+
expect(info.summary!.deletions).toBe(0)
142+
expect(info.summary!.files).toBe(0)
143+
})
144+
})

0 commit comments

Comments
 (0)