11import { describe , it , expect } from 'vitest' ;
2- import { writeSessionColor } from './sessionColor' ;
2+ import { writeSessionColor , patchSaveAgentColor } from './sessionColor' ;
3+
4+ const makeSaveAgentColor = ( ) =>
5+ ';async function Mr$(H,$,q){let K=q??sT(H);' +
6+ 'if(Hv(K,{type:"agent-color",agentColor:$,sessionId:H}),H===V$())' +
7+ 'WA().currentSessionAgentColor=$;c("tengu_agent_color_set",{})}' ;
38
49const makeCLIState = ( ) =>
510 'effortValue:oR(w.effort),' +
@@ -12,37 +17,41 @@ const makeDefaultState = () =>
1217const makeBoth = ( ) =>
1318 'first{' + makeDefaultState ( ) + 'second{' + makeCLIState ( ) ;
1419
20+ const makeFullFile = ( ) => makeBoth ( ) + makeSaveAgentColor ( ) ;
21+
1522describe ( 'sessionColor' , ( ) => {
1623 describe ( 'writeSessionColor' , ( ) => {
17- it ( 'should inject into CLI initialState' , ( ) => {
18- const result = writeSessionColor ( makeCLIState ( ) ) ;
24+ it ( 'should inject into CLI initialState and patch saveAgentColor ' , ( ) => {
25+ const result = writeSessionColor ( makeFullFile ( ) ) ;
1926 expect ( result ) . not . toBeNull ( ) ;
2027 expect ( result ) . toContain ( 'TWEAKCC_SESSION_COLOR' ) ;
2128 expect ( result ) . toContain ( '{name:"",color:__c}' ) ;
29+ expect ( result ) . toContain ( '__tweakccSaveAgentColor' ) ;
2230 } ) ;
2331
2432 it ( 'should inject into default app state' , ( ) => {
25- const result = writeSessionColor ( makeDefaultState ( ) ) ;
33+ const input = makeDefaultState ( ) + makeSaveAgentColor ( ) ;
34+ const result = writeSessionColor ( input ) ;
2635 expect ( result ) . not . toBeNull ( ) ;
2736 expect ( result ) . toContain ( 'TWEAKCC_SESSION_COLOR' ) ;
2837 } ) ;
2938
30- it ( 'should patch both locations when both present ' , ( ) => {
31- const result = writeSessionColor ( makeBoth ( ) ) ! ;
39+ it ( 'should patch both initialState locations ' , ( ) => {
40+ const result = writeSessionColor ( makeFullFile ( ) ) ! ;
3241 expect ( result ) . not . toBeNull ( ) ;
3342 const count = ( result . match ( / T W E A K C C _ S E S S I O N _ C O L O R / g) || [ ] ) . length ;
3443 expect ( count ) . toBe ( 2 ) ;
3544 } ) ;
3645
3746 it ( 'should validate color against allowed list' , ( ) => {
38- const result = writeSessionColor ( makeCLIState ( ) ) ! ;
47+ const result = writeSessionColor ( makeFullFile ( ) ) ! ;
3948 expect ( result ) . toContain ( '.includes(__c)' ) ;
4049 expect ( result ) . toContain ( '"green"' ) ;
4150 expect ( result ) . toContain ( '"cyan"' ) ;
4251 } ) ;
4352
4453 it ( 'should be idempotent' , ( ) => {
45- const first = writeSessionColor ( makeBoth ( ) ) ! ;
54+ const first = writeSessionColor ( makeFullFile ( ) ) ! ;
4655 const second = writeSessionColor ( first ) ! ;
4756 expect ( second ) . toBe ( first ) ;
4857 } ) ;
@@ -51,5 +60,37 @@ describe('sessionColor', () => {
5160 const result = writeSessionColor ( 'not a valid file' ) ;
5261 expect ( result ) . toBeNull ( ) ;
5362 } ) ;
63+
64+ it ( 'should schedule color save via queueMicrotask' , ( ) => {
65+ const result = writeSessionColor ( makeFullFile ( ) ) ! ;
66+ expect ( result ) . toContain ( 'queueMicrotask' ) ;
67+ expect ( result ) . toContain ( '__tweakccSaveAgentColor' ) ;
68+ } ) ;
69+
70+ it ( 'should expose saveAgentColor on globalThis' , ( ) => {
71+ const result = writeSessionColor ( makeFullFile ( ) ) ! ;
72+ expect ( result ) . toContain (
73+ 'globalThis.__tweakccSaveAgentColor=(c)=>Mr$(V$(),c)'
74+ ) ;
75+ } ) ;
76+ } ) ;
77+
78+ describe ( 'patchSaveAgentColor' , ( ) => {
79+ it ( 'should find and patch saveAgentColor' , ( ) => {
80+ const result = patchSaveAgentColor ( makeSaveAgentColor ( ) ) ;
81+ expect ( result ) . not . toBeNull ( ) ;
82+ expect ( result ) . toContain ( 'globalThis.__tweakccSaveAgentColor' ) ;
83+ } ) ;
84+
85+ it ( 'should return null when pattern not found' , ( ) => {
86+ const result = patchSaveAgentColor ( 'no match here' ) ;
87+ expect ( result ) . toBeNull ( ) ;
88+ } ) ;
89+
90+ it ( 'should preserve original function' , ( ) => {
91+ const result = patchSaveAgentColor ( makeSaveAgentColor ( ) ) ! ;
92+ expect ( result ) . toContain ( 'async function Mr$' ) ;
93+ expect ( result ) . toContain ( 'type:"agent-color"' ) ;
94+ } ) ;
5495 } ) ;
5596} ) ;
0 commit comments