import { afterEach, describe, expect, it } from "vitest";
import { join } from "node:path";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";

import { RecentAssetService } from "../../apps/api/src/recent-assets.js";

const roots: string[] = [];

afterEach(() => {
  for (const root of roots.splice(0)) rmSync(root, { force: true, recursive: true });
});

describe("TASK-WP4-03 account recent text templates", () => {
  it("writes only a successfully used template and isolates accounts", () => {
    const root = mkdtempSync(join(tmpdir(), "dada-wp4-03-"));
    roots.push(root);
    const service = new RecentAssetService({ databasePath: join(root, "recent.sqlite") });
    service.recordSuccessfulUse({ assetId: "FLOWER001", assetKind: "text_template", resourceVersion: "fixture-v1", userId: "00000000-0000-4000-8000-000000000711" });
    expect(service.list("00000000-0000-4000-8000-000000000711", "text_template")).toEqual([
      { asset_id: "FLOWER001", asset_kind: "text_template", resource_version: "fixture-v1" },
    ]);
    expect(service.list("00000000-0000-4000-8000-000000000712", "text_template")).toEqual([]);
    service.close();
  });
});
