@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, test } from "bun:test";
22import { mkdirSync , mkdtempSync , readFileSync , rmSync , writeFileSync } from "node:fs" ;
33import { tmpdir } from "node:os" ;
44import { join } from "node:path" ;
5- import { injectGrokConfig } from "../src/grok/inject" ;
5+ import { injectGrokConfig , stripGrokConfig } from "../src/grok/inject" ;
66
77/**
88 * #511 — Grok Build reported 200k for every model.
@@ -180,12 +180,118 @@ describe("Grok orphan adoption (#511)", () => {
180180 "" ,
181181 ] . join ( "\n" ) ) ;
182182
183- injectGrokConfig ( 10100 , MODELS , { grokHome } ) ;
183+ const result = injectGrokConfig ( 10100 , MODELS , { grokHome } ) ;
184+ expect ( result ) . toMatchObject ( { ok : true , changed : true } ) ;
184185 const content = readFileSync ( configPath , "utf8" ) ;
185186 expect ( content ) . toContain ( "[model.ocx-unknown]" ) ;
186187 expect ( content ) . toContain ( 'api_key = "opencodex-loopback"' ) ;
187188 } ) ;
188189
190+ test ( "removes a hidden current orphan but preserves a genuinely retired one" , ( ) => {
191+ writeFileSync ( configPath , [
192+ "[model.ocx-hidden]" ,
193+ 'model = "hidden/model"' ,
194+ 'base_url = "http://127.0.0.1:10100/v1"' ,
195+ 'api_key = "opencodex-loopback"' ,
196+ "" ,
197+ "[model.ocx-retired]" ,
198+ 'model = "retired/model"' ,
199+ 'base_url = "http://127.0.0.1:10100/v1"' ,
200+ 'api_key = "opencodex-loopback"' ,
201+ "" ,
202+ ] . join ( "\n" ) ) ;
203+
204+ const result = injectGrokConfig ( 10100 , MODELS , {
205+ grokHome,
206+ catalogModelIds : new Set ( [ "gpt-5.6-sol" , "hidden/model" ] ) ,
207+ } ) ;
208+ expect ( result ) . toMatchObject ( { ok : true , changed : true } ) ;
209+ const content = readFileSync ( configPath , "utf8" ) ;
210+ expect ( content ) . not . toContain ( "[model.ocx-hidden]" ) ;
211+ expect ( content ) . not . toContain ( 'model = "hidden/model"' ) ;
212+ expect ( content ) . toContain ( "[model.ocx-retired]" ) ;
213+ expect ( content ) . toContain ( 'model = "retired/model"' ) ;
214+ } ) ;
215+
216+ test ( "adopts a current orphan whose TOML model id uses literal quotes" , ( ) => {
217+ writeOrphanedConfig ( ) ;
218+ writeFileSync (
219+ configPath ,
220+ readFileSync ( configPath , "utf8" ) . replace ( 'model = "gpt-5.6-sol"' , "model = 'gpt-5.6-sol'" ) ,
221+ ) ;
222+
223+ const result = injectGrokConfig ( 10100 , MODELS , { grokHome } ) ;
224+ expect ( result ) . toMatchObject ( { ok : true , changed : true } ) ;
225+ const content = readFileSync ( configPath , "utf8" ) ;
226+ expect ( modelTables ( content ) ) . toEqual ( [ "ocx-gpt-5-6-sol" ] ) ;
227+ expect ( content ) . not . toContain ( "model = 'gpt-5.6-sol'" ) ;
228+ expect ( content ) . toContain ( 'model = "gpt-5.6-sol"' ) ;
229+ } ) ;
230+
231+ test ( "teardown removes a preserved retired orphan and restores user bytes exactly" , ( ) => {
232+ for ( const eol of [ "\n" , "\r\n" ] ) {
233+ const userPrefix = [ `theme = "${ eol === "\n" ? "lf" : "crlf" } "` , "" , "" ] . join ( eol ) ;
234+ const orphan = [
235+ "[model.ocx-retired]" ,
236+ 'model = "retired/model"' ,
237+ 'base_url = "http://127.0.0.1:10100/v1"' ,
238+ 'api_key = "opencodex-loopback"' ,
239+ "" ,
240+ ] . join ( eol ) ;
241+ writeFileSync ( configPath , userPrefix + orphan ) ;
242+ expect ( injectGrokConfig ( 10100 , MODELS , { grokHome } ) )
243+ . toMatchObject ( { ok : true , changed : true } ) ;
244+ expect ( readFileSync ( configPath , "utf8" ) ) . toContain ( "[model.ocx-retired]" ) ;
245+
246+ const stripped = stripGrokConfig ( { grokHome } ) ;
247+ expect ( stripped ) . toMatchObject ( { ok : true , changed : true } ) ;
248+ expect ( readFileSync ( configPath , "utf8" ) ) . toBe ( userPrefix ) ;
249+ }
250+ } ) ;
251+
252+ test ( "teardown preserves a comment-only tail beyond the fence byte-for-byte" , ( ) => {
253+ for ( const eol of [ "\n" , "\r\n" ] ) {
254+ const userPrefix = [ `theme = "${ eol === "\n" ? "lf" : "crlf" } "` , "" , "" ] . join ( eol ) ;
255+ const orphan = [
256+ "[model.ocx-retired]" ,
257+ 'model = "retired/model"' ,
258+ 'base_url = "http://127.0.0.1:10100/v1"' ,
259+ 'api_key = "opencodex-loopback"' ,
260+ "" ,
261+ ] . join ( eol ) ;
262+ const tail = [ "# keep this post-fence note" , "bare_user_key = true" , "" ] . join ( eol ) ;
263+ writeFileSync ( configPath , userPrefix + orphan ) ;
264+ expect ( injectGrokConfig ( 10100 , MODELS , { grokHome } ) )
265+ . toMatchObject ( { ok : true , changed : true } ) ;
266+ writeFileSync ( configPath , readFileSync ( configPath , "utf8" ) + tail ) ;
267+
268+ expect ( stripGrokConfig ( { grokHome } ) ) . toMatchObject ( { ok : true , changed : true } ) ;
269+ expect ( readFileSync ( configPath , "utf8" ) ) . toBe ( userPrefix + tail ) ;
270+ }
271+ } ) ;
272+
273+ test ( "markerless teardown removes only ownership-proven orphan tables" , ( ) => {
274+ const owned = [
275+ "[model.ocx-retired]" ,
276+ 'model = "retired/model"' ,
277+ 'base_url = "http://127.0.0.1:10100/v1"' ,
278+ 'api_key = "opencodex-loopback"' ,
279+ "" ,
280+ ] . join ( "\n" ) ;
281+ const userOwned = [
282+ "[model.user-owned]" ,
283+ 'model = "user/model"' ,
284+ 'base_url = "http://127.0.0.1:10100/v1"' ,
285+ 'api_key = "not-ours"' ,
286+ "" ,
287+ ] . join ( "\n" ) ;
288+ writeFileSync ( configPath , owned + userOwned ) ;
289+
290+ const stripped = stripGrokConfig ( { grokHome } ) ;
291+ expect ( stripped ) . toMatchObject ( { ok : true , changed : true } ) ;
292+ expect ( readFileSync ( configPath , "utf8" ) ) . toBe ( userOwned ) ;
293+ } ) ;
294+
189295 test ( "still removes a catalog orphan when that model is excluded" , ( ) => {
190296 writeOrphanedConfig ( ) ;
191297
0 commit comments