forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-schema.test.ts
More file actions
78 lines (71 loc) · 2.52 KB
/
Copy pathsession-schema.test.ts
File metadata and controls
78 lines (71 loc) · 2.52 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
import { describe, expect, test } from "bun:test"
import { Schema } from "effect"
import { ProjectV2 } from "@opencode-ai/core/project"
import { MessageID, SessionID } from "../../src/session/schema"
import { Session } from "../../src/session/session"
const info = {
id: SessionID.descending(),
slug: "test-session",
projectID: ProjectV2.ID.global,
workspaceID: undefined,
directory: "/tmp/opencode",
parentID: undefined,
summary: undefined,
cost: 0,
tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } },
share: undefined,
title: "Test session",
version: "1.0.0",
time: {
created: 1,
updated: 2,
compacting: undefined,
archived: undefined,
},
permission: undefined,
revert: undefined,
} satisfies Session.Info
describe("Session schema", () => {
test("encodes undefined optional session fields as omitted keys", () => {
const encoded = Schema.encodeUnknownSync(Session.Info)(info) as Record<string, unknown>
for (const key of ["workspaceID", "parentID", "summary", "share", "permission", "revert"]) {
expect(Object.hasOwn(encoded, key)).toBe(false)
}
expect(Object.hasOwn(encoded.time as Record<string, unknown>, "compacting")).toBe(false)
expect(Object.hasOwn(encoded.time as Record<string, unknown>, "archived")).toBe(false)
expect(JSON.stringify(encoded)).not.toContain("parentID")
})
test("encodes undefined optional global session project fields as omitted keys", () => {
const encoded = Schema.encodeUnknownSync(Session.GlobalInfo)({
...info,
project: {
id: ProjectV2.ID.global,
name: undefined,
worktree: "/tmp/opencode",
},
}) as Record<string, unknown>
expect(Object.hasOwn(encoded, "parentID")).toBe(false)
expect(Object.hasOwn(encoded.project as Record<string, unknown>, "name")).toBe(false)
})
test("encodes nested undefined optional session fields as omitted keys", () => {
const encoded = Schema.encodeUnknownSync(Session.Info)({
...info,
summary: {
additions: 1,
deletions: 2,
files: 3,
diffs: undefined,
},
revert: {
messageID: MessageID.ascending(),
partID: undefined,
snapshot: undefined,
diff: undefined,
},
}) as Record<string, unknown>
expect(Object.hasOwn(encoded.summary as Record<string, unknown>, "diffs")).toBe(false)
for (const key of ["partID", "snapshot", "diff"]) {
expect(Object.hasOwn(encoded.revert as Record<string, unknown>, key)).toBe(false)
}
})
})