SPB Git

Repository Public

sdk-swift.test.ts 4.2 KB 72 lines typescript

1// Paquet Swift (packages/swift-example) : `swift test` — tests unitaires + test réel optionnel — et l'exécutable d'exemple, lancés2// contre le vrai serveur démarré ici. Ce fichier prépare boîtes, clés et modèle, passe le tout par l'environnement, puis purge.3import assert from "node:assert/strict"4import { spawn } from "node:child_process"5import path from "node:path"6import { after, before, test } from "node:test"7import { Client, EXTERNAL_ADDR, ORIGIN, startServer, stopServer } from "../harness"8import { mkKey, mkMailbox, purgeAll, setupAdmin, type Box } from "./v1-helpers"910const PKG = path.resolve(import.meta.dirname, "../../packages/swift-example")11let admin: Client12let A: Box, B: Box, X: Box13const env: Record<string, string> = {}1415function run(args: string[], extra: Record<string, string>) {16  return new Promise<{ code: number | null; out: string }>((resolve) => {17    const p = spawn("swift", args, { cwd: PKG, env: { ...process.env, ...extra } })18    let out = ""19    p.stdout.on("data", (d) => (out += d))20    p.stderr.on("data", (d) => (out += d))21    p.on("close", (code) => resolve({ code, out }))22  })23}2425before(async () => {26  await startServer()27  admin = await setupAdmin()28  ;[A, B, X] = await Promise.all([mkMailbox(admin, "wa"), mkMailbox(admin, "wb"), mkMailbox(admin, "ext")])29  assert.equal(X.address, EXTERNAL_ADDR)30  const tpl = (await admin.call("POST", "/api/templates", { name: "Code Swift", subject: "Votre code, {{first_name}}",31    html: `<!DOCTYPE html><html><head><meta charset="utf-8"></head><body><p>Bonjour {{first_name}}, votre code est <b>{{code}}</b>.</p></body></html>`, text: "Bonjour {{first_name}}, code {{code}}",32    variables: [{ name: "first_name", example: "Zoé", required: true }, { name: "code", example: "000000", required: true }] })).id as string33  Object.assign(env, {34    SPMAIL_BASE_URL: ORIGIN, SPMAIL_TEST_FROM: A.address, SPMAIL_TEST_TO: B.address, SPMAIL_TEST_EXTERNAL: X.address, SPMAIL_TEST_TEMPLATE_ID: tpl,35    SPMAIL_API_KEY: (await mkKey(admin, { scopes: ["mail.send", "mail.read", "mail.search", "mail.modify", "mail.attachments.read", "mail.attachments.write"], addresses: [A.addressId, B.addressId] })).token,36    SPMAIL_API_KEY_APPROVAL: (await mkKey(admin, { scopes: ["mail.send"], addresses: [A.addressId], external: "approval" })).token,37  })38})39after(async () => {40  await purgeAll(admin)41  await stopServer()42})4344test("swift test : tests unitaires et test réel contre le vrai serveur", async () => {45  const r = await run(["test"], env)46  const totals = [...r.out.matchAll(/Executed (\d+) tests?, with (?:(\d+) tests? skipped and )?(\d+) failures?/g)].pop()47  assert.ok(totals, `sortie inattendue :\n${r.out.slice(-3000)}`)48  console.log(`swift test: ${totals[0]}`)49  assert.equal(r.code, 0, r.out.slice(-6000))50  assert.equal(Number(totals[3]), 0)51  assert.equal(totals[2], undefined, "le test réel ne doit pas être sauté")52  assert.ok(Number(totals[1]) >= 8)53  assert.match(r.out, /testSendListGetReplyMarkReadUploadAgainstARealServer\]' passed/)54  assert.ok(!r.out.includes(env.SPMAIL_API_KEY.slice(15)), "la clé apparaît dans la sortie")55})5657test("l'exécutable d'exemple : whoami, notification envoyée et remise, liste des non-lus", async () => {58  const who = await run(["run", "spmail-examples", "whoami"], env)59  assert.equal(who.code, 0, who.out.slice(-2000))60  assert.match(who.out, /scopes .*mail\.send; addresses api-wa-/)61  const notify = await run(["run", "spmail-examples", "notify", "Exemple Swift", "Corps de la notification"], { ...env, SPMAIL_FROM: A.address, SPMAIL_TO: B.address })62  assert.equal(notify.code, 0, notify.out.slice(-2000))63  assert.ok(notify.out.includes(`delivered: ${B.address} → delivered`), notify.out.slice(-500))64  const unread = await run(["run", "spmail-examples", "unread"], env)65  assert.equal(unread.code, 0, unread.out.slice(-2000))66  assert.match(unread.out, /Exemple Swift/)67  // destinataire « externe » avec la clé à politique approval : l'exemple dit clairement que rien n'est parti68  const ext = await run(["run", "spmail-examples", "notify", "Externe"], { ...env, SPMAIL_API_KEY: env.SPMAIL_API_KEY_APPROVAL, SPMAIL_FROM: A.address, SPMAIL_TO: X.address })69  assert.equal(ext.code, 0, ext.out.slice(-2000))70  assert.match(ext.out, /Not sent: waiting for the owner's approval/)71})72
Files main
Menu

Theme