SPB Git

Repository Public

global-setup.ts 3.9 KB 96 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Identity8 * File             : sdk/typescript/tests/integration/global-setup.ts9 * Module           : sdk10 * Repository       : https://git.spboucher.ai/maclustr-identity11 * Project Website  : https://identity.spboucher.ai12 * Created          : 2026-12-0113 * Last Modified    : 2026-12-0114 *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: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142/**43 * Provisions two sandbox applications and their test keys on the LOCAL backend through its CLI.44 * Set MLID_SECRET_KEY and MLID_SECRET_KEY_B to skip provisioning. Never point this at production.45 */46import { execFileSync } from "node:child_process";47import { existsSync } from "node:fs";48import { resolve } from "node:path";49import type { GlobalSetupContext } from "vitest/node";5051declare module "vitest" {52  export interface ProvidedContext {53    mlid: { baseUrl: string; keyA: string; keyB: string } | null;54  }55}5657export default async function setup({ provide }: GlobalSetupContext): Promise<void> {58  if (process.env.MLID_SKIP_INTEGRATION === "1") {59    provide("mlid", null);60    return;61  }62  const baseUrl = process.env.MLID_BASE_URL ?? "http://localhost:8870";63  if (!/^http:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/.test(baseUrl)) throw new Error("Integration tests only run against a local backend.");64  try {65    const r = await fetch(`${baseUrl}/health`);66    if (!r.ok) throw new Error(String(r.status));67  } catch {68    console.warn(`\n[integration] No backend at ${baseUrl}: integration tests are skipped.\n`);69    provide("mlid", null);70    return;71  }72  let keyA = process.env.MLID_SECRET_KEY;73  let keyB = process.env.MLID_SECRET_KEY_B;74  if (!keyA || !keyB) {75    const repo = resolve(__dirname, "../../../..");76    const python = resolve(repo, ".venv/bin/python");77    if (!existsSync(python)) throw new Error(`Backend virtualenv not found at ${python}; set MLID_SECRET_KEY and MLID_SECRET_KEY_B instead.`);78    const env = {79      ...process.env,80      DATABASE_URL: process.env.DATABASE_URL ?? "postgres://127.0.0.1/maclustr_identity_sdk",81      MLID_DATA_DIR: process.env.MLID_DATA_DIR ?? "/tmp/mlid-sdk/data",82      MLID_MASTER_KEY_FILE: process.env.MLID_MASTER_KEY_FILE ?? "/tmp/mlid-sdk/master.key",83    };84    const cli = (...args: string[]) => execFileSync(python, ["-m", "services.mlid.cli", ...args], { cwd: repo, env, encoding: "utf8" }).trim();85    const run = Date.now().toString(36);86    const make = (suffix: string) => {87      const slug = `sdk-ts-${suffix}-${run}`;88      cli("app-create", slug, `TS SDK integration ${suffix.toUpperCase()}`);89      return cli("key-create", slug).split("\n").pop() as string;90    };91    keyA = make("a");92    keyB = make("b");93  }94  provide("mlid", { baseUrl, keyA, keyB });95}96
Files main
Menu

Theme