|
1 | 1 | import * as NodeAssert from "node:assert/strict"; |
| 2 | +import * as NodeSqlite from "node:sqlite"; |
2 | 3 |
|
3 | 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
4 | 5 | import { it } from "@effect/vitest"; |
@@ -98,6 +99,47 @@ it.effect("reads Go limits with the instance's XDG credentials and preserves res |
98 | 99 | }), |
99 | 100 | ); |
100 | 101 |
|
| 102 | +it.effect("reads a Go API key from OpenCode 2's credential database", () => |
| 103 | + Effect.gen(function* () { |
| 104 | + const fs = yield* FileSystem.FileSystem; |
| 105 | + const dataHome = yield* fs.makeTempDirectoryScoped(); |
| 106 | + yield* fs.makeDirectory(`${dataHome}/opencode`); |
| 107 | + const database = new NodeSqlite.DatabaseSync(`${dataHome}/opencode/opencode.db`); |
| 108 | + database.exec( |
| 109 | + "CREATE TABLE credential (integration_id text, value text, active integer, time_updated integer)", |
| 110 | + ); |
| 111 | + const insert = database.prepare("INSERT INTO credential VALUES (?, ?, ?, ?)"); |
| 112 | + // A Console login and an inactive Go key must lose to the active Go key. |
| 113 | + insert.run("opencode", JSON.stringify({ type: "oauth", access: "console-token" }), 1, 3); |
| 114 | + insert.run("opencode-go", JSON.stringify({ type: "api", key: "old-key" }), 0, 2); |
| 115 | + insert.run("opencode-go", JSON.stringify({ type: "api", key: "db-key" }), 1, 1); |
| 116 | + database.close(); |
| 117 | + |
| 118 | + const resetsAt = "2026-09-17T12:00:00.000Z"; |
| 119 | + const window = { percent: 1, resetsAt }; |
| 120 | + const limits = yield* readOpenCodeGoUsageLimits({ |
| 121 | + enabled: true, |
| 122 | + serverUrl: "", |
| 123 | + environment: { XDG_DATA_HOME: dataHome, OPENCODE_API_KEY: "env-key" }, |
| 124 | + }).pipe( |
| 125 | + Effect.provideService( |
| 126 | + HttpClient.HttpClient, |
| 127 | + HttpClient.make((request) => { |
| 128 | + NodeAssert.equal(request.headers.authorization, "Bearer db-key"); |
| 129 | + return Effect.succeed( |
| 130 | + HttpClientResponse.fromWeb( |
| 131 | + request, |
| 132 | + Response.json({ usage: { rolling: window, weekly: window, monthly: window } }), |
| 133 | + ), |
| 134 | + ); |
| 135 | + }), |
| 136 | + ), |
| 137 | + ); |
| 138 | + NodeAssert.equal(limits.unavailable, undefined); |
| 139 | + NodeAssert.equal(limits.windows.length, 3); |
| 140 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 141 | +); |
| 142 | + |
101 | 143 | it.effect("does not read local credentials for external or disabled OpenCode instances", () => |
102 | 144 | Effect.gen(function* () { |
103 | 145 | for (const settings of [ |
|
0 commit comments