import { Writable } from "stream";
import { describe, expect, it } from "vitest";
import { createRunReporter } from "./run-reporter.mjs";

describe("run reporter setup output", () => {
  it("prints concise high-level setup summaries in compact mode", () => {
    let stdout = "";
    const reporter = createRunReporter({
      outputMode: "compact",
      stdout: new Writable({
        write(chunk, _encoding, callback) {
          stdout += chunk.toString();
          callback();
        },
      }),
    });

    reporter.setupOperationFinished({
      serviceName: "api",
      stage: "template",
      kind: "database-template",
      summary: "template rebuild",
      status: "passed",
      durationMs: 8_000,
    });

    expect(stdout).toContain("RUN SETUP api template rebuild");
    expect(stdout).toContain("8s");
  });

  it("does not print low-level setup steps in compact mode", () => {
    let stdout = "";
    const reporter = createRunReporter({
      outputMode: "compact",
      stdout: new Writable({
        write(chunk, _encoding, callback) {
          stdout += chunk.toString();
          callback();
        },
      }),
    });

    reporter.setupOperationFinished({
      serviceName: "api",
      stage: "template:migrate:api:1",
      kind: "setup-step",
      summary: "sql-file: db/schema.sql",
      status: "passed",
      durationMs: 8_000,
    });

    expect(stdout).toBe("");
  });

  it("prints concise setup failures", () => {
    let stdout = "";
    const reporter = createRunReporter({
      outputMode: "compact",
      stdout: new Writable({
        write(chunk, _encoding, callback) {
          stdout += chunk.toString();
          callback();
        },
      }),
    });

    reporter.setupOperationFinished({
      serviceName: "api",
      stage: "runtime:prepare",
      kind: "runtime-prepare",
      summary: "runtime prepare",
      status: "failed",
      durationMs: 1_200,
      error: "Command failed with exit code 1: node scripts/fail-prepare.mjs",
    });

    expect(stdout).toContain("FAIL SETUP api runtime:prepare");
    expect(stdout).toContain("Command failed with exit code 1");
  });
});
