import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";

const root = fileURLToPath(new URL("../../", import.meta.url));
const source = (relative: string) => readFileSync(`${root}/${relative}`, "utf8");

describe("I1 owner-pain invariants", () => {
  it("no-git-vocabulary-in-user-surfaces", () => {
    const userSurfaceSources = [
      "packages/application/src/read-models.ts",
      "apps/web/src/data-source.ts",
      "apps/web/src/approved-live.ts",
      "apps/web/src/render-review.ts",
      "apps/web/src/render-factory.ts",
      "apps/web/src/render-project.ts",
      "apps/web/src/render-queue.ts",
    ].map(source);
    const combined = userSurfaceSources.join("\n");
    const banned: readonly RegExp[] = [
      /\brebase\b/iu,
      /force-push/iu,
      /\bupstream\b/iu,
      /\bHEAD\b/u,
      /\bdetached\b/iu,
      /conflict marker/iu,
      /cherry-pick/iu,
      /\bstash\b/iu,
      /target-head/iu,
      /target head/iu,
    ];
    for (const term of banned) expect(combined).not.toMatch(term);
  });

  it("no-approval-of-git-kind", () => {
    const model = source("packages/domain/src/model.ts");
    const approval = model.match(/export interface Approval[\s\S]*?\n\}/u)?.[0] ?? "";
    expect(approval).not.toMatch(/git|branch|merge|rebase|head|stash|conflict/iu);

    const repositories = source("packages/application/src/ports/repositories.ts");
    expect(repositories).not.toMatch(/ApprovalRepository|readonly approvals:/u);
  });

  it("terminal destructive cleanup is gated by a collected immutable checkpoint", () => {
    const execution = source("packages/application/src/execution.ts");
    const cleanup = execution.match(
      /private async cleanupTerminalChangeSetWorkspaces[\s\S]*?private controlPlaneContinuationContext/u,
    )?.[0];
    expect(cleanup).toBeDefined();
    const guard = cleanup!.indexOf("has no collected content checkpoint");
    const destroy = cleanup!.indexOf("awp-i1-automerge-workspace-destroy");
    expect(guard).toBeGreaterThanOrEqual(0);
    expect(destroy).toBeGreaterThan(guard);
    expect(cleanup).toContain("current.checkpointDigest !== workspace.checkpointDigest");
    expect(cleanup).toContain("!current.checkpointCollectedAt");
  });

  it("agent completion cannot mark a Task completed before verified trusted merge", () => {
    const execution = source("packages/application/src/execution.ts");
    const completion = execution.match(/async complete\([\s\S]*?async refreshRequiredChecks/u)?.[0];
    const merge = execution.match(
      /private async executeMerge\([\s\S]*?private async reconcileMergedFactoryRunDispatches/u,
    )?.[0];
    expect(completion).toBeDefined();
    expect(completion).toContain('tx.tasks.update({ ...task, status: "review"');
    expect(completion).not.toContain('tx.tasks.update({ ...task, status: "completed"');
    expect(merge).toBeDefined();
    expect(merge).toContain("verificationSatisfied(changeSet, evidence, findings)");
    expect(merge).toContain('status: "completed"');
  });
});
