import { describe, expect, it } from "vitest";
import {
  applyTimingUpdates,
  buildTimingKey,
  createEmptyTimings,
  estimateTaskDuration,
  normalizeTimings,
} from "./index.mjs";

describe("timings", () => {
  it("creates and normalizes empty timing structures", () => {
    expect(createEmptyTimings()).toEqual({ version: 1, files: {} });
    expect(normalizeTimings({})).toEqual({ version: 1, files: {} });
  });

  it("applies rolling timing updates", () => {
    const next = applyTimingUpdates(
      {
        version: 1,
        files: {
          "api|k6|integration|a.js": {
            durationMs: 1000,
            runs: 1,
            updatedAt: "2020-01-01T00:00:00.000Z",
          },
        },
      },
      [{ key: "api|k6|integration|a.js", durationMs: 3000 }],
      "2020-01-02T00:00:00.000Z"
    );

    expect(next.files["api|k6|integration|a.js"]).toEqual({
      durationMs: 2000,
      runs: 2,
      updatedAt: "2020-01-02T00:00:00.000Z",
    });
  });

  it("estimates task durations and builds timing keys", () => {
    expect(
      estimateTaskDuration(
        { files: { key: { durationMs: 3210 } } },
        "key",
        { framework: "k6", type: "integration", weight: 1, files: ["a.js"] }
      )
    ).toBe(3210);

    expect(
      estimateTaskDuration(
        { files: {} },
        "key",
        { framework: "playwright", type: "e2e", weight: 2, files: ["a.js", "b.js"] }
      )
    ).toBe(20000);

    expect(
      buildTimingKey(
        "api",
        { framework: "k6", type: "integration" },
        "tests/health.js"
      )
    ).toBe("api|k6|integration|tests/health.js");
  });
});
