import { describe, expect, test } from "vitest";
import { DASHBOARD_SCHEMA_VERSION } from "@dimensionlab/dashboard-model";
import { genericDashboardFixture } from "@dimensionlab/dashboard-model/fixtures";
import {
  UnsupportedDashboardModelVersionError,
  migrateDashboardDocumentForPersistence,
} from "./model-migrations";

describe("dashboard model migrations", () => {
  test("accepts the current dashboard model version without migration", () => {
    const result = migrateDashboardDocumentForPersistence(genericDashboardFixture);

    expect(result.valid).toBe(true);
    if (!result.valid) throw new Error("expected current fixture to be valid");
    expect(result.document).toEqual(genericDashboardFixture);
    expect(result.fromVersion).toBe(DASHBOARD_SCHEMA_VERSION);
    expect(result.toVersion).toBe(DASHBOARD_SCHEMA_VERSION);
    expect(result.migrated).toBe(false);
  });

  test("fails unsupported model versions with an explicit migration error", () => {
    const previousVersion = {
      ...genericDashboardFixture,
      schemaVersion: "dashboard.v0",
    };

    expect(() => migrateDashboardDocumentForPersistence(previousVersion)).toThrow(
      UnsupportedDashboardModelVersionError,
    );
  });
});
