SPB Git

Repository Public

ai-live.test.ts 10.4 KB 123 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Note SPBoucher AI8 * File             : tests/unit/ai-live.test.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/note-spboucher-ai11 * Project Website  : https://note.spboucher.ai12 * Created          : 2026-11-1913 * Last Modified    : 2026-11-1914 *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: contact@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 */4243/**44 * OPT-IN live checks against the real providers (costs a fraction of a cent; never part of a normal `pnpm test`):45 *   AI_LIVE=1 node_modules/.bin/tsx --test --test-force-exit tests/unit/ai-live.test.ts46 *  1. one real end-to-end transcription: speech synthesized on the node (`say` + `afconvert`), stored as an attachment, queued, run by47 *     the job handler with the discovered OpenAI transcription model, read back — the words and the segment timestamps are asserted;48 *  2. the strict-JSON paths on a cheap model (claude-haiku-5-5): Smart Inbox suggestions and a `classify` database property.49 * What is asserted is what was actually returned; the measured numbers are printed, and the spend is read from `ai_usage`.50 */51import "../helpers/testdb";52import { test, before, after } from "node:test";53import assert from "node:assert/strict";54import { execFileSync } from "node:child_process";55import fs from "node:fs";56import os from "node:os";57import path from "node:path";58import { and, eq, gte, inArray, sql as dsql } from "drizzle-orm";59import { db, schema, sql } from "../../src/db/index";60import { storeFile } from "../../src/lib/files/storage";61import { createPage } from "../../src/lib/pages/service";62import { capture } from "../../src/lib/inbox/service";63import { ensureDatabase, createProperty, createRow } from "../../src/lib/databases/service";64import { requestTranscription, getTranscript, listTranscriptionModels, pickTranscriptionModel, timestampSupport } from "../../src/lib/ai/transcribe";65import { handleTranscribe } from "../../src/worker/handlers/transcribe";66import { suggest } from "../../src/lib/ai/inbox";67import { setAiConfig, queueAiProperty, listRuns } from "../../src/lib/ai/properties";68import { handleAiProperty } from "../../src/worker/handlers/ai-property";6970const LIVE = process.env.AI_LIVE === "1"; const opts = { skip: LIVE ? false : "set AI_LIVE=1 to call the real providers" };71const TAG = `live${Date.now().toString(36)}`; const t0 = new Date(); let ws = ""; const made: string[] = []; const atts: string[] = []; const rows: string[] = [];72before(async () => { if (!LIVE) return; const [w] = await db.select().from(schema.workspaces).limit(1); assert.ok(w); ws = w.id; });73after(async () => {74  if (LIVE) {75    const use = await db.select({ task: schema.aiUsage.task, model: schema.aiUsage.model, n: dsql<number>`count(*)::int`, cost: dsql<number>`coalesce(sum(cost_usd),0)::float8`, unpriced: dsql<number>`count(*) filter (where cost_usd is null)::int` }).from(schema.aiUsage).where(gte(schema.aiUsage.createdAt, t0)).groupBy(schema.aiUsage.task, schema.aiUsage.model);76    console.log("# ai_usage of this run:", JSON.stringify(use), "total USD (priced calls):", use.reduce((a, u) => a + Number(u.cost), 0).toFixed(6));77    if (atts.length) { await sql`delete from pgboss.job where name = 'transcribe' and data->>'attachmentId' in ${sql(atts)}`.catch(() => {}); await db.delete(schema.attachments).where(inArray(schema.attachments.id, atts)); }78    if (rows.length) await sql`delete from pgboss.job where name = 'ai-property' and data->>'rowId' in ${sql(rows)}`.catch(() => {});79    if (made.length) { await db.delete(schema.pages).where(inArray(schema.pages.parentId, made)); await db.delete(schema.pages).where(inArray(schema.pages.id, made)); }80  }81  await sql.end();82});8384test("LIVE transcription: synthesized speech → attachment → job → transcript with segment timestamps, engine and model reported", opts, async () => {85  assert.equal(os.platform(), "darwin", "the audio is synthesized with macOS `say`");86  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "spn-voice-")); const aiff = path.join(dir, "v.aiff"); const m4a = path.join(dir, "v.m4a");87  const said = "The meeting is on Monday morning. We keep the fixed rate for five years. Please send the signed documents to the bank.";88  execFileSync("say", ["-o", aiff, said]); execFileSync("afconvert", ["-f", "m4af", "-d", "aac", aiff, m4a]);89  const buf = fs.readFileSync(m4a); const st = await storeFile(buf, `voice-${TAG}.m4a`);90  const [a] = await db.insert(schema.attachments).values({ workspaceId: ws, filename: `voice-${TAG}.m4a`, mime: "audio/mp4", size: buf.length, sha256: st.sha256, storagePath: st.storagePath }).returning(); atts.push(a.id);91  const exposed = await listTranscriptionModels(true); const chosen = pickTranscriptionModel(exposed);92  console.log("# transcription models exposed by the account:", exposed.join(", "), "→ chosen:", chosen, `(${timestampSupport(chosen ?? "")})`);93  assert.ok(chosen, "the account exposes a transcription model");94  const q = await requestTranscription(ws, a.id); assert.equal(q.queued, true); assert.equal(q.status, "pending");95  const started = Date.now(); await handleTranscribe({ attachmentId: a.id, workspaceId: ws }); const ms = Date.now() - started;96  const t = await getTranscript(ws, a.id);97  console.log("# measured:", JSON.stringify({ bytes: buf.length, ms, status: t.status, engine: t.engine, model: t.model, language: t.language, duration: t.duration, segments: t.transcript.length, transcript: t.transcript, error: t.error }));98  assert.equal(t.status, "done", t.error ?? ""); assert.equal(t.engine, "openai"); assert.equal(t.model, chosen);99  const heard = t.text.toLowerCase(); for (const w of ["monday", "fixed rate", "bank"]) assert.ok(heard.includes(w), `"${w}" was transcribed (got: ${t.text})`);100  assert.ok(t.transcript.length >= 1); assert.ok(t.transcript[0].t >= 0); assert.ok(t.transcript.every((s, i, all) => i === 0 || s.t >= all[i - 1].t), "segment times are non-decreasing");101  if (timestampSupport(chosen!) !== "none") { assert.equal(t.segments, true); assert.ok(t.duration! > 3 && t.duration! < 30, `plausible duration (${t.duration})`); assert.ok(t.transcript.at(-1)!.t < t.duration!); }102  const [u] = await db.select().from(schema.aiUsage).where(and(eq(schema.aiUsage.task, "transcribe"), eq(schema.aiUsage.refId, a.id))); assert.ok(u && u.ok, "the call is in the usage log"); assert.equal(u.model, chosen);103});104105test("LIVE strict JSON on claude-haiku-5-5: Smart Inbox suggestions validate; a classify property returns one of its choices", opts, async () => {106  const proj = await createPage(ws, { title: `Rénovation cuisine ${TAG}`, section: "projects", content: { type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: "Budget, soumissions et échéancier de la rénovation de la cuisine." }] }] } }); made.push(proj.id);107  const cap = await capture(ws, { kind: "note", text: `soumission plombier ${TAG}\nLe plombier propose 4 200 $ pour déplacer l'évier ; réponse attendue avant le 5 décembre.` }); made.push(cap.id);108  const r = await suggest(ws, cap.id, { model: "claude-haiku-5-5" });109  console.log("# inbox suggestions:", JSON.stringify({ model: r.model, costUsd: r.costUsd, suggestions: r.suggestions }));110  assert.equal(r.status, "ready"); assert.equal(r.ai, true); assert.equal(r.model, "claude-haiku-5-5"); assert.ok(typeof r.suggestions.summary === "string" && r.suggestions.summary.length > 10);111  assert.ok(r.suggestions.tags.every((t) => t.name === t.name.toLowerCase() && typeof t.existing === "boolean"));112  for (const rel of r.suggestions.relations) assert.equal((await db.select().from(schema.pages).where(eq(schema.pages.id, rel.pageId))).length, 1, "a suggested relation is a real page");113  if (r.suggestions.destination?.parentId) assert.equal((await db.select().from(schema.pages).where(eq(schema.pages.id, r.suggestions.destination.parentId))).length, 1, "a suggested destination is a real page");114  const host = await createPage(ws, { title: `Base live ${TAG}`, type: "database", section: "databases" }); made.push(host.id); const d = await ensureDatabase(ws, host.id);115  const prop = await createProperty(ws, d.id, { name: "Urgence", type: "ai" }); const note = await createProperty(ws, d.id, { name: "Note", type: "text" });116  await setAiConfig(ws, d.id, prop.id, { task: "classify", choices: ["Urgent", "Peut attendre"], model: "claude-haiku-5-5" });117  const row = await createRow(ws, d.id, { title: "Fuite d'eau sous l'évier", values: { [note.id]: "L'eau coule sur le plancher depuis ce matin." } }); rows.push(row.id);118  const q = await queueAiProperty(ws, d.id, row.id, prop.id); await handleAiProperty({ rowId: row.id, propertyId: prop.id, workspaceId: ws, runId: q.runId });119  const [run] = await listRuns(ws, d.id, { rowId: row.id }); const value = (await db.select({ v: schema.databaseRows.values }).from(schema.databaseRows).where(eq(schema.databaseRows.id, row.id)))[0].v[prop.id];120  console.log("# classify:", JSON.stringify({ value, status: run.status, model: run.model, inputTokens: run.inputTokens, outputTokens: run.outputTokens, costUsd: run.costUsd, error: run.error }));121  assert.equal(run.status, "done", run.error ?? ""); assert.ok(["Urgent", "Peut attendre"].includes(value as string), `one of the choices (got ${JSON.stringify(value)})`); assert.equal(run.model, "claude-haiku-5-5");122});123
Files main
Menu

Theme