import { describe, expect, test } from "bun:test";
import { formatCurrentWrnSource, migrateV060WrnSource } from "../src/update.ts";

const report = () => ({
  changedAutomatically: [],
  needsReview: [],
  unresolvedImports: [],
  ambiguousFunctions: [],
  legacyOutputPayloads: [],
  parseFailures: [],
});

describe("v0.6 source migration", () => {
  test("converts legacy outputs and remains idempotent", () => {
    const input = `component Demo {
  props {
    @event confirm = function
  }
  functions {
    function confirm() {
      $emit("confirm", { ok: true })
    }
  }
  view { <button @click='confirm()'>Confirm</button> }
}`;
    const first = migrateV060WrnSource(input, report(), "Demo.wrn");
    const second = migrateV060WrnSource(first, report(), "Demo.wrn");
    expect(first).toContain("outputs {");
    expect(first).toContain("confirm(payload: unknown)");
    expect(first).toContain("output.confirm({ ok: true })");
    expect(second).toBe(first);
  });

  test("uses the canonical framework formatter idempotently", () => {
    const source = `page Home {
view {
<button type="button" class="one two three four five six seven eight nine ten eleven twelve" @click='save()'>Save</button>
}
}`;
    const formatted = formatCurrentWrnSource(source);

    expect(formatted).toContain("<button\n");
    expect(formatCurrentWrnSource(formatted)).toBe(formatted);
  });
});
