mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-03 21:14:04 +00:00
refactor: share startup memory test helpers
This commit is contained in:
@@ -34,6 +34,45 @@ function createQmdManagerMock() {
|
||||
};
|
||||
}
|
||||
|
||||
async function startMemoryBackendForTest(cfg: OpenClawConfig) {
|
||||
const log = createGatewayLogMock();
|
||||
await startGatewayMemoryBackend({ cfg, log });
|
||||
return log;
|
||||
}
|
||||
|
||||
async function startQmdBackendWithManager(cfg: OpenClawConfig) {
|
||||
getMemorySearchManagerMock.mockResolvedValue({ manager: createQmdManagerMock() });
|
||||
return await startMemoryBackendForTest(cfg);
|
||||
}
|
||||
|
||||
function expectNoMemoryBackendStartup(log: ReturnType<typeof createGatewayLogMock>) {
|
||||
expect(getMemorySearchManagerMock).not.toHaveBeenCalled();
|
||||
expect(log.info).not.toHaveBeenCalled();
|
||||
expect(log.warn).not.toHaveBeenCalled();
|
||||
}
|
||||
|
||||
function expectQmdManagerRequests(cfg: OpenClawConfig, agentIds: string[]) {
|
||||
expect(getMemorySearchManagerMock).toHaveBeenCalledTimes(agentIds.length);
|
||||
for (const [index, agentId] of agentIds.entries()) {
|
||||
expect(getMemorySearchManagerMock).toHaveBeenNthCalledWith(index + 1, {
|
||||
cfg,
|
||||
agentId,
|
||||
purpose: "cli",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function expectBootSyncCompleted(
|
||||
log: ReturnType<typeof createGatewayLogMock>,
|
||||
count: number,
|
||||
agents: string,
|
||||
) {
|
||||
const noun = count === 1 ? "agent" : "agents";
|
||||
expect(log.info).toHaveBeenCalledWith(
|
||||
`qmd memory startup boot sync completed for ${count} ${noun}: ${agents}`,
|
||||
);
|
||||
}
|
||||
|
||||
describe("startGatewayMemoryBackend", () => {
|
||||
beforeEach(() => {
|
||||
getMemorySearchManagerMock.mockClear();
|
||||
@@ -44,13 +83,10 @@ describe("startGatewayMemoryBackend", () => {
|
||||
agents: { list: [{ id: "main", default: true }] },
|
||||
memory: { backend: "builtin" },
|
||||
} as OpenClawConfig;
|
||||
const log = { info: vi.fn(), warn: vi.fn() };
|
||||
|
||||
await startGatewayMemoryBackend({ cfg, log });
|
||||
const log = await startMemoryBackendForTest(cfg);
|
||||
|
||||
expect(getMemorySearchManagerMock).not.toHaveBeenCalled();
|
||||
expect(log.info).not.toHaveBeenCalled();
|
||||
expect(log.warn).not.toHaveBeenCalled();
|
||||
expectNoMemoryBackendStartup(log);
|
||||
});
|
||||
|
||||
it("keeps qmd managers lazy when startup refresh is not opted in", async () => {
|
||||
@@ -58,13 +94,10 @@ describe("startGatewayMemoryBackend", () => {
|
||||
agents: { list: [{ id: "main", default: true }] },
|
||||
memory: { backend: "qmd", qmd: {} },
|
||||
} as OpenClawConfig;
|
||||
const log = createGatewayLogMock();
|
||||
|
||||
await startGatewayMemoryBackend({ cfg, log });
|
||||
const log = await startMemoryBackendForTest(cfg);
|
||||
|
||||
expect(getMemorySearchManagerMock).not.toHaveBeenCalled();
|
||||
expect(log.info).not.toHaveBeenCalled();
|
||||
expect(log.warn).not.toHaveBeenCalled();
|
||||
expectNoMemoryBackendStartup(log);
|
||||
});
|
||||
|
||||
it("runs qmd boot sync for the default and explicitly configured agents", async () => {
|
||||
@@ -75,25 +108,11 @@ describe("startGatewayMemoryBackend", () => {
|
||||
{ id: "lazy" },
|
||||
],
|
||||
});
|
||||
const log = createGatewayLogMock();
|
||||
getMemorySearchManagerMock.mockResolvedValue({ manager: createQmdManagerMock() });
|
||||
|
||||
await startGatewayMemoryBackend({ cfg, log });
|
||||
const log = await startQmdBackendWithManager(cfg);
|
||||
|
||||
expect(getMemorySearchManagerMock).toHaveBeenCalledTimes(2);
|
||||
expect(getMemorySearchManagerMock).toHaveBeenNthCalledWith(1, {
|
||||
cfg,
|
||||
agentId: "ops",
|
||||
purpose: "cli",
|
||||
});
|
||||
expect(getMemorySearchManagerMock).toHaveBeenNthCalledWith(2, {
|
||||
cfg,
|
||||
agentId: "main",
|
||||
purpose: "cli",
|
||||
});
|
||||
expect(log.info).toHaveBeenCalledWith(
|
||||
'qmd memory startup boot sync completed for 2 agents: "ops", "main"',
|
||||
);
|
||||
expectQmdManagerRequests(cfg, ["ops", "main"]);
|
||||
expectBootSyncCompleted(log, 2, '"ops", "main"');
|
||||
expect(log.info).toHaveBeenCalledWith(
|
||||
'qmd memory startup initialization deferred for 1 agent: "lazy"',
|
||||
);
|
||||
@@ -105,25 +124,11 @@ describe("startGatewayMemoryBackend", () => {
|
||||
defaults: { memorySearch: { enabled: true } },
|
||||
list: [{ id: "ops", default: true }, { id: "main" }],
|
||||
});
|
||||
const log = createGatewayLogMock();
|
||||
getMemorySearchManagerMock.mockResolvedValue({ manager: createQmdManagerMock() });
|
||||
|
||||
await startGatewayMemoryBackend({ cfg, log });
|
||||
const log = await startQmdBackendWithManager(cfg);
|
||||
|
||||
expect(getMemorySearchManagerMock).toHaveBeenCalledTimes(2);
|
||||
expect(getMemorySearchManagerMock).toHaveBeenNthCalledWith(1, {
|
||||
cfg,
|
||||
agentId: "ops",
|
||||
purpose: "cli",
|
||||
});
|
||||
expect(getMemorySearchManagerMock).toHaveBeenNthCalledWith(2, {
|
||||
cfg,
|
||||
agentId: "main",
|
||||
purpose: "cli",
|
||||
});
|
||||
expect(log.info).toHaveBeenCalledWith(
|
||||
'qmd memory startup boot sync completed for 2 agents: "ops", "main"',
|
||||
);
|
||||
expectQmdManagerRequests(cfg, ["ops", "main"]);
|
||||
expectBootSyncCompleted(log, 2, '"ops", "main"');
|
||||
expect(log.info.mock.calls.some(([message]) => String(message).includes("deferred"))).toBe(
|
||||
false,
|
||||
);
|
||||
@@ -146,9 +151,7 @@ describe("startGatewayMemoryBackend", () => {
|
||||
expect(log.warn).toHaveBeenCalledWith(
|
||||
'qmd memory startup initialization failed for agent "main": qmd missing',
|
||||
);
|
||||
expect(log.info).toHaveBeenCalledWith(
|
||||
'qmd memory startup boot sync completed for 1 agent: "ops"',
|
||||
);
|
||||
expectBootSyncCompleted(log, 1, '"ops"');
|
||||
});
|
||||
|
||||
it("skips agents with memory search disabled", async () => {
|
||||
@@ -159,20 +162,11 @@ describe("startGatewayMemoryBackend", () => {
|
||||
{ id: "ops", memorySearch: { enabled: false } },
|
||||
],
|
||||
});
|
||||
const log = createGatewayLogMock();
|
||||
getMemorySearchManagerMock.mockResolvedValue({ manager: createQmdManagerMock() });
|
||||
|
||||
await startGatewayMemoryBackend({ cfg, log });
|
||||
const log = await startQmdBackendWithManager(cfg);
|
||||
|
||||
expect(getMemorySearchManagerMock).toHaveBeenCalledTimes(1);
|
||||
expect(getMemorySearchManagerMock).toHaveBeenCalledWith({
|
||||
cfg,
|
||||
agentId: "main",
|
||||
purpose: "cli",
|
||||
});
|
||||
expect(log.info).toHaveBeenCalledWith(
|
||||
'qmd memory startup boot sync completed for 1 agent: "main"',
|
||||
);
|
||||
expectQmdManagerRequests(cfg, ["main"]);
|
||||
expectBootSyncCompleted(log, 1, '"main"');
|
||||
expect(log.warn).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -186,12 +180,9 @@ describe("startGatewayMemoryBackend", () => {
|
||||
},
|
||||
},
|
||||
} as OpenClawConfig;
|
||||
const log = createGatewayLogMock();
|
||||
|
||||
await startGatewayMemoryBackend({ cfg, log });
|
||||
const log = await startMemoryBackendForTest(cfg);
|
||||
|
||||
expect(getMemorySearchManagerMock).not.toHaveBeenCalled();
|
||||
expect(log.info).not.toHaveBeenCalled();
|
||||
expect(log.warn).not.toHaveBeenCalled();
|
||||
expectNoMemoryBackendStartup(log);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user