|
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,137 @@ it.effect("reads Go limits with the instance's XDG credentials and preserves res |
98 | 99 | }), |
99 | 100 | ); |
100 | 101 |
|
| 102 | +it.effect( |
| 103 | + "uses the selected public Console key for Go limits, falling back only on auth rejection", |
| 104 | + () => |
| 105 | + Effect.gen(function* () { |
| 106 | + const fs = yield* FileSystem.FileSystem; |
| 107 | + const dataHome = yield* fs.makeTempDirectoryScoped(); |
| 108 | + yield* fs.makeDirectory(`${dataHome}/opencode`); |
| 109 | + yield* fs.writeFileString( |
| 110 | + `${dataHome}/opencode/auth.json`, |
| 111 | + JSON.stringify({ "opencode-go": { type: "api", key: "go-key" } }), |
| 112 | + ); |
| 113 | + const database = yield* Effect.acquireRelease( |
| 114 | + Effect.sync(() => new NodeSqlite.DatabaseSync(`${dataHome}/opencode/opencode.db`)), |
| 115 | + (database) => Effect.sync(() => database.close()), |
| 116 | + ); |
| 117 | + database.exec( |
| 118 | + "CREATE TABLE credential (id text PRIMARY KEY, integration_id text, value text, active integer, time_created integer, time_updated integer)", |
| 119 | + ); |
| 120 | + const insert = database.prepare("INSERT INTO credential VALUES (?, ?, ?, ?, ?, ?)"); |
| 121 | + const window = { percent: 1, resetsAt: "2026-09-17T12:00:00.000Z" }; |
| 122 | + const consoleKey = { type: "key", key: "console-key" }; |
| 123 | + const fingerprints = new Map([ |
| 124 | + ["console-key", "3e45cfde9158185930d4cc7020a4e390895574fd54bd8f9631d23a336bbf94f0"], |
| 125 | + ["go-key", "d0e590e93fc9bd9353f2b993c0c287391cae505da2a78cb698b75418d2f5367e"], |
| 126 | + ["inline-key", "85649de18e63305548b6407e185f708483727ee6189cc4bc9512ddb17e23e776"], |
| 127 | + ]); |
| 128 | + for (const { |
| 129 | + consoleRow = consoleKey, |
| 130 | + active = 1, |
| 131 | + consoleStatus = 200, |
| 132 | + inline = false, |
| 133 | + databaseFile, |
| 134 | + requested, |
| 135 | + } of [ |
| 136 | + { |
| 137 | + consoleRow: { ...consoleKey, metadata: { server: "https://opencode.ai/console" } }, |
| 138 | + requested: ["console-key"], |
| 139 | + }, |
| 140 | + { consoleStatus: 401, requested: ["console-key", "go-key"] }, |
| 141 | + { consoleStatus: 403, requested: ["console-key", "go-key"] }, |
| 142 | + { consoleStatus: 500, requested: ["console-key"] }, |
| 143 | + { active: 0, requested: ["go-key"] }, |
| 144 | + { |
| 145 | + consoleRow: { |
| 146 | + type: "oauth", |
| 147 | + methodID: "device", |
| 148 | + access: "token", |
| 149 | + refresh: "refresh", |
| 150 | + expires: 0, |
| 151 | + }, |
| 152 | + requested: ["go-key"], |
| 153 | + }, |
| 154 | + { |
| 155 | + consoleRow: { ...consoleKey, metadata: { server: "https://console.example" } }, |
| 156 | + requested: ["go-key"], |
| 157 | + }, |
| 158 | + { inline: true, requested: ["inline-key"] }, |
| 159 | + { databaseFile: "selected.db", requested: ["console-key"] }, |
| 160 | + { databaseFile: ":memory:", requested: ["go-key"] }, |
| 161 | + { active: null, requested: ["console-key"] }, |
| 162 | + ]) { |
| 163 | + database.exec("DELETE FROM credential"); |
| 164 | + insert.run("cred_selected", "opencode", JSON.stringify(consoleRow), active, 2, 2); |
| 165 | + // Legacy selections follow creation order, even when an older key was updated later. |
| 166 | + insert.run( |
| 167 | + "cred_older", |
| 168 | + "opencode", |
| 169 | + JSON.stringify({ type: "key", key: "old-key" }), |
| 170 | + active === null ? null : 0, |
| 171 | + 1, |
| 172 | + 9, |
| 173 | + ); |
| 174 | + insert.run("cred_other", "other", JSON.stringify(consoleKey), 1, 10, 10); |
| 175 | + if (databaseFile === "selected.db") { |
| 176 | + yield* fs.writeFile( |
| 177 | + `${dataHome}/opencode/selected.db`, |
| 178 | + yield* fs.readFile(`${dataHome}/opencode/opencode.db`), |
| 179 | + ); |
| 180 | + database |
| 181 | + .prepare("UPDATE credential SET value = ? WHERE id = 'cred_selected'") |
| 182 | + .run(JSON.stringify({ type: "key", key: "old-key" })); |
| 183 | + } |
| 184 | + const before = yield* fs.readFile(`${dataHome}/opencode/opencode.db`); |
| 185 | + const authorizations: string[] = []; |
| 186 | + const limits = yield* readOpenCodeGoUsageLimits({ |
| 187 | + enabled: true, |
| 188 | + serverUrl: "", |
| 189 | + environment: { |
| 190 | + XDG_DATA_HOME: dataHome, |
| 191 | + ...(databaseFile !== undefined ? { OPENCODE_DB: databaseFile } : {}), |
| 192 | + ...(inline |
| 193 | + ? { OPENCODE_AUTH_CONTENT: '{"opencode-go":{"type":"api","key":"inline-key"}}' } |
| 194 | + : {}), |
| 195 | + }, |
| 196 | + }).pipe( |
| 197 | + Effect.provideService( |
| 198 | + HttpClient.HttpClient, |
| 199 | + HttpClient.make((request) => { |
| 200 | + NodeAssert.equal(request.url, "https://opencode.ai/zen/go/v1/usage"); |
| 201 | + const key = (request.headers.authorization ?? "").replace("Bearer ", ""); |
| 202 | + authorizations.push(key); |
| 203 | + // The legacy key is expired in the regression; it only works in fallback scenarios. |
| 204 | + const status = |
| 205 | + key === "console-key" ? consoleStatus : requested.includes(key) ? 200 : 401; |
| 206 | + return Effect.succeed( |
| 207 | + HttpClientResponse.fromWeb( |
| 208 | + request, |
| 209 | + Response.json( |
| 210 | + { usage: { rolling: window, weekly: window, monthly: window } }, |
| 211 | + { status }, |
| 212 | + ), |
| 213 | + ), |
| 214 | + ); |
| 215 | + }), |
| 216 | + ), |
| 217 | + ); |
| 218 | + NodeAssert.deepEqual(authorizations, requested); |
| 219 | + NodeAssert.deepEqual(yield* fs.readFile(`${dataHome}/opencode/opencode.db`), before); |
| 220 | + NodeAssert.equal( |
| 221 | + limits.unavailable?.reason, |
| 222 | + consoleStatus === 500 ? "probeFailed" : undefined, |
| 223 | + ); |
| 224 | + NodeAssert.equal(limits.windows.length, consoleStatus === 500 ? 0 : 3); |
| 225 | + NodeAssert.equal( |
| 226 | + limits.credentialFingerprint, |
| 227 | + consoleStatus === 500 ? undefined : fingerprints.get(requested.at(-1)!), |
| 228 | + ); |
| 229 | + } |
| 230 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 231 | +); |
| 232 | + |
101 | 233 | it.effect("does not read local credentials for external or disabled OpenCode instances", () => |
102 | 234 | Effect.gen(function* () { |
103 | 235 | for (const settings of [ |
|
0 commit comments