import { describe, expect, it } from "vitest";
import { buildHistoryTestId, createEmptyHistory, summarizeHistoryForFiles, updateHistoryFromRunArtifact } from "./index.mjs";

describe("test history", () => {
  it("tracks first seen and pass/fail counters across runs", () => {
    const history = createEmptyHistory();
    const first = updateHistoryFromRunArtifact(
      history,
      {
        generatedAt: "2026-04-30T10:00:00.000Z",
        services: [
          {
            name: "api",
            suites: [
              {
                name: "routes",
                type: "int",
                framework: "default",
                files: [
                  {
                    path: "src/api/routes/__testkit__/health.int.testkit.ts",
                    status: "passed",
                    durationMs: 2000,
                  },
                ],
              },
            ],
          },
        ],
      }
    );
    const second = updateHistoryFromRunArtifact(
      first,
      {
        generatedAt: "2026-05-01T10:00:00.000Z",
        services: [
          {
            name: "api",
            suites: [
              {
                name: "routes",
                type: "int",
                framework: "default",
                files: [
                  {
                    path: "src/api/routes/__testkit__/health.int.testkit.ts",
                    status: "failed",
                    durationMs: 4000,
                  },
                ],
              },
            ],
          },
        ],
      }
    );

    const id = buildHistoryTestId("api", "int", "src/api/routes/__testkit__/health.int.testkit.ts");
    expect(second.tests[id]).toMatchObject({
      firstSeenAt: "2026-04-30T10:00:00.000Z",
      lastSeenAt: "2026-05-01T10:00:00.000Z",
      lastRunAt: "2026-05-01T10:00:00.000Z",
      runCount: 2,
      passCount: 1,
      failCount: 1,
      skipCount: 0,
      avgDurationMs: 3000,
      lastStatus: "failed",
    });
  });

  it("summarizes history for discovery file entries", () => {
    const history = {
      version: 1,
      tests: {
        [buildHistoryTestId("api", "int", "src/api/routes/__testkit__/health.int.testkit.ts")]: {
          id: buildHistoryTestId("api", "int", "src/api/routes/__testkit__/health.int.testkit.ts"),
          path: "src/api/routes/__testkit__/health.int.testkit.ts",
          service: "api",
          suiteName: "routes",
          selectionType: "int",
          framework: "k6",
          firstSeenAt: "2026-04-30T10:00:00.000Z",
          lastSeenAt: "2026-05-01T10:00:00.000Z",
          lastRunAt: "2026-05-01T10:00:00.000Z",
          runCount: 2,
          passCount: 1,
          failCount: 1,
          skipCount: 0,
          notRunCount: 0,
          avgDurationMs: 3000,
          durationCount: 2,
          lastStatus: "failed",
        },
      },
    };

    const summaries = summarizeHistoryForFiles(history, [
      {
        id: buildHistoryTestId("api", "int", "src/api/routes/__testkit__/health.int.testkit.ts"),
        service: "api",
        selectionType: "int",
        path: "src/api/routes/__testkit__/health.int.testkit.ts",
      },
    ]);

    expect(summaries.get(buildHistoryTestId("api", "int", "src/api/routes/__testkit__/health.int.testkit.ts"))).toMatchObject({
      runCount: 2,
      passCount: 1,
      failCount: 1,
      avgDurationMs: 3000,
      lastStatus: "failed",
    });
  });
});
