import test from "node:test";
import assert from "node:assert/strict";
import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createProjectPreferencesStore } from "./project-preferences.js";

test("project hides persist as safe keys and timestamps", async () => {
  const dir = await mkdtemp(join(tmpdir(), "project-preferences-"));
  try {
    const store = createProjectPreferencesStore(dir);
    const hidden = await store.hide("workspace-a", new Date("2026-07-14T01:00:00.000Z"));

    assert.deepEqual(hidden, { projectKey: "workspace-a", hiddenAt: "2026-07-14T01:00:00.000Z" });
    assert.deepEqual(await store.hiddenProjects(), [hidden]);
  } finally {
    await rm(dir, { recursive: true, force: true });
  }
});

test("project hides reject path-like and invalid keys", async () => {
  const store = createProjectPreferencesStore("");
  await assert.rejects(store.hide("C:\\Private\\Project"), /invalid_project_key/);
  await assert.rejects(store.hide(""), /invalid_project_key/);
});
