import { randomUUID } from "node:crypto";

import { describe, expect, it } from "vitest";

import { storageCapacityErrorDetails } from "../../apps/api/src/storage-policy.js";
import { createErrorEnvelope, isErrorEnvelope } from "../../packages/shared-contracts/src/index.js";

describe("TDD-WP0-STO-002-over-limit API envelope", () => {
  it.each(["reference", "generated", "export", "sticker_original", "sticker_thumbnail"])(
    "returns stable 507 details for %s",
    (kind) => {
      const envelope = createErrorEnvelope({
        code: "STORAGE_CAPACITY_EXCEEDED",
        correlationId: randomUUID(),
        details: storageCapacityErrorDetails({
          activeReservationBytes: 0,
          managedContentBytes: 5_368_709_119,
          projectedWriteBytes: 2,
        }),
      });
      expect(isErrorEnvelope(envelope)).toBe(true);
      expect(envelope.error).toMatchObject({
        code: "STORAGE_CAPACITY_EXCEEDED",
        details: { capacity_status: "critical", remaining_bytes: 1 },
        message_key: "STORAGE_CAPACITY_EXCEEDED",
      });
      expect(kind).toBeTruthy();
    },
  );
});
