forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.test.ts
More file actions
177 lines (153 loc) · 4.51 KB
/
db.test.ts
File metadata and controls
177 lines (153 loc) · 4.51 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
import { test, expect, describe, beforeEach } from "bun:test"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import {
initDb,
getDb,
withTransaction,
CURRENT_SCHEMA_VERSION,
__resetDbForTests,
} from "../src/lib/db"
const tmpDbPath = () =>
path.join(
os.tmpdir(),
`copilot-api-test-${Date.now()}-${Math.random().toString(36).slice(2)}.sqlite`,
)
describe("db module", () => {
beforeEach(() => {
__resetDbForTests()
})
test("initDb on a fresh path creates all tables and sets schema_version", () => {
const p = tmpDbPath()
const db = initDb(p)
const tables = (
db
.prepare(
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name",
)
.all() as Array<{ name: string }>
).map((r) => r.name)
for (const t of [
"accounts",
"model_pricing",
"model_pricing_versions",
"pricing_sync_log",
"usage_daily",
"usage_events",
"meta",
]) {
expect(tables).toContain(t)
}
const ver = db
.prepare("SELECT value FROM meta WHERE key='schema_version'")
.get() as { value: string } | undefined
expect(ver?.value).toBe(String(CURRENT_SCHEMA_VERSION))
db.close()
fs.unlinkSync(p)
})
test("initDb is idempotent: running twice leaves schema_version unchanged and does not duplicate rows", () => {
const p = tmpDbPath()
const db1 = initDb(p)
db1
.prepare(
"INSERT INTO meta (key, value) VALUES ('marker', 'persisted') "
+ "ON CONFLICT(key) DO UPDATE SET value=excluded.value",
)
.run()
db1.close()
__resetDbForTests()
const db2 = initDb(p)
const marker = db2
.prepare("SELECT value FROM meta WHERE key='marker'")
.get() as { value: string } | undefined
expect(marker?.value).toBe("persisted")
const ver = db2
.prepare("SELECT value FROM meta WHERE key='schema_version'")
.get() as { value: string } | undefined
expect(ver?.value).toBe(String(CURRENT_SCHEMA_VERSION))
db2.close()
fs.unlinkSync(p)
})
test("getDb throws before initDb is called", () => {
expect(() => getDb()).toThrow()
})
test("getDb returns the initialized instance", () => {
const p = tmpDbPath()
const db = initDb(p)
expect(getDb()).toBe(db)
db.close()
fs.unlinkSync(p)
})
test("withTransaction commits on success", () => {
const p = tmpDbPath()
const db = initDb(p)
withTransaction((d) => {
d.prepare(
"INSERT INTO accounts (name, account_type, created_at) "
+ "VALUES ('a', 'individual', 1)",
).run()
})
const row = db.prepare("SELECT name FROM accounts WHERE name='a'").get() as
| { name: string }
| undefined
expect(row?.name).toBe("a")
db.close()
fs.unlinkSync(p)
})
test("withTransaction rolls back on throw", () => {
const p = tmpDbPath()
const db = initDb(p)
expect(() =>
withTransaction((d) => {
d.prepare(
"INSERT INTO accounts (name, account_type, created_at) "
+ "VALUES ('b', 'individual', 1)",
).run()
throw new Error("boom")
}),
).toThrow("boom")
const row = db.prepare("SELECT name FROM accounts WHERE name='b'").get() as
| { name: string }
| undefined
expect(row).toBeUndefined()
db.close()
fs.unlinkSync(p)
})
test("WAL mode is enabled", () => {
const p = tmpDbPath()
const db = initDb(p)
const mode = db.pragma("journal_mode")
// bun:sqlite returns { journal_mode: "wal" }, better-sqlite3 returns [{ journal_mode: "wal" }]
let val: string
if (Array.isArray(mode)) {
val = (mode as Array<{ journal_mode: string }>)[0].journal_mode
} else if (
typeof mode === "object"
&& mode !== null
&& "journal_mode" in mode
) {
val = (mode as { journal_mode: string }).journal_mode
} else {
val = String(mode)
}
expect(val.toLowerCase()).toBe("wal")
db.close()
fs.unlinkSync(p)
})
test("schema includes expected indexes", () => {
const p = tmpDbPath()
const db = initDb(p)
const idxs = (
db
.prepare("SELECT name FROM sqlite_master WHERE type='index'")
.all() as Array<{ name: string }>
).map((r) => r.name)
expect(idxs).toContain("idx_usage_account_model_ts")
expect(idxs).toContain("idx_usage_ts")
expect(idxs).toContain("idx_pricing_versions_model_time")
expect(idxs).toContain("idx_pricing_versions_current")
db.close()
fs.unlinkSync(p)
})
})