Skip to content

Commit 1accff3

Browse files
committed
Simplify trunk branch test to avoid runtime branching
- Add readStream() helper to read from ReadableStream - Use RUNTIME_EXEC IPC for both local and SSH runtimes - Remove conditional logic based on runtime type - Reduces test code from ~80 lines to ~35 lines - Same verification logic now runs for both runtimes This makes the test cleaner and easier to maintain while ensuring parity between runtime implementations.
1 parent 4423fd2 commit 1accff3

File tree

2 files changed

+59
-74
lines changed

2 files changed

+59
-74
lines changed

tests/ipcMain/RUN_TRUNK_BRANCH_TEST.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ This test is part of the runtime matrix and runs for both:
7777

7878
This ensures parity between runtime implementations.
7979

80+
## Implementation Details
81+
82+
The test uses a unified approach for both runtimes:
83+
- Uses `RUNTIME_EXEC` IPC channel to execute shell commands (works for both local and SSH)
84+
- Avoids runtime-specific branching logic
85+
- Uses helper function `readStream()` to read command output from ReadableStream
86+
87+
This approach simplifies the test and ensures the same verification logic runs for both runtimes.
88+

tests/ipcMain/createWorkspace.test.ts

Lines changed: 50 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,25 @@ async function commitChanges(repoPath: string, message: string): Promise<void> {
116116
});
117117
}
118118

119+
/**
120+
* Read a ReadableStream to a string
121+
*/
122+
async function readStream(stream: ReadableStream<Uint8Array>): Promise<string> {
123+
const reader = stream.getReader();
124+
const decoder = new TextDecoder();
125+
let result = "";
126+
try {
127+
while (true) {
128+
const { done, value } = await reader.read();
129+
if (done) break;
130+
result += decoder.decode(value, { stream: true });
131+
}
132+
} finally {
133+
reader.releaseLock();
134+
}
135+
return result;
136+
}
137+
119138
/**
120139
* Create workspace and handle cleanup on test failure
121140
* Returns result and cleanup function
@@ -319,80 +338,37 @@ describeIntegration("WORKSPACE_CREATE with both runtimes", () => {
319338
await new Promise((resolve) => setTimeout(resolve, getInitWaitTime()));
320339

321340
// Verify the new branch was created from custom-trunk, not from default branch
322-
const workspacePath = result.metadata.namedWorkspacePath;
323-
324-
// For LocalRuntime, check the worktree directly
325-
// For SSHRuntime, we need to check the remote workspace
326-
if (type === "local") {
327-
// Check that trunk-file.txt exists (from custom-trunk)
328-
const trunkFileExists = await fs
329-
.access(path.join(workspacePath, "trunk-file.txt"))
330-
.then(() => true)
331-
.catch(() => false);
332-
expect(trunkFileExists).toBe(true);
333-
334-
// Check that other-file.txt does NOT exist (from other-branch)
335-
const otherFileExists = await fs
336-
.access(path.join(workspacePath, "other-file.txt"))
337-
.then(() => true)
338-
.catch(() => false);
339-
expect(otherFileExists).toBe(false);
340-
341-
// Verify git log shows the custom trunk commit
342-
const { stdout: logOutput } = await execAsync(
343-
`git log --oneline --all`,
344-
{ cwd: workspacePath }
345-
);
346-
expect(logOutput).toContain("Custom trunk commit");
347-
} else if (type === "ssh" && sshConfig) {
348-
// For SSH runtime, check files on the remote host
349-
const checkFileCmd = `test -f ${workspacePath}/trunk-file.txt && echo "exists" || echo "missing"`;
350-
const checkFileStream = await env.mockIpcRenderer.invoke(
351-
IPC_CHANNELS.RUNTIME_EXEC,
352-
result.metadata.id,
353-
checkFileCmd,
354-
{ cwd: workspacePath, timeout: 10 }
355-
);
356-
357-
// Read stdout to check if file exists
358-
let checkOutput = "";
359-
const reader = checkFileStream.stdout.getReader();
360-
const decoder = new TextDecoder();
361-
try {
362-
while (true) {
363-
const { done, value } = await reader.read();
364-
if (done) break;
365-
checkOutput += decoder.decode(value, { stream: true });
366-
}
367-
} finally {
368-
reader.releaseLock();
369-
}
370-
371-
expect(checkOutput.trim()).toBe("exists");
372-
373-
// Check that other-file.txt does NOT exist
374-
const checkOtherFileCmd = `test -f ${workspacePath}/other-file.txt && echo "exists" || echo "missing"`;
375-
const checkOtherStream = await env.mockIpcRenderer.invoke(
376-
IPC_CHANNELS.RUNTIME_EXEC,
377-
result.metadata.id,
378-
checkOtherFileCmd,
379-
{ cwd: workspacePath, timeout: 10 }
380-
);
381-
382-
let checkOtherOutput = "";
383-
const otherReader = checkOtherStream.stdout.getReader();
384-
try {
385-
while (true) {
386-
const { done, value } = await otherReader.read();
387-
if (done) break;
388-
checkOtherOutput += decoder.decode(value, { stream: true });
389-
}
390-
} finally {
391-
otherReader.releaseLock();
392-
}
393-
394-
expect(checkOtherOutput.trim()).toBe("missing");
395-
}
341+
// Use RUNTIME_EXEC to check files (works for both local and SSH runtimes)
342+
343+
// Check that trunk-file.txt exists (from custom-trunk)
344+
const checkTrunkFileStream = await env.mockIpcRenderer.invoke(
345+
IPC_CHANNELS.RUNTIME_EXEC,
346+
result.metadata.id,
347+
`test -f trunk-file.txt && echo "exists" || echo "missing"`,
348+
{ timeout: 10 }
349+
);
350+
const trunkFileOutput = await readStream(checkTrunkFileStream.stdout);
351+
expect(trunkFileOutput.trim()).toBe("exists");
352+
353+
// Check that other-file.txt does NOT exist (from other-branch)
354+
const checkOtherFileStream = await env.mockIpcRenderer.invoke(
355+
IPC_CHANNELS.RUNTIME_EXEC,
356+
result.metadata.id,
357+
`test -f other-file.txt && echo "exists" || echo "missing"`,
358+
{ timeout: 10 }
359+
);
360+
const otherFileOutput = await readStream(checkOtherFileStream.stdout);
361+
expect(otherFileOutput.trim()).toBe("missing");
362+
363+
// Verify git log shows the custom trunk commit
364+
const gitLogStream = await env.mockIpcRenderer.invoke(
365+
IPC_CHANNELS.RUNTIME_EXEC,
366+
result.metadata.id,
367+
`git log --oneline --all`,
368+
{ timeout: 10 }
369+
);
370+
const logOutput = await readStream(gitLogStream.stdout);
371+
expect(logOutput).toContain("Custom trunk commit");
396372

397373
await cleanup();
398374
} finally {

0 commit comments

Comments
 (0)