273 lines · 10.7 KB
import { beforeEach, describe, expect, mock, test } from "bun:test";
import { Database } from "bun:sqlite";
import { readFileSync } from "node:fs";
import { applyCombinedSchema } from "../test-schema.test-helper";
import type { RepoCardData } from "~/lib/og";
import type { RepoRow } from "~/lib/types";
import { loadRuntimeFromDisk, pngSize } from "./disk-runtime";

mock.module("cloudflare:workers", () => ({
  env: {
    PUBLIC_URL: "https://forge.example.com",
    BUCKET: { get: async () => null },
    IMAGES: {},
  },
  waitUntil: () => undefined,
  WorkflowEntrypoint: class {},
  DurableObject: class {},
  WorkerEntrypoint: class {},
}));
mock.module("@tanstack/react-start/server", () => ({ getRequestHeader: () => undefined }));

const db = new Database(":memory:");
applyCombinedSchema(db);
for (const migration of ["0064_repo_topics.sql", "0065_repo_languages.sql"]) {
  db.exec(readFileSync(new URL(`../../../migrations/${migration}`, import.meta.url), "utf8"));
}
mock.module("../db", () => ({
  first: async (sql: string, ...params: unknown[]) => db.query(sql).get(...(params as never[])) ?? null,
  all: async (sql: string, ...params: unknown[]) => db.query(sql).all(...(params as never[])),
  run: async () => ({ meta: { changes: 0 } }),
  batch: async () => [],
  stmt: (sql: string, ...params: unknown[]) => ({ sql, params }),
  placeholders: (count: number) => Array(count).fill("?").join(", "),
  chunks: <T>(items: T[]) => [items],
  MAX_IN_LIST: 90,
}));

let renders = 0;
let limited = false;
mock.module("../limits", () => ({