Skip to content

Commit 365ef7c

Browse files
author
Peter Steinberger
committed
test(tui): share gateway mock model
1 parent 7168e9e commit 365ef7c

1 file changed

Lines changed: 41 additions & 18 deletions

File tree

‎src/tui/tui-pty-local.e2e.test.ts‎

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,18 @@ type MockModelRequest = {
4545
type GatewayScenario = MockModelBehavior & {
4646
agentId: string;
4747
modelId: string;
48+
routeMarker: string;
4849
toolsProfile: "minimal" | "coding";
4950
};
5051

5152
const SHARED_GATEWAY_AGENT_ID = "tui-pty-gateway";
53+
const SHARED_GATEWAY_MODEL_ID = "tui-pty-gateway";
5254

5355
const GATEWAY_SCENARIOS = {
5456
validation: {
5557
agentId: "tui-pty-validation",
5658
modelId: "tui-pty-validation",
59+
routeMarker: "trigger malformed edit calls",
5760
toolsProfile: "coding",
5861
replyText: "FIRST_RUN_ACTIVE",
5962
holdFirstResponse: false,
@@ -63,6 +66,7 @@ const GATEWAY_SCENARIOS = {
6366
crossClient: {
6467
agentId: SHARED_GATEWAY_AGENT_ID,
6568
modelId: "tui-pty-cross-client",
69+
routeMarker: "seed cross-client session",
6670
toolsProfile: "minimal",
6771
replyText: "FIRST_RUN_ACTIVE",
6872
holdFirstResponse: false,
@@ -71,6 +75,7 @@ const GATEWAY_SCENARIOS = {
7175
followup: {
7276
agentId: SHARED_GATEWAY_AGENT_ID,
7377
modelId: "tui-pty-followup",
78+
routeMarker: "slow first turn",
7479
toolsProfile: "minimal",
7580
replyText: "FIRST_RUN_ACTIVE",
7681
holdFirstResponse: true,
@@ -79,6 +84,7 @@ const GATEWAY_SCENARIOS = {
7984
emptyReply: {
8085
agentId: SHARED_GATEWAY_AGENT_ID,
8186
modelId: "tui-pty-empty-reply",
87+
routeMarker: "non-deliverable first turn",
8288
toolsProfile: "minimal",
8389
replyText: "[[reply_to_current]]",
8490
holdFirstResponse: false,
@@ -87,6 +93,7 @@ const GATEWAY_SCENARIOS = {
8793
cancel: {
8894
agentId: SHARED_GATEWAY_AGENT_ID,
8995
modelId: "tui-pty-cancel",
96+
routeMarker: "slow turn to abort",
9097
toolsProfile: "minimal",
9198
replyText: "FIRST_RUN_ACTIVE",
9299
holdFirstResponse: true,
@@ -95,6 +102,7 @@ const GATEWAY_SCENARIOS = {
95102
collect: {
96103
agentId: SHARED_GATEWAY_AGENT_ID,
97104
modelId: "tui-pty-collect",
105+
routeMarker: "slow collect parent",
98106
toolsProfile: "minimal",
99107
replyText: "FIRST_RUN_ACTIVE",
100108
holdFirstResponse: true,
@@ -103,6 +111,7 @@ const GATEWAY_SCENARIOS = {
103111
reconnect: {
104112
agentId: SHARED_GATEWAY_AGENT_ID,
105113
modelId: "tui-pty-reconnect",
114+
routeMarker: "send preserved draft after restart",
106115
toolsProfile: "minimal",
107116
replyText: "RECONNECTED_RUN_COMPLETE",
108117
},
@@ -280,6 +289,10 @@ async function readJsonRequest(req: IncomingMessage): Promise<Record<string, unk
280289

281290
async function startRoutedMockModelServer(
282291
behaviors: Readonly<Record<string, MockModelBehavior>>,
292+
opts: {
293+
advertisedModelIds?: string[];
294+
resolveBehaviorId?: (body: Record<string, unknown>, modelId: string) => string;
295+
} = {},
283296
): Promise<MockModelServer> {
284297
const requests: MockModelRequest[] = [];
285298
const requestsByModel = new Map<string, MockModelRequest[]>();
@@ -297,22 +310,26 @@ async function startRoutedMockModelServer(
297310
}
298311
if (req.method === "GET" && url.pathname === "/v1/models") {
299312
writeJson(res, 200, {
300-
data: Object.keys(behaviors).map((id) => ({ id, object: "model" })),
313+
data: (opts.advertisedModelIds ?? Object.keys(behaviors)).map((id) => ({
314+
id,
315+
object: "model",
316+
})),
301317
});
302318
return;
303319
}
304320
if (req.method === "POST") {
305321
const body = await readJsonRequest(req);
306322
if (url.pathname === "/v1/responses" || url.pathname === "/responses") {
307323
const modelId = typeof body.model === "string" ? body.model : "";
308-
const behavior = behaviors[modelId];
324+
const behaviorId = opts.resolveBehaviorId?.(body, modelId) ?? modelId;
325+
const behavior = behaviors[behaviorId];
309326
if (!behavior) {
310-
writeJson(res, 400, { error: `unknown mock model: ${modelId || "missing"}` });
327+
writeJson(res, 400, { error: `unknown mock behavior: ${behaviorId || "missing"}` });
311328
return;
312329
}
313-
const modelRequests = requestsByModel.get(modelId) ?? [];
314-
if (!requestsByModel.has(modelId)) {
315-
requestsByModel.set(modelId, modelRequests);
330+
const modelRequests = requestsByModel.get(behaviorId) ?? [];
331+
if (!requestsByModel.has(behaviorId)) {
332+
requestsByModel.set(behaviorId, modelRequests);
316333
}
317334
const requestIndex = modelRequests.length;
318335
const request = { method: req.method, path: url.pathname, body };
@@ -327,7 +344,7 @@ async function startRoutedMockModelServer(
327344
requestIndex === 0
328345
? behavior.replyText
329346
: (behavior.followupReplyText ?? behavior.replyText),
330-
requestIndex === 0 ? firstResponseGates.get(modelId)?.promise : undefined,
347+
requestIndex === 0 ? firstResponseGates.get(behaviorId)?.promise : undefined,
331348
);
332349
return;
333350
}
@@ -597,8 +614,7 @@ function buildGatewayModeConfig(params: { tempDir: string; providerBaseUrl: stri
597614
...new Map(scenarios.map((scenario) => [scenario.agentId, scenario])).values(),
598615
];
599616
const defaultScenario = GATEWAY_SCENARIOS.validation;
600-
const defaultModelRef = `tui-pty-mock/${defaultScenario.modelId}`;
601-
const modelRefs = scenarios.map((scenario) => `tui-pty-mock/${scenario.modelId}`);
617+
const defaultModelRef = `tui-pty-mock/${SHARED_GATEWAY_MODEL_ID}`;
602618
const base = buildLocalModeConfig({
603619
workspaceDir: path.join(params.tempDir, defaultScenario.agentId),
604620
providerBaseUrl: params.providerBaseUrl,
@@ -609,9 +625,7 @@ function buildGatewayModeConfig(params: { tempDir: string; providerBaseUrl: stri
609625
defaults: {
610626
workspace: path.join(params.tempDir, defaultScenario.agentId),
611627
model: { primary: defaultModelRef },
612-
models: Object.fromEntries(
613-
modelRefs.map((modelRef) => [modelRef, { agentRuntime: { id: "openclaw" } }]),
614-
),
628+
models: { [defaultModelRef]: { agentRuntime: { id: "openclaw" } } },
615629
skills: [],
616630
skipBootstrap: true,
617631
},
@@ -622,7 +636,7 @@ function buildGatewayModeConfig(params: { tempDir: string; providerBaseUrl: stri
622636
...(index === 0 ? { default: true } : {}),
623637
workspace: path.join(params.tempDir, scenario.agentId),
624638
skills: [],
625-
model: { primary: `tui-pty-mock/${scenario.modelId}` },
639+
model: { primary: defaultModelRef },
626640
tools: { profile: scenario.toolsProfile },
627641
},
628642
]),
@@ -631,10 +645,7 @@ function buildGatewayModeConfig(params: { tempDir: string; providerBaseUrl: stri
631645
models: {
632646
mode: "replace",
633647
providers: {
634-
"tui-pty-mock": buildMockModelProvider(
635-
params.providerBaseUrl,
636-
scenarios.map((scenario) => scenario.modelId),
637-
),
648+
"tui-pty-mock": buildMockModelProvider(params.providerBaseUrl, [SHARED_GATEWAY_MODEL_ID]),
638649
},
639650
},
640651
messages: {
@@ -645,6 +656,14 @@ function buildGatewayModeConfig(params: { tempDir: string; providerBaseUrl: stri
645656
} satisfies OpenClawConfig;
646657
}
647658

659+
function resolveGatewayBehaviorId(body: Record<string, unknown>): string {
660+
const serialized = JSON.stringify(body);
661+
const match = Object.values(GATEWAY_SCENARIOS).find((scenario) =>
662+
serialized.includes(scenario.routeMarker),
663+
);
664+
return match?.modelId ?? "";
665+
}
666+
648667
async function startSharedGatewayFixture(): Promise<SharedGatewayFixture> {
649668
const tempDir = await mkdtemp(path.join(tmpdir(), "openclaw-tui-pty-gateway-"));
650669
let mockModel: MockModelServer | undefined;
@@ -670,6 +689,10 @@ async function startSharedGatewayFixture(): Promise<SharedGatewayFixture> {
670689
},
671690
]),
672691
),
692+
{
693+
advertisedModelIds: [SHARED_GATEWAY_MODEL_ID],
694+
resolveBehaviorId: (body) => resolveGatewayBehaviorId(body),
695+
},
673696
);
674697
gateway = await createOpenClawTestInstance({
675698
name: "tui-pty-shared-gateway",
@@ -800,7 +823,7 @@ async function startGatewayModeTui(
800823
await shared.controlClient.patchSession({
801824
key: sessionKey,
802825
agentId: scenario.agentId,
803-
model: `tui-pty-mock/${scenario.modelId}`,
826+
model: `tui-pty-mock/${SHARED_GATEWAY_MODEL_ID}`,
804827
});
805828
const run = shared.run;
806829
const outputOffset = run.visibleOutput().length;

0 commit comments

Comments
 (0)