Skip to content

Commit 162fd4f

Browse files
committed
fix: keep prepared-model tests inside owned state
1 parent cfddfc2 commit 162fd4f

12 files changed

Lines changed: 1640 additions & 1199 deletions
Lines changed: 118 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// Preserve module setup before modules that consume it.
22
// oxfmt-ignore
33
import {
4+
cleanupPreparedModelRuntimeHarness,
45
getPreparedModelRuntimeMocks,
56
resetPreparedModelRuntimeHarness,
67
} from "./prepared-model-runtime.test-harness.js";
7-
import { beforeEach, describe, expect, it, vi } from "vitest";
8+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
89
import { createDeferred } from "../../test/helpers/promise.js";
10+
import {
11+
createOpenClawTestState,
12+
type OpenClawTestState,
13+
} from "../test-utils/openclaw-test-state.js";
914
import { resolvePublishedModelCatalogOwner } from "./prepared-model-catalog-owner.js";
1015
import {
1116
getPreparedModelRuntimeAuthMaterializations,
@@ -21,10 +26,12 @@ import {
2126
} from "./prepared-model-runtime.js";
2227

2328
const mocks = getPreparedModelRuntimeMocks();
29+
let state: OpenClawTestState;
2430

2531
describe("prepared model runtime config stamps", () => {
26-
beforeEach(() => {
27-
resetPreparedModelRuntimeHarness();
32+
beforeEach(async () => {
33+
state = await createOpenClawTestState({ label: "prepared-model-runtime" });
34+
resetPreparedModelRuntimeHarness(state);
2835
mocks.configuredAgentIds = ["default"];
2936
});
3037

@@ -34,8 +41,8 @@ describe("prepared model runtime config stamps", () => {
3441
await refreshPreparedModelRuntimeSnapshots(initialConfig, { gatewayLifecycle: true });
3542
const input = {
3643
agentId: "default",
37-
agentDir: "/tmp/unused-agent",
38-
inheritedAuthDir: "/tmp/unused-agent",
44+
agentDir: state.agentDir("default"),
45+
inheritedAuthDir: state.agentDir("default"),
3946
config: initialConfig,
4047
};
4148
const existingReader = await prepareModelRuntimeSnapshot(input);
@@ -92,8 +99,8 @@ describe("prepared model runtime config stamps", () => {
9299
await expect(
93100
prepareModelRuntimeSnapshot({
94101
agentId: "default",
95-
agentDir: "/tmp/unused-agent",
96-
inheritedAuthDir: "/tmp/unused-agent",
102+
agentDir: state.agentDir("default"),
103+
inheritedAuthDir: state.agentDir("default"),
97104
config: nextConfig,
98105
}),
99106
).resolves.toMatchObject({ config: nextConfig });
@@ -106,26 +113,33 @@ describe("prepared model runtime config stamps", () => {
106113
const staleConfig = {};
107114
const nextConfig = { gateway: { reload: { mode: "hot" as const } } };
108115
const supplierReady = createDeferred();
109-
const stalePublication = refreshPreparedModelRuntimeSnapshots(async () => {
110-
await supplierReady.promise;
111-
return staleConfig;
112-
});
113-
const nextPublication = refreshPreparedModelRuntimeSnapshots(nextConfig, {
114-
gatewayLifecycle: true,
115-
});
116-
117-
supplierReady.resolve();
118-
await Promise.all([stalePublication, nextPublication]);
119-
120-
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce();
121-
await expect(
122-
prepareModelRuntimeSnapshot({
123-
agentId: "default",
124-
agentDir: "/tmp/unused-agent",
125-
inheritedAuthDir: "/tmp/unused-agent",
126-
config: nextConfig,
127-
}),
128-
).resolves.toMatchObject({ config: nextConfig });
116+
let stalePublication: ReturnType<typeof refreshPreparedModelRuntimeSnapshots> | undefined;
117+
let nextPublication: ReturnType<typeof refreshPreparedModelRuntimeSnapshots> | undefined;
118+
try {
119+
stalePublication = refreshPreparedModelRuntimeSnapshots(async () => {
120+
await supplierReady.promise;
121+
return staleConfig;
122+
});
123+
nextPublication = refreshPreparedModelRuntimeSnapshots(nextConfig, {
124+
gatewayLifecycle: true,
125+
});
126+
127+
supplierReady.resolve();
128+
await Promise.all([stalePublication, nextPublication]);
129+
130+
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledOnce();
131+
await expect(
132+
prepareModelRuntimeSnapshot({
133+
agentId: "default",
134+
agentDir: state.agentDir("default"),
135+
inheritedAuthDir: state.agentDir("default"),
136+
config: nextConfig,
137+
}),
138+
).resolves.toMatchObject({ config: nextConfig });
139+
} finally {
140+
supplierReady.resolve();
141+
await Promise.allSettled([stalePublication, nextPublication]);
142+
}
129143
});
130144

131145
it("drops a publication whose lifecycle claim is lost during async config resolution", async () => {
@@ -136,76 +150,92 @@ describe("prepared model runtime config stamps", () => {
136150
const supplierStarted = createDeferred();
137151
const releaseSupplier = createDeferred();
138152
let claimCurrent = true;
139-
const stalePublication = refreshPreparedModelRuntimeSnapshots(
140-
async () => {
141-
supplierStarted.resolve();
142-
await releaseSupplier.promise;
143-
return staleConfig;
144-
},
145-
{
146-
gatewayLifecycle: true,
147-
isPublicationCurrent: () => claimCurrent,
148-
},
149-
);
150-
151-
await supplierStarted.promise;
152-
const readerSettled = vi.fn();
153-
const reader = prepareModelRuntimeSnapshot({
154-
agentId: "default",
155-
agentDir: "/tmp/unused-agent",
156-
config: staleConfig,
157-
}).then(readerSettled, readerSettled);
158-
claimCurrent = false;
159-
releaseSupplier.resolve();
160-
await stalePublication;
161-
// Losing the external claim must settle readers even if no replacement is scheduled.
162-
await vi.waitFor(() => expect(readerSettled).toHaveBeenCalledWith(expect.any(Error)));
163-
await reader;
164-
await refreshPreparedModelRuntimeSnapshots(nextConfig, { gatewayLifecycle: true });
165-
166-
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
167-
await expect(
168-
prepareModelRuntimeSnapshot({
153+
let stalePublication: ReturnType<typeof refreshPreparedModelRuntimeSnapshots> | undefined;
154+
try {
155+
stalePublication = refreshPreparedModelRuntimeSnapshots(
156+
async () => {
157+
supplierStarted.resolve();
158+
await releaseSupplier.promise;
159+
return staleConfig;
160+
},
161+
{
162+
gatewayLifecycle: true,
163+
isPublicationCurrent: () => claimCurrent,
164+
},
165+
);
166+
167+
await supplierStarted.promise;
168+
const readerSettled = vi.fn();
169+
const reader = prepareModelRuntimeSnapshot({
169170
agentId: "default",
170-
agentDir: "/tmp/unused-agent",
171-
inheritedAuthDir: "/tmp/unused-agent",
172-
config: nextConfig,
173-
}),
174-
).resolves.toMatchObject({ config: nextConfig });
175-
await expect(
176-
loadPublishedGatewayReplyDispatchRuntime({ agentId: "default" }),
177-
).resolves.toMatchObject({ config: nextConfig });
171+
agentDir: state.agentDir("default"),
172+
config: staleConfig,
173+
}).then(readerSettled, readerSettled);
174+
claimCurrent = false;
175+
releaseSupplier.resolve();
176+
await stalePublication;
177+
// Losing the external claim must settle readers even if no replacement is scheduled.
178+
await vi.waitFor(() => expect(readerSettled).toHaveBeenCalledWith(expect.any(Error)));
179+
await reader;
180+
await refreshPreparedModelRuntimeSnapshots(nextConfig, { gatewayLifecycle: true });
181+
182+
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
183+
await expect(
184+
prepareModelRuntimeSnapshot({
185+
agentId: "default",
186+
agentDir: state.agentDir("default"),
187+
inheritedAuthDir: state.agentDir("default"),
188+
config: nextConfig,
189+
}),
190+
).resolves.toMatchObject({ config: nextConfig });
191+
await expect(
192+
loadPublishedGatewayReplyDispatchRuntime({ agentId: "default" }),
193+
).resolves.toMatchObject({ config: nextConfig });
194+
} finally {
195+
claimCurrent = false;
196+
releaseSupplier.resolve();
197+
await Promise.allSettled([stalePublication]);
198+
}
178199
});
179200

180201
it("keeps an in-flight auth publication on the advanced stamp", async () => {
181202
const initialConfig = {};
182203
const nextConfig = { gateway: { reload: { mode: "hot" as const } } };
183204
await refreshPreparedModelRuntimeSnapshots(initialConfig, { gatewayLifecycle: true });
205+
const finishAuthRefreshGate = createDeferred();
184206
let finishAuthRefresh: (() => void) | undefined;
185-
mocks.ensureOpenClawModelsJson.mockImplementationOnce(
186-
async () =>
187-
await new Promise<{ agentDir: string; wrote: false }>((resolve) => {
188-
finishAuthRefresh = () => resolve({ agentDir: "/tmp/unused-agent", wrote: false });
189-
}),
190-
);
207+
mocks.ensureOpenClawModelsJson.mockImplementationOnce(async (_config, agentDir) => {
208+
finishAuthRefresh = () => finishAuthRefreshGate.resolve();
209+
await finishAuthRefreshGate.promise;
210+
return { agentDir: String(agentDir), wrote: false };
211+
});
191212

192-
mocks.mutationListener?.({ affectsInheritedStores: true });
193-
await vi.waitFor(() => expect(finishAuthRefresh).toBeDefined());
194-
mocks.configuredAgentDirs.set("default", "/tmp/later-agent");
195-
advancePreparedModelRuntimeConfig(nextConfig);
196-
finishAuthRefresh?.();
213+
try {
214+
mocks.mutationListener?.({ affectsInheritedStores: true });
215+
await vi.waitFor(() => expect(finishAuthRefresh).toBeDefined());
216+
mocks.configuredAgentDirs.set("default", "/tmp/later-agent");
217+
advancePreparedModelRuntimeConfig(nextConfig);
218+
finishAuthRefreshGate.resolve();
197219

198-
const snapshot = await prepareModelRuntimeSnapshot({
199-
agentId: "default",
200-
agentDir: "/tmp/unused-agent",
201-
inheritedAuthDir: "/tmp/unused-agent",
202-
config: nextConfig,
203-
});
204-
expect(resolvePublishedModelCatalogOwner(snapshot)).toMatchObject({
205-
agentId: "default",
206-
workspaceDir: "/tmp/unused-workspace",
207-
config: nextConfig,
208-
});
209-
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
220+
const snapshot = await prepareModelRuntimeSnapshot({
221+
agentId: "default",
222+
agentDir: state.agentDir("default"),
223+
inheritedAuthDir: state.agentDir("default"),
224+
config: nextConfig,
225+
});
226+
expect(resolvePublishedModelCatalogOwner(snapshot)).toMatchObject({
227+
agentId: "default",
228+
workspaceDir: "/tmp/unused-workspace",
229+
config: nextConfig,
230+
});
231+
expect(mocks.ensureOpenClawModelsJson).toHaveBeenCalledTimes(2);
232+
} finally {
233+
finishAuthRefreshGate.resolve();
234+
await Promise.allSettled([loadPublishedGatewayReplyDispatchRuntime({ agentId: "default" })]);
235+
}
210236
});
211237
});
238+
239+
afterEach(async ({ task }) => {
240+
await cleanupPreparedModelRuntimeHarness(state, task.result?.state === "fail");
241+
});

0 commit comments

Comments
 (0)