From 7abfd6bf64490cd11bf984bc566d1dd3733d772e Mon Sep 17 00:00:00 2001 From: bendtherules Date: Fri, 20 Mar 2026 14:46:00 +0530 Subject: [PATCH] test: fix tests --- tests/open_error.test.ts | 69 ++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/tests/open_error.test.ts b/tests/open_error.test.ts index 89b6c7a..97b4a78 100644 --- a/tests/open_error.test.ts +++ b/tests/open_error.test.ts @@ -1,5 +1,6 @@ import { expect, test, mock, beforeEach, afterEach, describe, spyOn } from "bun:test"; import * as os from "node:os"; +import * as fs from "node:fs"; import { openMultiModel } from "../src/core/open"; let originalBun$: typeof Bun.$ = (globalThis as any).Bun?.$; @@ -90,7 +91,7 @@ describe("openMultiModel error paths", () => { mockCommandResponses[`tmux has-session -t ${session}`] = { ok: false, stdout: "", stderr: "" }; // first model success const firstBranch = `opencode/${session}/gpt-5-2`; - const firstPath = `/home/user/.local/share/opencode/multi-model/${session}/gpt-5-2`; + const firstPath = `/mock/home/.local/share/opencode/multi-model/${session}/gpt-5-2`; mockCommandResponses[`git show-ref --verify --quiet refs/heads/${firstBranch}`] = { ok: false, stdout: "", stderr: "" }; mockCommandResponses[`git worktree add -b ${firstBranch} ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`tmux new-session -d -s ${session} -n gpt-5-2 -c ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; @@ -116,23 +117,25 @@ describe("openMultiModel error paths", () => { mockCommandResponses[`tmux has-session -t ${session}`] = { ok: false, stdout: "", stderr: "" }; // first model success const firstBranch = `opencode/${session}/gpt-5-2`; - const firstPath = `/home/user/.local/share/opencode/multi-model/${session}/gpt-5-2`; + const firstPath = `/mock/home/.local/share/opencode/multi-model/${session}/gpt-5-2`; mockCommandResponses[`git show-ref --verify --quiet refs/heads/${firstBranch}`] = { ok: false, stdout: "", stderr: "" }; mockCommandResponses[`git worktree add -b ${firstBranch} ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`tmux new-session -d -s ${session} -n gpt-5-2 -c ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`tmux send-keys -t ${session}:gpt-5-2 ${binary} --model 'openai/gpt-5.2' C-m`] = { ok: true, stdout: "", stderr: "" }; // second model fails worktree const secondBranch = `opencode/${session}/gpt-5-2-2`; - const secondPath = `/home/user/.local/share/opencode/multi-model/${session}/gpt-5-2-2`; + const secondPath = `/mock/home/.local/share/opencode/multi-model/${session}/gpt-5-2-2`; mockCommandResponses[`git show-ref --verify --quiet refs/heads/${secondBranch}`] = { ok: false, stdout: "", stderr: "" }; mockCommandResponses[`git worktree add -b ${secondBranch} ${secondPath}`] = { ok: false, stdout: "", stderr: "wt error" }; mockCommandResponses[`git worktree remove -f ${secondPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`git branch -D ${secondBranch}`] = { ok: true, stdout: "", stderr: "" }; - const result = await openMultiModel({ sessionName: session, models, binaryName: binary, mode: "cli" }); - expect(result.success).toBe(true); - expect(result.instructions).toContain("Error: Created tmux session"); expect(result.instructions).toContain("Failed: anthropic/gpt-5-2"); - expect(result.windows).toContain("gpt-5-2"); - expect(result.windows).toContain("gpt-5-2-2"); +const result = await openMultiModel({ sessionName: session, models, binaryName: binary, mode: "cli" }); + + + expect(result.success).toBe(true); + expect(result.instructions).toContain("Error: Created tmux session"); expect(result.instructions).toContain("Failed: anthropic/gpt-5-2"); + expect(result.windows).toContain("gpt-5-2"); + expect(result.windows).toContain("gpt-5-2-2"); }); test("second model window creation fails", async () => { @@ -146,24 +149,26 @@ describe("openMultiModel error paths", () => { mockCommandResponses[`tmux has-session -t ${session}`] = { ok: false, stdout: "", stderr: "" }; // first model success const firstBranch = `opencode/${session}/gpt-5-2`; - const firstPath = `/home/user/.local/share/opencode/multi-model/${session}/gpt-5-2`; + const firstPath = `/mock/home/.local/share/opencode/multi-model/${session}/gpt-5-2`; mockCommandResponses[`git show-ref --verify --quiet refs/heads/${firstBranch}`] = { ok: false, stdout: "", stderr: "" }; mockCommandResponses[`git worktree add -b ${firstBranch} ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`tmux new-session -d -s ${session} -n gpt-5-2 -c ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`tmux send-keys -t ${session}:gpt-5-2 ${binary} --model 'openai/gpt-5.2' C-m`] = { ok: true, stdout: "", stderr: "" }; // second model window failure const secondBranch = `opencode/${session}/gpt-5-2-2`; - const secondPath = `/home/user/.local/share/opencode/multi-model/${session}/gpt-5-2-2`; + const secondPath = `/mock/home/.local/share/opencode/multi-model/${session}/gpt-5-2-2`; mockCommandResponses[`git show-ref --verify --quiet refs/heads/${secondBranch}`] = { ok: false, stdout: "", stderr: "" }; mockCommandResponses[`git worktree add -b ${secondBranch} ${secondPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`tmux new-window -d -t ${session} -n gpt-5-2-2 -c ${secondPath}`] = { ok: false, stdout: "", stderr: "win err" }; mockCommandResponses[`git worktree remove -f ${secondPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`git branch -D ${secondBranch}`] = { ok: true, stdout: "", stderr: "" }; - const result = await openMultiModel({ sessionName: session, models, binaryName: binary, mode: "cli" }); - expect(result.success).toBe(true); - expect(result.instructions).toContain("Error: Created tmux session"); expect(result.instructions).toContain("Failed: anthropic/gpt-5-2"); - expect(result.windows).toContain("gpt-5-2"); - expect(result.windows).toContain("gpt-5-2-2"); +const result = await openMultiModel({ sessionName: session, models, binaryName: binary, mode: "cli" }); + + + expect(result.success).toBe(true); + expect(result.instructions).toContain("Error: Created tmux session"); expect(result.instructions).toContain("Failed: anthropic/gpt-5-2"); + expect(result.windows).toContain("gpt-5-2"); + expect(result.windows).toContain("gpt-5-2-2"); }); test("second model launch command fails", async () => { @@ -177,14 +182,14 @@ describe("openMultiModel error paths", () => { mockCommandResponses[`tmux has-session -t ${session}`] = { ok: false, stdout: "", stderr: "" }; // first model success const firstBranch = `opencode/${session}/gpt-5-2`; - const firstPath = `/home/user/.local/share/opencode/multi-model/${session}/gpt-5-2`; + const firstPath = `/mock/home/.local/share/opencode/multi-model/${session}/gpt-5-2`; mockCommandResponses[`git show-ref --verify --quiet refs/heads/${firstBranch}`] = { ok: false, stdout: "", stderr: "" }; mockCommandResponses[`git worktree add -b ${firstBranch} ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`tmux new-session -d -s ${session} -n gpt-5-2 -c ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`tmux send-keys -t ${session}:gpt-5-2 ${binary} --model 'openai/gpt-5.2' C-m`] = { ok: true, stdout: "", stderr: "" }; // second model launch fails const secondBranch = `opencode/${session}/gpt-5-2-2`; - const secondPath = `/home/user/.local/share/opencode/multi-model/${session}/gpt-5-2-2`; + const secondPath = `/mock/home/.local/share/opencode/multi-model/${session}/gpt-5-2-2`; mockCommandResponses[`git show-ref --verify --quiet refs/heads/${secondBranch}`] = { ok: false, stdout: "", stderr: "" }; mockCommandResponses[`git worktree add -b ${secondBranch} ${secondPath}`] = { ok: true, stdout: "", stderr: "" }; mockCommandResponses[`tmux new-window -d -t ${session} -n gpt-5-2-2 -c ${secondPath}`] = { ok: true, stdout: "", stderr: "" }; @@ -198,4 +203,34 @@ describe("openMultiModel error paths", () => { expect(result.windows).toContain("gpt-5-2-2"); }); + test("second model worktree path already exists", async () => { + const session = "sess-wtpath"; + const binary = "opencode"; + const models = ["openai/gpt-5.2", "anthropic/gpt-5-2"]; + mockCommandResponses["git rev-parse --is-inside-work-tree"] = { ok: true, stdout: "true", stderr: "" }; + mockCommandResponses["command -v tmux"] = { ok: true, stdout: "tmux", stderr: "" }; + mockCommandResponses["command -v opencode"] = { ok: true, stdout: "opencode", stderr: "" }; + mockCommandResponses["opencode models"] = { ok: true, stdout: models.join("\n"), stderr: "" }; + mockCommandResponses[`tmux has-session -t ${session}`] = { ok: false, stdout: "", stderr: "" }; + // first model success + const firstBranch = `opencode/${session}/gpt-5-2`; + const firstPath = `/mock/home/.local/share/opencode/multi-model/${session}/gpt-5-2`; + mockCommandResponses[`git show-ref --verify --quiet refs/heads/${firstBranch}`] = { ok: false, stdout: "", stderr: "" }; + mockCommandResponses[`git worktree add -b ${firstBranch} ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; + mockCommandResponses[`tmux new-session -d -s ${session} -n gpt-5-2 -c ${firstPath}`] = { ok: true, stdout: "", stderr: "" }; + mockCommandResponses[`tmux send-keys -t ${session}:gpt-5-2 ${binary} --model 'openai/gpt-5.2' C-m`] = { ok: true, stdout: "", stderr: "" }; + // second model path exists + const secondBranch = `opencode/${session}/gpt-5-2-2`; + const secondPath = `/mock/home/.local/share/opencode/multi-model/${session}/gpt-5-2-2`; + mockCommandResponses[`git show-ref --verify --quiet refs/heads/${secondBranch}`] = { ok: false, stdout: "", stderr: "" }; + // mock fs existsSync to return true for secondPath + spyOn(fs, "existsSync").mockImplementation((p) => p === secondPath); + const result = await openMultiModel({ sessionName: session, models, binaryName: binary, mode: "cli" }); + expect(result.success).toBe(true); + expect(result.instructions).toContain("Error: Created tmux session"); + expect(result.instructions).toContain("Failed: anthropic/gpt-5-2 (Worktree path already exists at"); + expect(result.windows).toContain("gpt-5-2"); + expect(result.windows).toContain("gpt-5-2-2"); + }); + });