Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,032 changes: 0 additions & 5,032 deletions packages/core/src/__tests__/session-manager.test.ts

This file was deleted.

563 changes: 563 additions & 0 deletions packages/core/src/__tests__/session-manager/claim-pr.test.ts

Large diffs are not rendered by default.

556 changes: 556 additions & 0 deletions packages/core/src/__tests__/session-manager/communication.test.ts

Large diffs are not rendered by default.

699 changes: 699 additions & 0 deletions packages/core/src/__tests__/session-manager/lifecycle.test.ts

Large diffs are not rendered by default.

149 changes: 149 additions & 0 deletions packages/core/src/__tests__/session-manager/opencode-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import {
chmodSync,
mkdirSync,
writeFileSync,
} from "node:fs";
import { join } from "node:path";
import { randomUUID } from "node:crypto";

export function installMockOpencode(
tmpDir: string,
sessionListJson: string,
deleteLogPath: string,
listDelaySeconds = 0,
listLogPath?: string,
): string {
const binDir = join(tmpDir, "mock-bin");
mkdirSync(binDir, { recursive: true });
const scriptPath = join(binDir, "opencode");
writeFileSync(
scriptPath,
[
"#!/usr/bin/env bash",
"set -euo pipefail",
'if [[ "$1" == "session" && "$2" == "list" ]]; then',
listLogPath ? ` printf '%s\n' "$*" >> '${listLogPath.replace(/'/g, "'\\''")}'` : "",
listDelaySeconds > 0 ? ` sleep ${listDelaySeconds}` : "",
` printf '%s\n' '${sessionListJson.replace(/'/g, "'\\''")}'`,
" exit 0",
"fi",
'if [[ "$1" == "session" && "$2" == "delete" ]]; then',
` printf '%s\n' "$*" >> '${deleteLogPath.replace(/'/g, "'\\''")}'`,
" exit 0",
"fi",
"exit 1",
"",
].join("\n"),
"utf-8",
);
chmodSync(scriptPath, 0o755);
return binDir;
}

export function installMockOpencodeSequence(
tmpDir: string,
sessionListJsons: string[],
deleteLogPath: string,
listLogPath?: string,
): string {
const binDir = join(tmpDir, "mock-bin-sequence");
mkdirSync(binDir, { recursive: true });
const scriptPath = join(binDir, "opencode");
const sequencePath = join(tmpDir, `opencode-sequence-${randomUUID()}.txt`);
writeFileSync(sequencePath, "0\n", "utf-8");

const cases = sessionListJsons
.map((entry, index) => {
const escaped = entry.replace(/'/g, "'\\''");
return `if [[ "$idx" == "${index}" ]]; then printf '%s\\n' '${escaped}'; exit 0; fi`;
})
.join("\n");
const final = sessionListJsons.at(-1)?.replace(/'/g, "'\\''") ?? "[]";

writeFileSync(
scriptPath,
[
"#!/usr/bin/env bash",
"set -euo pipefail",
'if [[ "$1" == "session" && "$2" == "list" ]]; then',
listLogPath ? ` printf '%s\n' "$*" >> '${listLogPath.replace(/'/g, "'\\''")}'` : "",
` seq_file='${sequencePath.replace(/'/g, "'\\''")}'`,
' idx=$(cat "$seq_file")',
" next=$((idx + 1))",
' printf "%s\n" "$next" > "$seq_file"',
` ${cases}`,
` printf '%s\\n' '${final}'`,
" exit 0",
"fi",
'if [[ "$1" == "session" && "$2" == "delete" ]]; then',
` printf '%s\n' "$*" >> '${deleteLogPath.replace(/'/g, "'\\''")}'`,
" exit 0",
"fi",
"exit 1",
"",
]
.filter(Boolean)
.join("\n"),
"utf-8",
);
chmodSync(scriptPath, 0o755);
return binDir;
}

export function installMockOpencodeWithNotFoundDelete(
tmpDir: string,
sessionListJson: string,
): string {
const binDir = join(tmpDir, "mock-bin-not-found");
mkdirSync(binDir, { recursive: true });
const scriptPath = join(binDir, "opencode");
writeFileSync(
scriptPath,
[
"#!/usr/bin/env bash",
"set -euo pipefail",
'if [[ "$1" == "session" && "$2" == "list" ]]; then',
` printf '%s\n' '${sessionListJson.replace(/'/g, "'\\''")}'`,
" exit 0",
"fi",
'if [[ "$1" == "session" && "$2" == "delete" ]]; then',
' printf "Error: Session not found: %s\\n" "$3" >&2',
" exit 1",
"fi",
"exit 1",
"",
].join("\n"),
"utf-8",
);
chmodSync(scriptPath, 0o755);
return binDir;
}

export function installMockGit(
tmpDir: string,
remoteBranches: string[],
): string {
const binDir = join(tmpDir, "mock-git-bin");
mkdirSync(binDir, { recursive: true });
const scriptPath = join(binDir, "git");
const refs = remoteBranches
.map((branch) => `deadbeef\trefs/heads/${branch}`)
.join("\\n")
.replace(/'/g, "'\\''");
writeFileSync(
scriptPath,
[
"#!/usr/bin/env bash",
"set -euo pipefail",
'if [[ "$1" == "ls-remote" && "$2" == "--heads" && "$3" == "origin" ]]; then',
` printf '%b\\n' '${refs}'`,
" exit 0",
"fi",
"exit 1",
"",
].join("\n"),
"utf-8",
);
chmodSync(scriptPath, 0o755);
return binDir;
}
Loading
Loading