|
| 1 | +import { afterEach, describe, expect, test } from "bun:test" |
| 2 | +import { chmodSync, existsSync, mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs" |
| 3 | +import { tmpdir } from "os" |
| 4 | +import { join } from "path" |
| 5 | +import { spawnSync } from "child_process" |
| 6 | + |
| 7 | +const tempRoots: string[] = [] |
| 8 | +const scriptPath = join(process.cwd(), "bin", "opencode-memory") |
| 9 | + |
| 10 | +function makeTempRoot(): string { |
| 11 | + const root = mkdtempSync(join(tmpdir(), "opencode-memory-test-")) |
| 12 | + tempRoots.push(root) |
| 13 | + return root |
| 14 | +} |
| 15 | + |
| 16 | +function writeExecutable(filePath: string, content: string): void { |
| 17 | + writeFileSync(filePath, content, "utf-8") |
| 18 | + chmodSync(filePath, 0o755) |
| 19 | +} |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + while (tempRoots.length > 0) { |
| 23 | + const root = tempRoots.pop() |
| 24 | + if (root) { |
| 25 | + rmSync(root, { recursive: true, force: true }) |
| 26 | + } |
| 27 | + } |
| 28 | +}) |
| 29 | + |
| 30 | +describe("opencode-memory wrapper", () => { |
| 31 | + test("normalizes TMPDIR before composing extraction log paths", () => { |
| 32 | + const root = makeTempRoot() |
| 33 | + const fakeBin = join(root, "bin") |
| 34 | + const homeDir = join(root, "home") |
| 35 | + const tmpDir = `${join(root, "tmp")}/` |
| 36 | + const claudeDir = join(root, "claude") |
| 37 | + |
| 38 | + mkdirSync(fakeBin, { recursive: true }) |
| 39 | + mkdirSync(homeDir, { recursive: true }) |
| 40 | + mkdirSync(tmpDir, { recursive: true }) |
| 41 | + mkdirSync(claudeDir, { recursive: true }) |
| 42 | + |
| 43 | + writeExecutable( |
| 44 | + join(fakeBin, "opencode"), |
| 45 | + `#!/usr/bin/env bash |
| 46 | +set -euo pipefail |
| 47 | +if [ "\${1:-}" = "session" ] && [ "\${2:-}" = "list" ]; then |
| 48 | + echo '[{"id":"ses_test_123","time":{"updated":1,"created":1}}]' |
| 49 | + exit 0 |
| 50 | +fi |
| 51 | +if [ "\${1:-}" = "run" ]; then |
| 52 | + echo "extraction ok" |
| 53 | + exit 0 |
| 54 | +fi |
| 55 | +if [ "\${1:-}" = "--help" ]; then |
| 56 | + echo "fake help" |
| 57 | + exit 0 |
| 58 | +fi |
| 59 | +exit 0 |
| 60 | +`, |
| 61 | + ) |
| 62 | + |
| 63 | + const result = spawnSync("bash", [scriptPath, "--help"], { |
| 64 | + cwd: root, |
| 65 | + encoding: "utf-8", |
| 66 | + env: { |
| 67 | + ...process.env, |
| 68 | + PATH: `${fakeBin}:${process.env.PATH ?? ""}`, |
| 69 | + HOME: homeDir, |
| 70 | + TMPDIR: tmpDir, |
| 71 | + CLAUDE_CONFIG_DIR: claudeDir, |
| 72 | + OPENCODE_MEMORY_FOREGROUND: "1", |
| 73 | + OPENCODE_MEMORY_AUTODREAM: "0", |
| 74 | + }, |
| 75 | + }) |
| 76 | + |
| 77 | + expect(result.status).toBe(0) |
| 78 | + expect(result.stderr).toContain("Extraction log: ") |
| 79 | + expect(result.stderr).not.toContain("//opencode-memory-logs") |
| 80 | + |
| 81 | + const logPathMatch = result.stderr.match(/Extraction log: (.+)\n/) |
| 82 | + expect(logPathMatch).not.toBeNull() |
| 83 | + |
| 84 | + const logPath = logPathMatch?.[1].trim() ?? "" |
| 85 | + expect(logPath.startsWith(join(root, "tmp", "opencode-memory-logs", "extract-"))).toBe(true) |
| 86 | + expect(existsSync(logPath)).toBe(true) |
| 87 | + expect(readFileSync(logPath, "utf-8")).toContain("extraction ok") |
| 88 | + }) |
| 89 | +}) |
0 commit comments