diff --git a/plugins/opencode-go-usage-source/server/stores.ts b/plugins/opencode-go-usage-source/server/stores.ts index c3108055a7..658c854a55 100644 --- a/plugins/opencode-go-usage-source/server/stores.ts +++ b/plugins/opencode-go-usage-source/server/stores.ts @@ -28,7 +28,7 @@ const keySchema = z.object({ type: z.literal("key"), key: z.string().min(1) }); export function defaultStorePaths(env: NodeJS.ProcessEnv = process.env): StorePaths { const dataHome = env.XDG_DATA_HOME || join(env.HOME || homedir(), ".local", "share"); const directory = join(dataHome, "opencode"); - const database = env.OPENCODE_DB ? resolve(env.OPENCODE_DB) : join(directory, "opencode.db"); + const database = resolve(directory, env.OPENCODE_DB || "opencode.db"); return { file: join(directory, "auth.json"), database }; } diff --git a/plugins/opencode-go-usage-source/server/usage.test.ts b/plugins/opencode-go-usage-source/server/usage.test.ts index 022b150c17..18d44ab6fa 100644 --- a/plugins/opencode-go-usage-source/server/usage.test.ts +++ b/plugins/opencode-go-usage-source/server/usage.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { afterEach, beforeEach, expect, test } from "vitest"; @@ -181,6 +181,34 @@ test("resolves default and overridden OpenCode store paths", () => { }); }); +test("discovers a relative OPENCODE_DB under the OpenCode data directory", async () => { + const paths = defaultStorePaths({ + XDG_DATA_HOME: fixtureDirectory, + OPENCODE_DB: "go.db", + }); + const databasePath = join(fixtureDirectory, "opencode", "go.db"); + await mkdir(dirname(databasePath)); + const { DatabaseSync } = await import("node:sqlite"); + const db = new DatabaseSync(databasePath); + db.exec( + "CREATE TABLE credential (id TEXT PRIMARY KEY, integration_id TEXT, value TEXT, active INTEGER, time_updated INTEGER)", + ); + db.prepare("INSERT INTO credential VALUES (?, ?, ?, ?, ?)").run( + "go", + "opencode-go", + JSON.stringify({ type: "key", key: "relative-key" }), + 1, + 1, + ); + db.close(); + expect(await discover(paths)).toEqual([ + { + key: expect.stringMatching(/^[a-f0-9]{64}$/), + input: { store: "database", path: databasePath }, + }, + ]); +}); + test("discovers and fetches the default key from read-only auth.json", async () => { const directory = await mkdtemp(join(tmpdir(), "paseo-go-usage-")); const path = join(directory, "auth.json");