|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { spawnSync } from "node:child_process"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | +import { createRequire, syncBuiltinESMExports } from "node:module"; |
| 8 | +import { test } from "node:test"; |
| 9 | +import React from "react"; |
| 10 | +import { Models } from "openai/resources/models"; |
| 11 | +import { SessionManager, resolveCurrentSettings } from "@vegamo/deepcode-core"; |
| 12 | +import AppContainer from "../ui/views/AppContainer"; |
| 13 | +import { terminal } from "./terminal-helper"; |
| 14 | + |
| 15 | +for (const hasProject of [true, false]) { |
| 16 | + test(`TUI uses custom settings while preserving save scope (project exists: ${hasProject})`, async (t) => { |
| 17 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), "cli-settings-file-")); |
| 18 | + const project = path.join(root, "project"); |
| 19 | + const projectFile = path.join(project, ".deepcode/settings.json"); |
| 20 | + const userFile = path.join(root, ".deepcode/settings.json"); |
| 21 | + const settingsFile = path.join(root, "custom.json"); |
| 22 | + fs.mkdirSync(path.dirname(projectFile), { recursive: true }); |
| 23 | + fs.mkdirSync(path.dirname(userFile), { recursive: true }); |
| 24 | + fs.writeFileSync(userFile, "{}"); |
| 25 | + if (hasProject) fs.writeFileSync(projectFile, "{}"); |
| 26 | + const custom = JSON.stringify({ |
| 27 | + model: "deepseek-v4-pro", |
| 28 | + env: { API_KEY: "test-key", BASE_URL: "https://custom.invalid" }, |
| 29 | + permissions: { defaultMode: "askAll" }, |
| 30 | + statusline: { enabled: false }, |
| 31 | + }); |
| 32 | + fs.writeFileSync(settingsFile, custom); |
| 33 | + const env = process.env; |
| 34 | + process.env = Object.fromEntries(Object.entries(env).filter(([key]) => !key.startsWith("DEEPCODE_"))); |
| 35 | + t.mock.method(os, "homedir", () => root); |
| 36 | + syncBuiltinESMExports(); |
| 37 | + t.mock.method(Models.prototype, "list", async () => ({ data: [] })); |
| 38 | + t.mock.method(SessionManager.prototype, "initMcpServers", async () => {}); |
| 39 | + t.mock.method(SessionManager.prototype, "listSkills", async () => []); |
| 40 | + t.mock.method(SessionManager.prototype, "listSessions", () => []); |
| 41 | + const element = (key: number) => |
| 42 | + React.createElement(AppContainer, { |
| 43 | + key, |
| 44 | + projectRoot: project, |
| 45 | + settingsFile, |
| 46 | + version: "test", |
| 47 | + initialPrompt: undefined, |
| 48 | + resumeSessionId: undefined, |
| 49 | + forkSessionId: undefined, |
| 50 | + onRestart() {}, |
| 51 | + }); |
| 52 | + const h = await terminal(t, element(0), 120); |
| 53 | + t.after(() => { |
| 54 | + t.mock.restoreAll(); |
| 55 | + syncBuiltinESMExports(); |
| 56 | + process.env = env; |
| 57 | + fs.rmSync(root, { recursive: true, force: true }); |
| 58 | + }); |
| 59 | + assert.match(h.frame(), /deepseek-v4-pro/); |
| 60 | + await h.send("/model"); |
| 61 | + await h.send("\r"); |
| 62 | + assert.match(h.frame(), hasProject ? /All Projects +\[This Project\]/ : /\[All Projects\] +This Project/); |
| 63 | + await h.send("\u001b[A"); // Select the preceding model (deepseek-flash). |
| 64 | + await h.send("\r"); |
| 65 | + await h.send("\r"); |
| 66 | + const targetFile = hasProject ? projectFile : userFile; |
| 67 | + assert.equal(JSON.parse(fs.readFileSync(targetFile, "utf8")).model, "deepseek-flash"); |
| 68 | + assert.equal(resolveCurrentSettings(project, settingsFile).model, "deepseek-v4-pro"); |
| 69 | + await h.send("/permissions"); |
| 70 | + await h.send("\r"); |
| 71 | + assert.match(h.frame(), hasProject ? /All Projects +\[This Project\]/ : /\[All Projects\] +This Project/); |
| 72 | + await h.send("\r"); // Edit defaultMode. |
| 73 | + await h.send("\u001b[A"); // askAll. |
| 74 | + await h.send("\r"); |
| 75 | + await h.send("\u001b[A"); // Save. |
| 76 | + await h.send("\r"); |
| 77 | + assert.equal(JSON.parse(fs.readFileSync(targetFile, "utf8")).permissions.defaultMode, "askAll"); |
| 78 | + assert.equal(fs.readFileSync(settingsFile, "utf8"), custom); |
| 79 | + h.rerender(element(1)); |
| 80 | + await h.send(""); |
| 81 | + assert.match(h.frame(), /deepseek-v4-pro/); |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +test("CLI rejects invalid explicit files before startup but help and version do not read them", (t) => { |
| 86 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), "cli-settings-errors-")); |
| 87 | + t.after(() => fs.rmSync(root, { recursive: true, force: true })); |
| 88 | + const file = path.join(root, "settings.json"); |
| 89 | + const cli = fileURLToPath(new URL("../cli.tsx", import.meta.url)); |
| 90 | + const config = fileURLToPath(new URL("../../tsconfig.json", import.meta.url)); |
| 91 | + const tsx = createRequire(import.meta.url).resolve("tsx"); |
| 92 | + const run = (...args: string[]) => |
| 93 | + spawnSync(process.execPath, ["--import", tsx, cli, ...args], { |
| 94 | + cwd: root, |
| 95 | + encoding: "utf8", |
| 96 | + env: { ...process.env, TSX_TSCONFIG_PATH: config }, |
| 97 | + timeout: 15_000, |
| 98 | + }); |
| 99 | + for (const raw of [undefined, '{"secret":"do-not-print",broken}', "[]"]) { |
| 100 | + if (raw !== undefined) fs.writeFileSync(file, raw); |
| 101 | + for (const mode of [[], ["-x", "-p", "hello"]]) { |
| 102 | + const result = run("-s", "settings.json", ...mode); |
| 103 | + assert.equal(result.status, 1, result.stderr); |
| 104 | + assert.ok(result.stderr.includes(file), result.stderr); |
| 105 | + assert.doesNotMatch(result.stderr, /do-not-print/); |
| 106 | + assert.equal(result.stdout, ""); |
| 107 | + } |
| 108 | + } |
| 109 | + for (const flag of ["--help", "--version"]) { |
| 110 | + const result = run("--settings", "missing.json", flag); |
| 111 | + assert.equal(result.status, 0, result.stderr); |
| 112 | + assert.equal(result.stderr, ""); |
| 113 | + if (flag === "--help") assert.match(result.stdout, /-s, --settings +Path to a settings JSON file/); |
| 114 | + } |
| 115 | +}); |
0 commit comments