chore(test): stabilize mcporter assertions on Windows

This commit is contained in:
Vignesh Natarajan
2026-02-22 14:47:50 -08:00
parent 82d34b4b06
commit a58b40e153

View File

@@ -57,6 +57,13 @@ function emitAndClose(
}); });
} }
function isMcporterCommand(cmd: unknown): boolean {
if (typeof cmd !== "string") {
return false;
}
return /(^|[\\/])mcporter(?:\.cmd)?$/i.test(cmd);
}
vi.mock("../logging/subsystem.js", () => ({ vi.mock("../logging/subsystem.js", () => ({
createSubsystemLogger: () => { createSubsystemLogger: () => {
const logger = { const logger = {
@@ -1339,7 +1346,7 @@ describe("QmdMemoryManager", () => {
spawnMock.mockImplementation((cmd: string, args: string[]) => { spawnMock.mockImplementation((cmd: string, args: string[]) => {
const child = createMockChild({ autoClose: false }); const child = createMockChild({ autoClose: false });
if (cmd === "mcporter" && args[0] === "call") { if (isMcporterCommand(cmd) && args[0] === "call") {
emitAndClose(child, "stdout", JSON.stringify({ results: [] })); emitAndClose(child, "stdout", JSON.stringify({ results: [] }));
return child; return child;
} }
@@ -1354,7 +1361,9 @@ describe("QmdMemoryManager", () => {
manager.search("hello", { sessionKey: "agent:main:slack:dm:u123" }), manager.search("hello", { sessionKey: "agent:main:slack:dm:u123" }),
).resolves.toEqual([]); ).resolves.toEqual([]);
const mcporterCalls = spawnMock.mock.calls.filter((call: unknown[]) => call[0] === "mcporter"); const mcporterCalls = spawnMock.mock.calls.filter((call: unknown[]) =>
isMcporterCommand(call[0]),
);
expect(mcporterCalls.length).toBeGreaterThan(0); expect(mcporterCalls.length).toBeGreaterThan(0);
expect(mcporterCalls.some((call: unknown[]) => (call[1] as string[])[0] === "daemon")).toBe( expect(mcporterCalls.some((call: unknown[]) => (call[1] as string[])[0] === "daemon")).toBe(
false, false,
@@ -1421,7 +1430,7 @@ describe("QmdMemoryManager", () => {
spawnMock.mockImplementation((cmd: string, args: string[]) => { spawnMock.mockImplementation((cmd: string, args: string[]) => {
const child = createMockChild({ autoClose: false }); const child = createMockChild({ autoClose: false });
if (cmd === "mcporter" && args[0] === "call") { if (isMcporterCommand(cmd) && args[0] === "call") {
emitAndClose(child, "stdout", JSON.stringify({ results: [] })); emitAndClose(child, "stdout", JSON.stringify({ results: [] }));
return child; return child;
} }
@@ -1433,7 +1442,7 @@ describe("QmdMemoryManager", () => {
await manager.search("hello", { sessionKey: "agent:main:slack:dm:u123" }); await manager.search("hello", { sessionKey: "agent:main:slack:dm:u123" });
const mcporterCall = spawnMock.mock.calls.find( const mcporterCall = spawnMock.mock.calls.find(
(call: unknown[]) => call[0] === "mcporter" && (call[1] as string[])[0] === "call", (call: unknown[]) => isMcporterCommand(call[0]) && (call[1] as string[])[0] === "call",
); );
expect(mcporterCall).toBeDefined(); expect(mcporterCall).toBeDefined();
const spawnOpts = mcporterCall?.[2] as { env?: NodeJS.ProcessEnv } | undefined; const spawnOpts = mcporterCall?.[2] as { env?: NodeJS.ProcessEnv } | undefined;
@@ -1461,7 +1470,7 @@ describe("QmdMemoryManager", () => {
let daemonAttempts = 0; let daemonAttempts = 0;
spawnMock.mockImplementation((cmd: string, args: string[]) => { spawnMock.mockImplementation((cmd: string, args: string[]) => {
const child = createMockChild({ autoClose: false }); const child = createMockChild({ autoClose: false });
if (cmd === "mcporter" && args[0] === "daemon") { if (isMcporterCommand(cmd) && args[0] === "daemon") {
daemonAttempts += 1; daemonAttempts += 1;
if (daemonAttempts === 1) { if (daemonAttempts === 1) {
emitAndClose(child, "stderr", "failed", 1); emitAndClose(child, "stderr", "failed", 1);
@@ -1470,7 +1479,7 @@ describe("QmdMemoryManager", () => {
} }
return child; return child;
} }
if (cmd === "mcporter" && args[0] === "call") { if (isMcporterCommand(cmd) && args[0] === "call") {
emitAndClose(child, "stdout", JSON.stringify({ results: [] })); emitAndClose(child, "stdout", JSON.stringify({ results: [] }));
return child; return child;
} }
@@ -1504,11 +1513,11 @@ describe("QmdMemoryManager", () => {
spawnMock.mockImplementation((cmd: string, args: string[]) => { spawnMock.mockImplementation((cmd: string, args: string[]) => {
const child = createMockChild({ autoClose: false }); const child = createMockChild({ autoClose: false });
if (cmd === "mcporter" && args[0] === "daemon") { if (isMcporterCommand(cmd) && args[0] === "daemon") {
emitAndClose(child, "stdout", ""); emitAndClose(child, "stdout", "");
return child; return child;
} }
if (cmd === "mcporter" && args[0] === "call") { if (isMcporterCommand(cmd) && args[0] === "call") {
emitAndClose(child, "stdout", JSON.stringify({ results: [] })); emitAndClose(child, "stdout", JSON.stringify({ results: [] }));
return child; return child;
} }
@@ -1522,7 +1531,7 @@ describe("QmdMemoryManager", () => {
await manager.search("two", { sessionKey: "agent:main:slack:dm:u123" }); await manager.search("two", { sessionKey: "agent:main:slack:dm:u123" });
const daemonStarts = spawnMock.mock.calls.filter( const daemonStarts = spawnMock.mock.calls.filter(
(call: unknown[]) => call[0] === "mcporter" && (call[1] as string[])[0] === "daemon", (call: unknown[]) => isMcporterCommand(call[0]) && (call[1] as string[])[0] === "daemon",
); );
expect(daemonStarts).toHaveLength(1); expect(daemonStarts).toHaveLength(1);