opencode/services/update/test/index.test.ts
opencode-agent[bot] 23e3493ab2
fix(update): cache D1 reads and use partial indexes for lookups (#55109)
Co-authored-by: neriousy <34747899+neriousy@users.noreply.github.com>
Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
2026-10-13 10:07:40 -05:00

142 lines
5.8 KiB
TypeScript

import { Database } from "bun:sqlite"
import { afterEach, expect, setSystemTime, test } from "bun:test"
import { readdirSync, readFileSync } from "node:fs"
import path from "node:path"
import worker from "../src/index"
type Env = Parameters<typeof worker.fetch>[1]
const migrations = path.join(import.meta.dir, "../migrations")
afterEach(() => setSystemTime())
// Runs the real schema and queries through bun:sqlite and records every statement sent to D1.
function database() {
const sqlite = new Database(":memory:")
readdirSync(migrations)
.sort()
.forEach((file) => sqlite.exec(readFileSync(path.join(migrations, file), "utf8")))
const queries: string[] = []
const DB = {
prepare: (sql: string) => ({
bind: (...values: unknown[]) => ({
first: async () => {
queries.push(sql)
return sqlite.query(sql).get(...(values as never[])) ?? null
},
all: async () => {
queries.push(sql)
return { results: sqlite.query(sql).all(...(values as never[])) }
},
}),
}),
}
const env = { DB, EVENTS: { send: () => Promise.resolve() } } as unknown as Env
const insert = (input: { channel: string; version: string; time: number; active?: boolean; minimum?: boolean }) =>
sqlite
.query(
"INSERT INTO artifact (channel, name, distribution, version, metadata, active, minimum, time_created, time_updated) VALUES (?, 'cli', 'opencode', ?, '{}', ?, ?, ?, ?)",
)
.run(input.channel, input.version, input.active ? 1 : 0, input.minimum ? 1 : 0, input.time, input.time)
return { sqlite, env, queries, insert }
}
function lookup(env: Env, pathname: string, headers?: HeadersInit) {
return worker.fetch(new Request(`https://opencode.ai/update/api/${pathname}`, { headers }), env, {
waitUntil: () => {},
})
}
async function version(response: Response) {
expect(response.status).toBe(200)
const body = (await response.json()) as { version: string }
return body.version
}
test("returns a retryable 503 when D1 rejects a lookup", async () => {
const overloaded = new Error("D1_ERROR: D1 DB is overloaded. Requests queued for too long.")
const env = {
DB: { prepare: () => ({ bind: () => ({ first: () => Promise.reject(overloaded) }) }) },
EVENTS: { send: () => Promise.resolve() },
} as unknown as Env
const response = await worker.fetch(
new Request("https://opencode.ai/update/api/latest/desktop/opencode/opencode-desktop-win-x64.exe"),
env,
{ waitUntil: () => {} },
)
expect(response.status).toBe(503)
expect(response.headers.get("Retry-After")).toBe("60")
expect(response.headers.get("Cache-Control")).toBe("no-store")
expect(await response.json()).toEqual({ error: "Update service temporarily unavailable" })
})
test("reuses D1 reads across lookups for 1 minute", async () => {
const db = database()
db.insert({ channel: "reuse", version: "1.0.0", time: 1, active: true })
setSystemTime(new Date("2026-10-13T00:00:00Z"))
expect(
await Promise.all([lookup(db.env, "reuse/cli/opencode"), lookup(db.env, "reuse/cli/opencode")]).then((responses) =>
Promise.all(responses.map(version)),
),
).toEqual(["1.0.0", "1.0.0"])
expect(db.queries).toHaveLength(2)
db.sqlite.query("UPDATE artifact SET active = 0").run()
db.insert({ channel: "reuse", version: "1.0.1", time: 2, active: true })
setSystemTime(new Date("2026-10-13T00:00:59Z"))
expect(await version(await lookup(db.env, "reuse/cli/opencode"))).toBe("1.0.0")
expect(db.queries).toHaveLength(2)
setSystemTime(new Date("2026-10-13T00:01:01Z"))
expect(await version(await lookup(db.env, "reuse/cli/opencode"))).toBe("1.0.1")
expect(db.queries).toHaveLength(4)
})
test("evicts stale and cold entries without clearing hot reads", async () => {
const db = database()
db.insert({ channel: "hot", version: "1.0.0", time: 1, active: true })
setSystemTime(new Date("2026-10-13T01:00:00Z"))
expect(await version(await lookup(db.env, "hot/cli/opencode"))).toBe("1.0.0")
expect(db.queries).toHaveLength(2)
for (let i = 0; i < 600; i++) {
expect((await lookup(db.env, `cold-${i}/cli/opencode`)).status).toBe(404)
expect(await version(await lookup(db.env, "hot/cli/opencode"))).toBe("1.0.0")
}
expect(db.queries).toHaveLength(1202)
})
test("retries D1 on the next lookup after a failed read", async () => {
const db = database()
db.insert({ channel: "retry", version: "1.0.0", time: 1, active: true })
const prepare = db.env.DB.prepare
let fail = true
db.env.DB.prepare = ((sql: string) =>
fail
? { bind: () => ({ first: () => Promise.reject(new Error("overloaded")) }) }
: prepare.call(db.env.DB, sql)) as typeof prepare
expect((await lookup(db.env, "retry/cli/opencode")).status).toBe(503)
fail = false
expect(await version(await lookup(db.env, "retry/cli/opencode"))).toBe("1.0.0")
})
test("selects the minimum and active artifacts from the partial indexes", async () => {
const db = database()
db.insert({ channel: "minimum", version: "1.0.0", time: 1, minimum: true })
db.insert({ channel: "minimum", version: "1.5.0", time: 2 })
db.insert({ channel: "minimum", version: "2.0.0", time: 3, active: true })
expect(await version(await lookup(db.env, "minimum/cli/opencode?current=0.9.0"))).toBe("1.0.0")
expect(await version(await lookup(db.env, "minimum/cli/opencode?current=1.0.0"))).toBe("2.0.0")
expect(await version(await lookup(db.env, "minimum/cli/opencode"))).toBe("2.0.0")
// A release that is both active and minimum is listed once.
db.insert({ channel: "both", version: "3.0.0", time: 1, active: true, minimum: true })
const response = await lookup(db.env, "both")
expect(response.status).toBe(200)
const body = (await response.json()) as { artifacts: { version: string }[] }
expect(body.artifacts.map((artifact) => artifact.version)).toEqual(["3.0.0"])
})