import { describe, expect, it } from "vitest";

import { presentBuildOperation, workspacePreviewMode } from "./hwpod-workspace-presentation";

describe("HWPOD workspace presentation", () => {
  it("renders Markdown files through the mature viewer mode", () => {
    expect(workspacePreviewMode("docs/README.md")).toBe("markdown");
    expect(workspacePreviewMode("notes.MARKDOWN")).toBe("markdown");
    expect(workspacePreviewMode("src/main.c")).toBe("text");
  });

  it("does not present an accepted asynchronous Keil job as completed", () => {
    const view = presentBuildOperation({
      ok: true,
      status: "completed",
      operationId: "operation-queued",
      result: {
        ok: true,
        response: {
          ok: true,
          results: [{ ok: true, output: { ok: true, exitCode: 0, stdout: JSON.stringify({ accepted: true, status: "queued", job_id: "keil-1" }) } }],
        },
      },
    });
    expect(view).toMatchObject({ tone: "warn", terminal: false, jobId: "keil-1", label: "构建已排队，等待真实终态" });
  });

  it("projects the real Keil terminal exit code and bounded logs", () => {
    const stdout = [
      JSON.stringify({ event: "started", command: "build", mode: "wait" }),
      JSON.stringify({ status: "completed", return_code: 0, errors: 0, warning_count: 2, elapsed_ms: 7165, message: "Build completed successfully", result: { hex_file: "F:\\out.hex", axf_file: "F:\\out.axf", build_log: "F:\\build.log" } }, null, 2),
    ].join("\n");
    const view = presentBuildOperation({
      ok: true,
      status: "completed",
      operationId: "operation-completed",
      result: {
        ok: true,
        response: {
          ok: true,
          results: [{ ok: true, output: { ok: true, exitCode: 0, stdout, stderr: "build started" } }],
        },
      },
    });
    expect(view).toMatchObject({ tone: "ok", terminal: true, exitCode: 0, label: "编译通过", message: "Build completed successfully", warningCount: 2, errorCount: 0, elapsedMs: 7165 });
    expect(view.artifacts).toEqual([
      { kind: "HEX", path: "F:\\out.hex" },
      { kind: "AXF", path: "F:\\out.axf" },
      { kind: "构建日志", path: "F:\\build.log" },
    ]);
    expect(view.log).toContain("build started");
    expect(view.log).toContain('"errors": 0');
  });
});
