146 lines · 5.8 KB
import { beforeEach, describe, expect, test } from "bun:test";
import { meterDef } from "~/lib/meters";
import type { MeterId } from "~/lib/meters";
const {
  accountSql,
  fakeEnv,
  quantity,
  resetMeteringHarness,
  recordUsage,
  claimScratchUsage,
  MeteringError,
  reserveUsage,
  commitUsage,
  releaseUsage,
  assertWithinQuota,
  QuotaExceededError,
  setPlan,
  clearPlanOverride,
  setSpendingLimit,
  withinSpendingLimit,
  billingSettings,
  scratchSiteAccount,
  countingBody,
} = await import("./metering.test-helper");

beforeEach(() => resetMeteringHarness());

describe("hardening", () => {
  test("a scratch account cannot be claimed by a second account", async () => {
    const scratch = scratchSiteAccount("s2");
    await claimScratchUsage(scratch, "u1");
    await expect(claimScratchUsage(scratch, "u2")).rejects.toBeInstanceOf(MeteringError);
  });

  test("claiming carries in-flight holds so later releases don't undercount", async () => {
    const scratch = scratchSiteAccount("s3");
    const hold = await reserveUsage({ id: "h1", accountId: scratch, meter: "compute.site_requests", quantity: 5 });
    await claimScratchUsage(scratch, "u1");
    await releaseUsage(hold.id, scratch);
    const row = accountSql("u1")