1- import fs from "node:fs/promises" ;
21import { describe , expect , test , vi } from "vitest" ;
32import { createDeferred } from "../../../test/helpers/promise.js" ;
43import {
@@ -10,7 +9,6 @@ import type { ModelCatalogEntry } from "../../agents/model-catalog.types.js";
109import { setPreparedModelRuntimeAuthStore } from "../../agents/prepared-model-runtime-auth.js" ;
1110import type { PreparedModelRuntimeSnapshot } from "../../agents/prepared-model-runtime.js" ;
1211import type { OpenClawConfig } from "../../config/types.openclaw.js" ;
13- import { withOpenClawTestState } from "../../test-utils/openclaw-test-state.js" ;
1412import { createGatewayChatMetadataRuntime } from "./chat-metadata-runtime.js" ;
1513import type { GatewayRequestContext } from "./types.js" ;
1614
@@ -226,21 +224,15 @@ describe("gateway chat metadata runtime", () => {
226224 harness . getSkillsVersion . mockClear ( ) ;
227225 harness . getPluginRegistryVersion . mockClear ( ) ;
228226
229- const first = await harness . runtime . readStartup ( {
230- agentId : "main" ,
231- includeSystem : false ,
232- } ) ;
233- const second = await harness . runtime . readStartup ( {
234- agentId : "main" ,
235- includeSystem : false ,
236- } ) ;
227+ const first = await harness . runtime . readStartup ( { agentId : "main" } ) ;
228+ const second = await harness . runtime . readStartup ( { agentId : "main" } ) ;
237229
238- expect ( second . agentsList ) . toBe ( first . agentsList ) ;
239- expect ( first . sessionModelCatalog ) . toEqual ( [
230+ expect ( first ?. sessionModelCatalog ) . toEqual ( [
240231 expect . objectContaining ( { id : "first" , provider : "test" } ) ,
241232 ] ) ;
242- expect ( first . defaultModelCatalog ) . toBe ( first . sessionModelCatalog ) ;
243- expect ( first . metadata . models ) . toEqual ( first . sessionModelCatalog ) ;
233+ expect ( second ) . toEqual ( first ) ;
234+ expect ( first ?. defaultModelCatalog ) . toBe ( first ?. sessionModelCatalog ) ;
235+ expect ( first ?. metadata . models ) . toEqual ( first ?. sessionModelCatalog ) ;
244236 expect ( harness . buildProjection ) . toHaveBeenCalledTimes ( 1 ) ;
245237 expect ( harness . getPreparedOwner ) . not . toHaveBeenCalled ( ) ;
246238 expect ( harness . getPreparedAuthStore ) . not . toHaveBeenCalled ( ) ;
@@ -262,16 +254,14 @@ describe("gateway chat metadata runtime", () => {
262254 } ) ;
263255 await harness . runtime . refresh ( ) ;
264256
265- const readNeutralStartup = async ( ) =>
266- await harness . runtime . readStartup ( {
267- agentId : defaultAgentId ,
268- includeSystem : false ,
269- } ) ;
257+ const readNeutralStartup = ( ) => harness . runtime . readStartup ( { agentId : defaultAgentId } ) ;
270258 const first = await readNeutralStartup ( ) ;
271259 const second = await readNeutralStartup ( ) ;
272260
273- expect ( first . agentsList . agents ) . toHaveLength ( agentIds . length ) ;
274- expect ( second . agentsList ) . toBe ( first . agentsList ) ;
261+ expect ( first ?. sessionModelCatalog ) . toEqual ( [
262+ expect . objectContaining ( { id : "first" , provider : "test" } ) ,
263+ ] ) ;
264+ expect ( second ) . toEqual ( first ) ;
275265 expect ( harness . buildProjection ) . toHaveBeenCalledTimes ( agentIds . length ) ;
276266
277267 await harness . runtime . readStartup ( {
@@ -280,14 +270,13 @@ describe("gateway chat metadata runtime", () => {
280270 authProfileOverride : "test:session" ,
281271 authProfileOverrideSource : "user" ,
282272 } ,
283- includeSystem : false ,
284273 } ) ;
285274 await readNeutralStartup ( ) ;
286275
287276 expect ( harness . buildProjection ) . toHaveBeenCalledTimes ( agentIds . length + 1 ) ;
288277 } ) ;
289278
290- test ( "caches a session auth projection separately from the neutral roster " , async ( ) => {
279+ test ( "caches a session auth projection separately from the neutral projection " , async ( ) => {
291280 const harness = createHarness ( ) ;
292281 await harness . runtime . refresh ( ) ;
293282
@@ -298,12 +287,10 @@ describe("gateway chat metadata runtime", () => {
298287 const first = await harness . runtime . readStartup ( {
299288 agentId : "main" ,
300289 sessionEntry,
301- includeSystem : false ,
302290 } ) ;
303291 const second = await harness . runtime . readStartup ( {
304292 agentId : "main" ,
305293 sessionEntry,
306- includeSystem : false ,
307294 } ) ;
308295
309296 expect ( second ) . toEqual ( first ) ;
@@ -337,7 +324,6 @@ describe("gateway chat metadata runtime", () => {
337324 await harness . runtime . readStartup ( {
338325 agentId : "main" ,
339326 sessionEntry,
340- includeSystem : false ,
341327 } ) ;
342328
343329 const projectionParams = harness . buildProjection . mock . calls . at ( - 1 ) ?. [ 0 ] ;
@@ -357,31 +343,6 @@ describe("gateway chat metadata runtime", () => {
357343 }
358344 } ) ;
359345
360- test ( "keeps disk-only roster rows without projecting them" , async ( ) => {
361- await withOpenClawTestState (
362- {
363- layout : "state-only" ,
364- scenario : "minimal" ,
365- agentEnv : "main" ,
366- } ,
367- async ( state ) => {
368- await fs . mkdir ( state . statePath ( "agents" , "dormant" , "agent" ) , { recursive : true } ) ;
369- const harness = createHarness ( { } ) ;
370-
371- await harness . runtime . refresh ( ) ;
372- harness . getPreparedOwner . mockClear ( ) ;
373- const startup = await harness . runtime . readStartup ( {
374- agentId : "main" ,
375- includeSystem : false ,
376- } ) ;
377-
378- expect ( startup . agentsList . agents . map ( ( agent ) => agent . id ) ) . toEqual ( [ "main" , "dormant" ] ) ;
379- expect ( harness . getPreparedOwner ) . not . toHaveBeenCalled ( ) ;
380- expect ( harness . buildProjection ) . toHaveBeenCalledTimes ( 1 ) ;
381- } ,
382- ) ;
383- } ) ;
384-
385346 test ( "reuses the prepared generation for an equivalent config replacement" , async ( ) => {
386347 const harness = createHarness ( ) ;
387348 await harness . runtime . refresh ( ) ;
@@ -570,13 +531,21 @@ describe("gateway chat metadata runtime", () => {
570531 expect ( harness . buildProjection ) . toHaveBeenCalledTimes ( 4 ) ;
571532 } ) ;
572533
573- test ( "waits for an invalidated generation to be replaced before serving reads " , async ( ) => {
534+ test ( "waits for replacement only for canonical metadata and session auth projections " , async ( ) => {
574535 const harness = createHarness ( ) ;
575536 await harness . runtime . refresh ( ) ;
576537
577538 harness . runtime . invalidate ( ) ;
578539 const read = harness . runtime . read ( { agentId : "main" } ) ;
540+ const overriddenStartup = harness . runtime . readStartup ( {
541+ agentId : "main" ,
542+ sessionEntry : {
543+ authProfileOverride : "test:session" ,
544+ authProfileOverrideSource : "user" ,
545+ } ,
546+ } ) ;
579547 let settled = false ;
548+ let overriddenSettled = false ;
580549 void read . then (
581550 ( ) => {
582551 settled = true ;
@@ -585,8 +554,18 @@ describe("gateway chat metadata runtime", () => {
585554 settled = true ;
586555 } ,
587556 ) ;
557+ void overriddenStartup . then (
558+ ( ) => {
559+ overriddenSettled = true ;
560+ } ,
561+ ( ) => {
562+ overriddenSettled = true ;
563+ } ,
564+ ) ;
565+ await expect ( harness . runtime . readStartup ( { agentId : "main" } ) ) . resolves . toBeUndefined ( ) ;
588566 await Promise . resolve ( ) ;
589567 expect ( settled ) . toBe ( false ) ;
568+ expect ( overriddenSettled ) . toBe ( false ) ;
590569
591570 const nextConfig = {
592571 agents : { list : [ { id : "main" , default : true } ] } ,
@@ -600,6 +579,13 @@ describe("gateway chat metadata runtime", () => {
600579 models : [ expect . objectContaining ( { id : "replacement" } ) ] ,
601580 swarmEnabled : true ,
602581 } ) ;
582+ await expect ( overriddenStartup ) . resolves . toMatchObject ( {
583+ metadata : {
584+ models : [ expect . objectContaining ( { id : "replacement" } ) ] ,
585+ swarmEnabled : true ,
586+ } ,
587+ sessionModelCatalog : [ expect . objectContaining ( { id : "replacement" } ) ] ,
588+ } ) ;
603589 } ) ;
604590
605591 test ( "retries a session projection invalidated while it is awaiting" , async ( ) => {
0 commit comments