Skip to content
TypeScript
3.1 KB
Raw Blame
import { mkdtempSync, readFileSync } from "node:fs";
/**
 * W2/W3 — 시각화 전달 모드 + 고도화.
 * term(ANSI), serve(localhost), Pareto frontier, 브랜딩 self-contained.
 */
import { get } from "node:http";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import type { RunOptions } from "../src/models";
import { execute } from "../src/orchestrator";
import { serveRuns } from "../src/viz/serve";
import { renderTerminal } from "../src/viz/terminal";

function opts(out: string): RunOptions {
  return {
    models: ["opus4.8", "gpt5.5"],
    agents: ["cc", "opencode", "codex"],
    harnesses: ["none", "omo", "ponytail"],
    bench: "subagent",
    tasks: 3,
    runs: 1,
    seed: 5,
    runner: "mock",
    concurrency: 4,
    timeoutS: 5,
    outDir: out,
    judge: "none",
    score: false,
    balanced: false,
    language: "en",
  };
}

function httpGet(url: string): Promise<{ status: number; body: string }> {
  return new Promise((resolve, reject) => {
    get(url, (res) => {
      let body = "";
      res.on("data", (c) => {
        body += c;
      });
      res.on("end", () => resolve({ status: res.statusCode ?? 0, body }));
    }).on("error", reject);
  });
}

describe("--view term (ANSI)", () => {
  it("emits charts, perspective picks, and a flow tree", async () => {
    const out = mkdtempSync(join(tmpdir(), "theseus-viz-"));
    const res = await execute(opts(out));
    const ansi = renderTerminal(res.metrics, res.graphs, "en");
    const plain = ansi.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g"), "");
    expect(plain).toMatch(/accuracy/);
    expect(plain).toMatch(/total tok/);
    expect(plain).toMatch(/█|·/); // bar chars
    expect(plain).toMatch(/├─|└─/); // flow tree
  });
});

describe("--view serve (localhost)", () => {
  it("serves a run index and a self-contained report", async () => {
    const out = mkdtempSync(join(tmpdir(), "theseus-srv-"));
    const res = await execute(opts(out));
    const handle = await serveRuns(out, 0);
    try {
      const index = await httpGet(`${handle.url}/`);
      expect(index.status).toBe(200);
      expect(index.body).toMatch(/runs index/);
      expect(index.body).toContain(res.runId);

      const report = await httpGet(`${handle.url}/${res.runId}/report.html`);
      expect(report.status).toBe(200);
      expect(report.body).not.toMatch(/<script/i);

      // path traversal 차단
      const evil = await httpGet(`${handle.url}/../../etc/hosts`);
      expect(evil.status).toBe(404);
    } finally {
      handle.close();
    }
  });
});

describe("Pareto frontier marking", () => {
  it("report marks the cost/accuracy frontier", async () => {
    const out = mkdtempSync(join(tmpdir(), "theseus-par-"));
    const res = await execute(opts(out));
    const html = readFileSync(res.reportPath, "utf8");
    expect(html).toMatch(/Pareto frontier/);
    // 브랜딩: 로고 + 철학 + self-contained
    expect(html).toMatch(/Ship of Theseus/);
    expect(html).toContain("<svg");
    expect(html).not.toMatch(/<script/i);
  });
});