SPB Git

Repository Public

helpers.ts 4.9 KB 100 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : ORIX App Universe8 * File             : tests/helpers.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website  : https://www.orixlab.dev12 * Created          : 2026-11-2113 * Last Modified    : 2026-11-2114 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { mkdtempSync, rmSync, readFileSync, readdirSync } from "node:fs";44import { tmpdir } from "node:os";45import { join } from "node:path";46import { vi } from "vitest";47import type { AppDoc } from "@/models/app";4849/** A throw-away data directory + database per test file. */50export function freshData(): { dir: string; cleanup: () => Promise<void> } {51  const dir = mkdtempSync(join(tmpdir(), "orix-test-"));52  process.env.ORIX_DATA_DIR = dir;53  process.env.APP_URL = "https://orix.test";54  process.env.SPBGIT_URL = "https://git.test";55  return { dir, cleanup: async () => { (await import("@/lib/database/db")).closeDb(); rmSync(dir, { recursive: true, force: true }); } };56}5758export const seedDocs = (): AppDoc[] => readdirSync("content/apps").filter((f) => f.endsWith(".json")).map((f) => JSON.parse(readFileSync(join("content/apps", f), "utf8")) as AppDoc);5960export function macApp(over: Partial<AppDoc> = {}): AppDoc {61  const l = (s: string) => ({ en: s, fr: s });62  return {63    slug: "demo", name: "ORIX Demo", order: 50, category: "system", platforms: ["macos"], status: "available", accent: "#4C8DFF", icon: null,64    repo: { name: "orix-demo", visibility: "public" }, bundleId: "ai.spboucher.orix.demo", version: "1.2.0", build: "7", minOS: { macos: "15.0" }, architectures: ["arm64"], downloadSize: null,65    tagline: l("Demo"), summary: l("A demo application."), description: l("Demo."), features: [], deepDives: [], specs: { offline: l("Yes"), permissions: [], languages: ["en"] },66    privacy: { summary: l("Nothing leaves the Mac."), points: [] }, faq: [], screenshots: [], distribution: [{ channel: "dmg", state: "none", url: null }], verification: { testedOnDevice: true, notes: l("ok") }, releases: [], ...over,67  };68}6970/** next/headers cookies() backed by a plain Map, so session and WebAuthn code runs outside a request. */71export function mockCookies() {72  const jar = new Map<string, string>();73  vi.doMock("next/headers", () => ({74    cookies: async () => ({75      get: (k: string) => (jar.has(k) ? { name: k, value: jar.get(k)! } : undefined),76      set: (k: string, v: string, o?: { maxAge?: number }) => { if (o?.maxAge === 0 || v === "") jar.delete(k); else jar.set(k, v); },77      delete: (k: string) => { jar.delete(k); },78    }),79  }));80  return jar;81}8283/** A fake SPB Git forge: JSON API + release files, with switches for failures. */84export function fakeForge(state: { releases: Record<string, unknown[]>; files?: Record<string, Buffer>; fail?: number; status?: number }) {85  const calls: string[] = [];86  const fetchImpl = async (url: string): Promise<Response> => {87    calls.push(url);88    if (state.fail && state.fail-- > 0) return new Response("boom", { status: state.status ?? 503 });89    const u = new URL(url);90    const api = /^\/api\/v1\/repos\/([^/]+)(\/releases)?$/.exec(u.pathname);91    if (api) {92      if (!(api[1] in state.releases)) return Response.json({ error: "not_found" }, { status: 404 });93      return api[2] ? Response.json({ releases: state.releases[api[1]] }) : Response.json({ name: api[1], description: "", head: "abc123", lastPush: "2026-11-20T12:00:00-05:00", commitCount: 3, tagCount: 1 });94    }95    const file = state.files?.[u.pathname];96    return file ? new Response(new Uint8Array(file)) : new Response("nope", { status: 404 });97  };98  return { fetchImpl, calls };99}100
Files main
Menu

Theme