import test from "node:test";
import assert from "node:assert/strict";
import { selectAttachmentUploads } from "./attachment-policy.js";

test("attachment selection keeps successful files while enforcing count and total size", () => {
  const mib = 1024 * 1024;
  const current = [{ size: 4 * mib }];
  const files = [
    { name: "first.png", size: 8 * mib },
    { name: "too-large.png", size: (8 * mib) + 1 },
    { name: "second.png", size: 8 * mib },
    { name: "over-total.png", size: 13 * mib }
  ];

  const result = selectAttachmentUploads(current, files);

  assert.deepEqual(result.accepted.map((file) => file.name), ["first.png", "second.png"]);
  assert.deepEqual(result.rejected.map((file) => file.name), ["too-large.png", "over-total.png"]);
});

test("attachment selection fills only the remaining slots", () => {
  const current = Array.from({ length: 7 }, () => ({ size: 1 }));
  const files = [{ name: "kept.png", size: 1 }, { name: "rejected.png", size: 1 }];

  const result = selectAttachmentUploads(current, files);

  assert.deepEqual(result.accepted.map((file) => file.name), ["kept.png"]);
  assert.deepEqual(result.rejected.map((file) => file.name), ["rejected.png"]);
});
