@@ -226,6 +226,75 @@ it.effect(
226226 } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
227227) ;
228228
229+ it . effect ( "prefers the selected stored Go key over the auth.json key OpenCode 2 imported" , ( ) =>
230+ Effect . gen ( function * ( ) {
231+ const fs = yield * FileSystem . FileSystem ;
232+ const dataHome = yield * fs . makeTempDirectoryScoped ( ) ;
233+ yield * fs . makeDirectory ( `${ dataHome } /opencode` ) ;
234+ yield * fs . writeFileString (
235+ `${ dataHome } /opencode/auth.json` ,
236+ JSON . stringify ( { "opencode-go" : { type : "api" , key : "imported-key" } } ) ,
237+ ) ;
238+ const database = yield * Effect . acquireRelease (
239+ Effect . sync ( ( ) => new NodeSqlite . DatabaseSync ( `${ dataHome } /opencode/opencode.db` ) ) ,
240+ ( database ) => Effect . sync ( ( ) => database . close ( ) ) ,
241+ ) ;
242+ database . exec (
243+ "CREATE TABLE credential (id text PRIMARY KEY, integration_id text, value text, active integer, time_created integer, time_updated integer)" ,
244+ ) ;
245+ const insert = database . prepare ( "INSERT INTO credential VALUES (?, ?, ?, ?, ?, ?)" ) ;
246+ insert . run (
247+ "cred_imported" ,
248+ "opencode-go" ,
249+ JSON . stringify ( { type : "key" , key : "imported-key" } ) ,
250+ 0 ,
251+ 1 ,
252+ 2 ,
253+ ) ;
254+ insert . run (
255+ "cred_replaced" ,
256+ "opencode-go" ,
257+ JSON . stringify ( { type : "key" , key : "replaced-key" } ) ,
258+ 1 ,
259+ 2 ,
260+ 2 ,
261+ ) ;
262+ const window = { percent : 1 , resetsAt : "2026-09-17T12:00:00.000Z" } ;
263+ for ( const { replacedStatus, requested } of [
264+ { replacedStatus : 200 , requested : [ "replaced-key" ] } ,
265+ { replacedStatus : 401 , requested : [ "replaced-key" , "imported-key" ] } ,
266+ ] ) {
267+ const authorizations : string [ ] = [ ] ;
268+ const limits = yield * readOpenCodeGoUsageLimits ( {
269+ enabled : true ,
270+ serverUrl : "" ,
271+ environment : { XDG_DATA_HOME : dataHome } ,
272+ } ) . pipe (
273+ Effect . provideService (
274+ HttpClient . HttpClient ,
275+ HttpClient . make ( ( request ) => {
276+ const key = ( request . headers . authorization ?? "" ) . replace ( "Bearer " , "" ) ;
277+ authorizations . push ( key ) ;
278+ const status = key === "replaced-key" ? replacedStatus : 200 ;
279+ return Effect . succeed (
280+ HttpClientResponse . fromWeb (
281+ request ,
282+ Response . json (
283+ { usage : { rolling : window , weekly : window , monthly : window } } ,
284+ { status } ,
285+ ) ,
286+ ) ,
287+ ) ;
288+ } ) ,
289+ ) ,
290+ ) ;
291+ NodeAssert . deepEqual ( authorizations , requested ) ;
292+ NodeAssert . equal ( limits . unavailable , undefined ) ;
293+ NodeAssert . equal ( limits . windows . length , 3 ) ;
294+ }
295+ } ) . pipe ( Effect . scoped , Effect . provide ( NodeServices . layer ) ) ,
296+ ) ;
297+
229298it . effect ( "does not read local credentials for external or disabled OpenCode instances" , ( ) =>
230299 Effect . gen ( function * ( ) {
231300 for ( const settings of [
0 commit comments