209 lines · 8.5 KB
/**
 * End-to-end check for usage metering against a live dev server running this worktree (default
 * http://localhost:3000, override with MCP_BASE_URL). It seeds a user and repo, then exercises git clone,
 * push and fetch, LFS upload and download, a raw and an archive download, and a mergefix AI run (the
 * server must use MERGEFIX_PROVIDER=mock). It then checks that the usage API and the billing page report
 * the same numbers, and that a replayed git push never double counts.
 *
 *   MCP_BASE_URL=http://localhost:3191 bun test ./e2e/metering.test.ts
 */
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
import { randomBytes } from "node:crypto";
import { mkdtempSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { api, BASE, body, seedWorld, sql, sqlRows, teardown } from "./mcp-helpers";
import type { World } from "./mcp-helpers";

type Meter = { meter: string; quantity: number };
type Summary = {
  meters: Meter[];
  repos: { name: string | null; meter: string; quantity: number }[];
  totalCents: number;
};

let world: World;
let work: string;
const TIMEOUT = 180_000;

function git(args: string[], cwd = work): string {
  const result = Bun.spawnSync(["git", "-c", "credential.helper=", ...args], {
    cwd,
    stdout: "pipe",
    stderr: "pipe",
    env: {
      ...process.env,
      GIT_TERMINAL_PROMPT: "0",
      GIT_LFS_SKIP_SMUDGE: "0",
    },
  });
  if (result.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${result.stderr.toString()}`);