Skip to content

Commit 727a4a6

Browse files
authored
perf(test): reuse Skill Workshop module graphs (#123772)
1 parent 07adc74 commit 727a4a6

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

‎src/cli/skills-cli.workshop-cache.test.ts‎

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Command } from "commander";
2-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
33
import {
44
createOpenClawTestState,
55
type OpenClawTestState,
@@ -8,6 +8,10 @@ import { createTrackedTempDirs } from "../test-utils/tracked-temp-dirs.js";
88

99
const tempDirs = createTrackedTempDirs();
1010
let testState: OpenClawTestState;
11+
let gatewaySnapshots: typeof import("../skills/runtime/session-snapshot.js");
12+
let gatewayRefreshState: typeof import("../skills/runtime/refresh-state.js");
13+
let gatewayWorkshop: typeof import("../skills/workshop/service.js");
14+
let registerSkillsCli: (typeof import("./skills-cli.js"))["registerSkillsCli"];
1115

1216
const mocks = vi.hoisted(() => ({
1317
callGateway: vi.fn(),
@@ -50,6 +54,21 @@ vi.mock("../agents/agent-scope.js", () => ({
5054
}));
5155

5256
describe("skills workshop CLI gateway snapshot invalidation", () => {
57+
beforeAll(async () => {
58+
// Keep one module graph for each process role. Reusing the graphs preserves
59+
// the Gateway/CLI cache boundary without rebuilding the full CLI per test.
60+
vi.resetModules();
61+
gatewaySnapshots = await import("../skills/runtime/session-snapshot.js");
62+
gatewayRefreshState = await import("../skills/runtime/refresh-state.js");
63+
gatewayWorkshop = await import("../skills/workshop/service.js");
64+
vi.resetModules();
65+
({ registerSkillsCli } = await import("./skills-cli.js"));
66+
});
67+
68+
afterAll(() => {
69+
vi.resetModules();
70+
});
71+
5372
beforeEach(async () => {
5473
testState = await createOpenClawTestState({
5574
layout: "state-only",
@@ -71,20 +90,15 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
7190
}
7291
return await mocks.gatewayApply(request);
7392
});
74-
vi.resetModules();
7593
});
7694

7795
afterEach(async () => {
7896
await testState.cleanup();
7997
await tempDirs.cleanup();
80-
vi.resetModules();
8198
});
8299

83100
it("applies through the gateway process that owns the cached session skill index", async () => {
84101
// This first module graph stands in for the long-running Gateway process.
85-
const gatewaySnapshots = await import("../skills/runtime/session-snapshot.js");
86-
const gatewayRefreshState = await import("../skills/runtime/refresh-state.js");
87-
const gatewayWorkshop = await import("../skills/workshop/service.js");
88102
const proposal = await gatewayWorkshop.proposeCreateSkill({
89103
workspaceDir: mocks.workspaceDir,
90104
name: "Gateway Visible",
@@ -113,8 +127,6 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
113127

114128
// A fresh module graph models the short-lived CLI process. Direct application
115129
// here would bump only the CLI's refresh-state map, leaving the Gateway stale.
116-
vi.resetModules();
117-
const { registerSkillsCli } = await import("./skills-cli.js");
118130
const program = new Command();
119131
program.exitOverride();
120132
registerSkillsCli(program);
@@ -142,8 +154,7 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
142154
});
143155

144156
it("does not replay a dispatched gateway apply failure in the CLI process", async () => {
145-
const workshop = await import("../skills/workshop/service.js");
146-
const proposal = await workshop.proposeCreateSkill({
157+
const proposal = await gatewayWorkshop.proposeCreateSkill({
147158
workspaceDir: mocks.workspaceDir,
148159
name: "Single Dispatch",
149160
description: "Apply only in the process that owns snapshot state",
@@ -153,8 +164,6 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
153164
throw new Error("gateway apply failed");
154165
};
155166

156-
vi.resetModules();
157-
const { registerSkillsCli } = await import("./skills-cli.js");
158167
const program = new Command();
159168
program.exitOverride();
160169
registerSkillsCli(program);
@@ -166,14 +175,13 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
166175
"health",
167176
"skills.proposals.apply",
168177
]);
169-
await expect(workshop.inspectSkillProposal(proposal.record.id)).resolves.toMatchObject({
178+
await expect(gatewayWorkshop.inspectSkillProposal(proposal.record.id)).resolves.toMatchObject({
170179
record: { status: "pending" },
171180
});
172181
});
173182

174183
it("preserves configless offline apply when no local gateway is listening", async () => {
175-
const workshop = await import("../skills/workshop/service.js");
176-
const proposal = await workshop.proposeCreateSkill({
184+
const proposal = await gatewayWorkshop.proposeCreateSkill({
177185
workspaceDir: mocks.workspaceDir,
178186
name: "Offline Upgrade",
179187
description: "Keep shipped configless Workshop apply behavior",
@@ -186,8 +194,6 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
186194
});
187195
mocks.callGateway.mockRejectedValueOnce(authError);
188196

189-
vi.resetModules();
190-
const { registerSkillsCli } = await import("./skills-cli.js");
191197
const program = new Command();
192198
program.exitOverride();
193199
registerSkillsCli(program);
@@ -203,14 +209,13 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
203209
timeoutMs: 250,
204210
});
205211
expect(mocks.releaseGatewayLock).toHaveBeenCalledTimes(1);
206-
await expect(workshop.inspectSkillProposal(proposal.record.id)).resolves.toMatchObject({
212+
await expect(gatewayWorkshop.inspectSkillProposal(proposal.record.id)).resolves.toMatchObject({
207213
record: { status: "applied" },
208214
});
209215
});
210216

211217
it("does not bypass Gateway ownership when CLI credentials are missing", async () => {
212-
const workshop = await import("../skills/workshop/service.js");
213-
const proposal = await workshop.proposeCreateSkill({
218+
const proposal = await gatewayWorkshop.proposeCreateSkill({
214219
workspaceDir: mocks.workspaceDir,
215220
name: "Gateway Owned Upgrade",
216221
description: "Keep snapshot invalidation in the running gateway",
@@ -224,8 +229,6 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
224229
mocks.callGateway.mockRejectedValueOnce(authError);
225230
mocks.acquireGatewayLock.mockRejectedValueOnce(new Error("gateway lock is owned"));
226231

227-
vi.resetModules();
228-
const { registerSkillsCli } = await import("./skills-cli.js");
229232
const program = new Command();
230233
program.exitOverride();
231234
registerSkillsCli(program);
@@ -236,7 +239,7 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
236239
expect(mocks.callGateway).toHaveBeenCalledTimes(1);
237240
expect(mocks.acquireGatewayLock).toHaveBeenCalledTimes(1);
238241
expect(mocks.releaseGatewayLock).not.toHaveBeenCalled();
239-
await expect(workshop.inspectSkillProposal(proposal.record.id)).resolves.toMatchObject({
242+
await expect(gatewayWorkshop.inspectSkillProposal(proposal.record.id)).resolves.toMatchObject({
240243
record: { status: "pending" },
241244
});
242245
});
@@ -281,8 +284,6 @@ describe("skills workshop CLI gateway snapshot invalidation", () => {
281284
};
282285
};
283286

284-
vi.resetModules();
285-
const { registerSkillsCli } = await import("./skills-cli.js");
286287
const program = new Command();
287288
program.exitOverride();
288289
registerSkillsCli(program);

0 commit comments

Comments
 (0)