210 lines · 8.8 KB
import { afterEach, beforeEach, describe, expect, mock, test } from "bun:test";
import { readFileSync } from "node:fs";
import { fakeD1 } from "../metering/fake-d1.test-helper";

let d1 = fakeD1();
const kvStore = new Map<string, string>();
const fakeEnv: Record<string, unknown> = {
  get DB() {
    return d1.binding;
  },
  SECRETS_KEY: "unit-test-secrets-key-0123456789abcdef",
  PUBLIC_URL: "https://forge.test",
  INTEGRATIONS_ALLOW_LOOPBACK: "false",
  MERGEFIX_GATEWAY_ID: "default",
  KV: {
    get: async (key: string) => kvStore.get(key) ?? null,
    put: async (key: string, value: string) => void kvStore.set(key, value),
  },
};
mock.module("cloudflare:workers", () => ({ env: fakeEnv, waitUntil: () => undefined }));
const { sharedScopes } = await import("../test-shared-scopes");
mock.module("../scoped-db", () => ({
  accountDb: (id: string) => sharedScopes(d1.db).accountDb(id),
  repoDb: (id: string) => sharedScopes(d1.db).repoDb(id),
  stmt: (sql: string, ...params: (string | number | null)[]) => ({ sql, params }),
}));

const {
  validateEndpoint,
  saveEndpoint,
  endpointSummary,
  resolveAiEndpoint,
  resolveAiEndpointSummary,
  deleteEndpoint,
  resealLegacyEndpoints,
} = await import("./endpoint");
const { chatCompletion, endpointUrl } = await import("../mergefix/gateway");

const migration = readFileSync(new URL("../../../migrations/0032_ai_endpoints.sql", import.meta.url), "utf8");
const capMigration = readFileSync(new URL("../../../migrations/0037_byok_token_cap.sql", import.meta.url), "utf8");