import assert from "node:assert/strict";
import test from "node:test";

const { buildFilesForSubmit } = await import(
  new URL("./submit-files.ts", import.meta.url).href
);
const { buildPriorityHintText, composeSubmitText } = await import(
  new URL("./priority-hint.ts", import.meta.url).href
);

void test("buildFilesForSubmit keeps uploads and appends valid references", () => {
  const result = buildFilesForSubmit(
    [
      {
        filename: "uploaded.md",
        size: 12,
        virtual_path: "/mnt/user-data/uploads/uploaded.md",
      },
    ],
    [
      {
        filename: "artifact.md",
        path: "/mnt/user-data/artifacts/artifact.md",
        ref_kind: "mention",
        ref_source: "artifact",
      },
    ],
  );

  assert.equal(result.staleCount, 0);
  assert.equal(result.files.length, 2);
  assert.equal(result.files[0]?.filename, "uploaded.md");
  assert.equal(result.files[1]?.ref_kind, "mention");
  assert.equal(result.files[1]?.ref_source, "artifact");
});

void test("buildFilesForSubmit drops stale references without blocking submit", () => {
  const result = buildFilesForSubmit(
    [],
    [
      {
        filename: "stale.md",
        path: "/stale.md",
        ref_kind: "mention",
        ref_source: "upload",
        stale: true,
      },
    ],
  );

  assert.equal(result.staleCount, 1);
  assert.equal(result.files.length, 0);
});

void test("buildFilesForSubmit keeps artifact mention path without re-upload", () => {
  const result = buildFilesForSubmit(
    [],
    [
      {
        filename: "image.png",
        path: "/mnt/user-data/artifacts/image.png",
        ref_kind: "mention",
        ref_source: "artifact",
      },
    ],
  );

  assert.equal(result.staleCount, 0);
  assert.equal(result.files.length, 1);
  assert.equal(result.files[0]?.path, "/mnt/user-data/artifacts/image.png");
  assert.equal(result.files[0]?.ref_source, "artifact");
});

void test("buildPriorityHintText keeps attachments first and skills second", () => {
  const result = buildPriorityHintText({
    attachmentNames: ["spec.md"],
    skillIds: ["skill.docs.generate"],
  });
  assert.equal(result, "XClaw优先使用【spec.md】和【skill.docs.generate】");
});

void test("buildPriorityHintText outputs single category when the other is empty", () => {
  assert.equal(
    buildPriorityHintText({
      attachmentNames: ["spec.md"],
      skillIds: [],
    }),
    "XClaw优先使用【spec.md】",
  );
  assert.equal(
    buildPriorityHintText({
      attachmentNames: [],
      skillIds: ["skill.docs.generate"],
    }),
    "XClaw优先使用【skill.docs.generate】",
  );
});

void test("buildPriorityHintText deduplicates case-insensitively", () => {
  const result = buildPriorityHintText({
    attachmentNames: ["Spec.md", "spec.md", " SPEC.md "],
    skillIds: ["skill.excel", "SKILL.EXCEL"],
  });
  assert.equal(result, "XClaw优先使用【Spec.md】和【skill.excel】");
});

void test("composeSubmitText appends hint only when needed", () => {
  assert.equal(
    composeSubmitText({
      baseText: "请总结",
      attachmentNames: ["spec.md"],
      skillIds: [],
    }),
    "请总结\nXClaw优先使用【spec.md】",
  );

  assert.equal(
    composeSubmitText({
      baseText: "请总结",
      attachmentNames: [],
      skillIds: [],
    }),
    "请总结",
  );
});
