SPB Git

Repository Public

next-rules.test.ts 28.6 KB 355 lines typescript

1// SP Mail Next — moteur de règles sur du vrai courrier (B → A sur stg.spboucher.ai) : étiquette, drapeau, épingle, lu, mise en veille,2// rappel, événement interne (webhook réel), brouillon de réponse (extrait ou IA), extraction ; arrêt (« stop »), portée par boîte,3// essai à blanc, idempotence à travers un redémarrage — et la garantie centrale : une règle ne produit JAMAIS de message sortant.4import assert from "node:assert/strict"5import http from "node:http"6import { after, before, test } from "node:test"7import { ApiError, ApiKey, Client, Receiver, cli, haltServer, reauth, restartServer, sleep, sql, startServerWith, stopServer, tokenOf, until } from "../harness"8import { mkKey, mkMailbox, purgeAll, sendAndWait, sess, setupAdmin, type Box } from "./v1-helpers"910// ------- faux fournisseur local : suggestions de réponse, tri, extraction11const llmSeen: any[] = []12const llm = http.createServer((req, res) => {13  let raw = ""14  req.on("data", (d) => (raw += d))15  req.on("end", () => {16    const body = raw ? JSON.parse(raw) : null17    llmSeen.push({ path: req.url, body })18    if (!String(req.url).endsWith("/chat/completions")) { res.writeHead(404); return res.end("{}") }19    const system = body.messages[0].content as string, user = body.messages[1].content as string20    const content = /draft three alternative replies/.test(system) ? JSON.stringify({ suggestions: [1, 2, 3].map((n) => ({ subject: null, text: `Réponse proposée ${n}` })) })21      : /Task: classify each/.test(system) ? JSON.stringify({ items: [...user.matchAll(/E-mail (\d+):/g)].map((m) => ({ n: Number(m[1]), category: "transaction", needs_action: false, important: false, reason: "Facture." })) })22      : /Task: extract the fields/.test(system) ? JSON.stringify({ amount: 142.18, due: "2026-12-05", paid: null })23      : "texte"24    res.writeHead(200, { "content-type": "application/json" })25    res.end(JSON.stringify({ model: body.model, choices: [{ index: 0, message: { role: "assistant", content }, finish_reason: "stop" }], usage: { prompt_tokens: 50, completion_tokens: 10 } }))26  })27})2829let ENV: Record<string, string> = {}30let admin: Client, member: Client31let A: Box, B: Box32let key: ApiKey33let acctA = "", acctB = ""34let folders: Record<string, string> = {}35const rx = new Receiver()36let label: any, snippet: any37const rules: Record<string, any> = {}38const haveSnooze = async () => typeof (await import("../../server/src/next/snooze")).snoozeMessages === "function"3940async function rejects(p: Promise<unknown>, status: number, code?: string) {41  await assert.rejects(p, (e: unknown) => e instanceof ApiError && e.status === status && (!code || e.code === code), `attendu ${status} ${code ?? ""}`)42}43const acctOf = async (mb: string) => (await admin.call("GET", `/api/jmap/${mb}/session`)).primaryAccounts["urn:ietf:params:jmap:mail"] as string44async function list(mb: string, acct: string, extra: string[] = []): Promise<any[]> {45  const r = await admin.jmap(mb, [["Email/query", { accountId: acct, limit: 500 }, "q"], ["Email/get", { accountId: acct, "#ids": { resultOf: "q", name: "Email/query", path: "/ids" }, properties: ["threadId", "mailboxIds", "keywords", "subject", "from", "to", "messageId", ...extra] }, "g"]])46  return r[1][1].list as any[]47}48const inA = async (subject: string) => (await list(A.id, acctA)).find((e) => e.subject === subject)49const send = (subject: string, text = "Bonjour, voici le message.") => sendAndWait(key, { from: B.address, to: [A.address], subject, text })50const runsOf = (ruleId: string) => sql`select * from rule_runs where rule_id = ${ruleId} order by id`51/** Attend l'exécution terminée de la règle pour le message de ce sujet ; rend le message (relu après les actions) et l'exécution. */52async function ran(ruleId: string, subject: string) {53  const e = await until(() => inA(subject), `arrivée de « ${subject} »`)54  const run = await until(async () => (await sql`select * from rule_runs where rule_id = ${ruleId} and email_id = ${e.id} and status <> 'running'`)[0], `exécution de la règle pour « ${subject} »`)55  return { email: (await inA(subject))!, run }56}57const mk = async (name: string, body: Record<string, unknown>) => (rules[name] = await admin.call("POST", "/api/next/rules", { name, ...body }))58const hits = (pred: (d: any) => boolean) => rx.hits.filter((h) => h.json?.type === "rule.matched" && pred(h.json.data))59/** Tout ce qui prouverait un envoi : lignes d'envoi de A, soumissions JMAP de A, courrier arrivé chez B. */60async function outboundEvidence() {61  const sub = await admin.jmap(A.id, [["EmailSubmission/query", { accountId: acctA }, "s"]])62  return {63    outbound_from_a: (await sql`select count(*)::int as n from outbound_messages where mailbox_id = ${A.id}`)[0].n,64    submissions_a: sub[0][1].ids.length,65    b_inbox: (await list(B.id, acctB)).filter((e) => (e.from ?? []).some((f: any) => f.email === A.address)).length,66  }67}6869before(async () => {70  await new Promise<void>((r) => llm.listen(0, "127.0.0.1", () => r()))71  ENV = { SPMAIL_AI_LOCAL_URL: `http://127.0.0.1:${(llm.address() as { port: number }).port}/v1`, SPMAIL_AI_RATE_PER_MINUTE: "1000", SPMAIL_OPS_MONITOR_INTERVAL_MS: "3600000" }72  await startServerWith(ENV)73  admin = await setupAdmin()74  member = new Client("web")75  await member.enroll(tokenOf(await cli("invite")), "membre", "Membre")76  ;[A, B] = await Promise.all([mkMailbox(admin, "rules-a"), mkMailbox(admin, "rules-b")])77  ;[acctA, acctB] = await Promise.all([acctOf(A.id), acctOf(B.id)])78  key = await mkKey(admin, { scopes: ["mail.send", "mail.read"], addresses: [B.addressId] })79  const m = await admin.jmap(A.id, [["Mailbox/get", { accountId: acctA, properties: ["role", "name"] }, "m"]])80  folders = Object.fromEntries(m[0][1].list.map((f: any) => [f.role ?? f.name, f.id]))81  await rx.start()82  await reauth(admin)83  await admin.call("POST", "/api/admin/webhook-allowlist", { host_port: `127.0.0.1:${rx.port}`, note: "récepteur de test" })84  await sess(admin, "POST", "/webhooks", { name: "règles", url: rx.url, events: ["rule.matched"] })85  label = await admin.call("POST", "/api/next/labels", { name: "Factures", color: "#0f766e" })86  snippet = await admin.call("POST", "/api/next/snippets", { name: "Accusé", text: "Bonjour,\n\nJ'ai bien reçu votre question et je vous reviens d'ici demain.\n\nCordialement" })87})88after(async () => {89  await rx.stop()90  await purgeAll(admin).catch((e) => console.error(e))91  await stopServer()92  await new Promise((r) => llm.close(r))93})9495test("validation et cloisonnement : schéma strict, références possédées, PATCH partiel sans valeurs par défaut parasites", async () => {96  assert.deepEqual((await admin.call("GET", "/api/next/rules")).data, [])97  const star = [{ type: "star" }]98  await rejects(admin.call("POST", "/api/next/rules", { name: "x", actions: [] }), 400, "invalid_request")99  await rejects(admin.call("POST", "/api/next/rules", { name: "x", actions: star, conditions: [{ field: "size", op: "contains", value: "x" }] }), 400, "invalid_request")100  await rejects(admin.call("POST", "/api/next/rules", { name: "x", actions: [{ type: "send", to: "x@evil.test" }] }), 400, "invalid_request")101  await rejects(admin.call("POST", "/api/next/rules", { name: "x", actions: [{ type: "forward", to: B.address }] }), 400, "invalid_request")102  await rejects(admin.call("POST", "/api/next/rules", { name: "x", actions: [{ type: "draft_reply" }] }), 400, "invalid_request")103  await rejects(admin.call("POST", "/api/next/rules", { name: "x", actions: [{ type: "label", label_id: "3f2b8c1e-9a4d-4e6f-8b7a-1c2d3e4f5a6b" }] }), 400, "unknown_label")104  await rejects(admin.call("POST", "/api/next/rules", { name: "x", actions: [{ type: "draft_reply", snippet_id: "3f2b8c1e-9a4d-4e6f-8b7a-1c2d3e4f5a6b" }] }), 400, "unknown_snippet")105  await rejects(admin.call("POST", "/api/next/rules", { name: "x", actions: star, mailbox_ids: ["3f2b8c1e-9a4d-4e6f-8b7a-1c2d3e4f5a6b"] }), 400, "unknown_mailbox")106  // le membre ne peut viser ni la boîte ni l'étiquette de quelqu'un d'autre107  await rejects(member.call("POST", "/api/next/rules", { name: "x", actions: star, mailbox_ids: [A.id] }), 400, "unknown_mailbox")108  await rejects(member.call("POST", "/api/next/rules", { name: "x", actions: [{ type: "label", label_id: label.id }] }), 400, "unknown_label")109  const r = await admin.call("POST", "/api/next/rules", { name: "Temporaire", match: "any", stop: true, enabled: false, conditions: [{ field: "subject", op: "contains", value: "zzz" }], actions: star })110  assert.deepEqual([r.match, r.stop, r.enabled, r.mailbox_ids, r.position], ["any", true, false, null, 0])111  // PATCH : seules les clés envoyées changent (les valeurs par défaut du schéma ne s'appliquent pas)112  const p = await admin.call("PATCH", `/api/next/rules/${r.id}`, { name: "Renommée" })113  assert.deepEqual([p.name, p.match, p.stop, p.enabled, p.conditions.length, p.actions.length], ["Renommée", "any", true, false, 1, 1])114  await rejects(admin.call("PATCH", `/api/next/rules/${r.id}`, { actions: [{ type: "explode" }] }), 400, "invalid_request")115  // la règle d'un autre utilisateur est introuvable116  for (const [m, u, b] of [["PATCH", `/api/next/rules/${r.id}`, { name: "x" }], ["DELETE", `/api/next/rules/${r.id}`, undefined], ["GET", `/api/next/rules/${r.id}/runs`, undefined]] as const) await rejects(member.call(m, u, b), 404, "not_found")117  assert.deepEqual((await member.call("GET", "/api/next/rules")).data, [])118  // règle désactivée seule : le crochet du veilleur ne met rien en file119  await send("Sans règle active")120  const e = await until(() => inA("Sans règle active"), "arrivée")121  await until(async () => (await sql`select 1 from watched_messages where email_id = ${e.id}`).length, "vu par le veilleur")122  assert.equal((await sql`select count(*)::int as n from rule_jobs`)[0].n, 0)123  await admin.call("DELETE", `/api/next/rules/${r.id}`)124  assert.equal((await sql`select count(*)::int as n from audit_log where action like 'rule.%'`)[0].n, 3, "création, modification et suppression consignées")125})126127test("du vrai courrier déclenche étiquette, drapeau, épingle, rappel et événement ; exécution consignée", async () => {128  await mk("Factures", { conditions: [{ field: "subject", op: "contains", value: "facture" }, { field: "from", op: "matches_domain", value: "stg.spboucher.ai" }],129    actions: [{ type: "label", label_id: label.id }, { type: "star" }, { type: "pin" }, { type: "remind", after_hours: 48 }, { type: "event", name: "invoice.received" }] })130  const sent = await send("Facture de novembre", "Montant dû : 142,18 $ avant le 5 décembre.")131  const { email, run } = await ran(rules.Factures.id, "Facture de novembre")132  assert.equal(run.status, "ok", run.error)133  assert.deepEqual(run.actions.map((a: any) => [a.type, a.status]), [["label", "ok"], ["star", "ok"], ["pin", "ok"], ["remind", "ok"], ["event", "ok"]])134  assert.equal(run.error, null)135  for (const k of [label.keyword, "$flagged", "$pinned"]) assert.equal(email.keywords[k], true, k)136  assert.ok(!email.keywords.$seen, "non lu : cette règle ne marque pas comme lu")137  // rappel réel, à 48 h138  const [rem] = await sql`select * from reminders where email_id = ${email.id}`139  assert.ok(rem && rem.status === "pending" && rem.note === "Rule: Factures")140  assert.ok(Math.abs(new Date(rem.due_at).getTime() - Date.now() - 48 * 3600_000) < 120_000)141  // événement interne livré au webhook : identifiants seulement, pas de sujet sans include_content142  const hit = await until(async () => hits((d) => d.name === "invoice.received")[0], "webhook rule.matched")143  assert.deepEqual(Object.keys(hit.json.data).sort(), ["address", "from", "message_id", "name", "received_at", "rule_id", "rule_name", "thread_id"])144  assert.deepEqual([hit.json.data.rule_id, hit.json.data.rule_name, hit.json.data.from, hit.json.address], [rules.Factures.id, "Factures", [B.address], A.address])145  assert.ok(!hit.body.includes("novembre") && !hit.body.includes("142"))146  assert.match(hit.json.data.message_id, /^msg_/)147  // journal lisible par le propriétaire seulement148  const log = (await admin.call("GET", `/api/next/rules/${rules.Factures.id}/runs`)).data149  assert.equal(log.length, 1)150  assert.deepEqual([log[0].email_id, log[0].mailbox_id, log[0].status], [email.id, A.id, "ok"])151  assert.equal(log[0].actions[0].keyword, label.keyword)152  // un message qui ne correspond pas : aucune exécution, aucun mot-clé153  await send("Bonjour sans rapport")154  const other = await until(() => inA("Bonjour sans rapport"), "arrivée")155  await until(async () => (await sql`select 1 from watched_messages where email_id = ${other.id}`).length && !(await sql`select 1 from rule_jobs`).length, "file des règles vidée")156  assert.equal((await runsOf(rules.Factures.id)).length, 1)157  assert.deepEqual(Object.keys((await inA("Bonjour sans rapport")).keywords).filter((k) => k !== "$hasnoattachment"), [])158  void sent159})160161test("lu + notify_app + stop : les règles suivantes ne s'appliquent pas ; portée par boîte ; mise en veille", async () => {162  await mk("Notifications", { conditions: [{ field: "subject", op: "contains", value: "notification" }], actions: [{ type: "mark_read" }, { type: "notify_app", app: "sp-wallet" }], stop: true })163  await mk("Tout", { actions: [{ type: "event", name: "any.received" }] })164  await mk("Autre boîte", { mailbox_ids: [B.id], actions: [{ type: "star" }] })165  await mk("Plus tard", { conditions: [{ field: "subject", op: "contains", value: "plus tard" }], actions: [{ type: "snooze", until_rule: "2h" }] })166  await send("Notification de dépôt")167  const n = await ran(rules.Notifications.id, "Notification de dépôt")168  assert.equal(n.email.keywords.$seen, true)169  const app = await until(async () => hits((d) => d.app === "sp-wallet")[0], "événement notify_app")170  assert.deepEqual([app.json.data.name, app.json.data.app, app.json.data.rule_name], ["notify_app", "sp-wallet", "Notifications"])171  // « stop » : la règle fourre-tout placée après n'a pas tourné pour ce message, mais tourne pour un autre172  await send("Message ordinaire")173  const o = await ran(rules.Tout.id, "Message ordinaire")174  await sleep(1500)175  assert.equal((await sql`select count(*)::int as n from rule_runs where rule_id = ${rules.Tout.id} and email_id = ${n.email.id}`)[0].n, 0)176  assert.equal(hits((d) => d.name === "any.received" && d.message_id === app.json.data.message_id).length, 0)177  assert.equal(hits((d) => d.name === "any.received").length, 1)178  // portée : la règle limitée à la boîte B n'a touché aucun message de A179  assert.equal((await runsOf(rules["Autre boîte"].id)).length, 0)180  assert.ok(!o.email.keywords.$flagged)181  // mise en veille : vraie ligne de veille, message rangé dans « Snoozed » avec $snoozed (module voisin) — sinon l'action est consignée comme ignorée182  await send("À voir plus tard")183  const s = await ran(rules["Plus tard"].id, "À voir plus tard")184  if (await haveSnooze()) {185    assert.deepEqual(s.run.actions.map((a: any) => [a.type, a.status]), [["snooze", "ok"]])186    const [row] = await sql`select * from snoozes where email_id = ${s.email.id}`187    assert.equal(row.status, "active")188    assert.ok(Math.abs(new Date(row.until).getTime() - Date.now() - 2 * 3600_000) < 120_000)189    assert.equal(s.email.keywords.$snoozed, true)190    assert.ok(!s.email.mailboxIds[folders.inbox], "sorti de la boîte de réception")191  } else assert.deepEqual(s.run.actions.map((a: any) => [a.type, a.status, a.detail]), [["snooze", "skipped", "snooze_unavailable"]])192})193194test("draft_reply : un BROUILLON (extrait, puis IA) rattaché au fil — jamais un envoi ; extraction scellée ; IA coupée = action ignorée", async () => {195  const before = await outboundEvidence()196  await mk("Questions", { conditions: [{ field: "subject", op: "contains", value: "question" }], actions: [{ type: "draft_reply", snippet_id: snippet.id }] })197  await mk("Devis", { conditions: [{ field: "subject", op: "contains", value: "devis" }], actions: [{ type: "draft_reply", ai_tone: "concise" }, { type: "extract", schema: { fields: [{ name: "amount", type: "number", description: "total" }, { name: "due", type: "date" }, { name: "paid", type: "boolean" }] } }] })198  await send("Petite question", "Bonjour, quel est le délai de livraison ?")199  const q = await ran(rules.Questions.id, "Petite question")200  assert.deepEqual(q.run.actions.map((a: any) => [a.type, a.status, a.source]), [["draft_reply", "ok", "snippet"]])201  const drafts = async () => (await list(A.id, acctA, ["inReplyTo", "references", "textBody", "bodyValues"])).filter((e) => e.mailboxIds[folders.drafts])202  const [d] = await drafts()203  assert.equal(d.id, q.run.actions[0].draft_id)204  assert.equal(d.keywords.$draft, true)205  assert.deepEqual([d.subject, d.from.map((x: any) => x.email), d.to.map((x: any) => x.email)], ["Re: Petite question", [A.address], [B.address]])206  assert.deepEqual(d.inReplyTo, q.email.messageId)207  assert.deepEqual(d.references, q.email.messageId)208  const full = await admin.jmap(A.id, [["Email/get", { accountId: acctA, ids: [d.id], properties: ["bodyValues", "textBody"], fetchTextBodyValues: true }, "g"]])209  assert.match(Object.values<any>(full[0][1].list[0].bodyValues)[0].value, /J'ai bien reçu votre question/)210  // IA coupée : le brouillon IA et l'extraction sont consignés comme ignorés, rien d'autre ne se passe211  await send("Demande de devis 1", "Pouvez-vous me faire un devis ?")212  const off = await ran(rules.Devis.id, "Demande de devis 1")213  assert.deepEqual(off.run.actions.map((a: any) => [a.type, a.status, a.detail]), [["draft_reply", "skipped", "ai_disabled"], ["extract", "skipped", "ai_disabled"]])214  assert.equal(off.run.status, "ok")215  assert.equal(llmSeen.length, 0)216  assert.equal((await drafts()).length, 1)217  // IA activée (fournisseur local) : brouillon tiré de la première suggestion, extraction validée et scellée218  await admin.call("PUT", "/api/next/ai/settings", { provider: "local" })219  await send("Demande de devis 2", "Devis demandé : 142,18 $ à payer avant le 5 décembre.")220  const on = await ran(rules.Devis.id, "Demande de devis 2")221  assert.deepEqual(on.run.actions.map((a: any) => [a.type, a.status]), [["draft_reply", "ok"], ["extract", "ok"]])222  assert.deepEqual([on.run.actions[0].source, on.run.actions[0].provider, on.run.actions[0].model], ["ai", "local", "maclustr-qwen3"])223  const aiDraft = (await drafts()).find((e) => e.id === on.run.actions[0].draft_id)224  assert.equal(aiDraft.subject, "Re: Demande de devis 2")225  const body = await admin.jmap(A.id, [["Email/get", { accountId: acctA, ids: [aiDraft.id], properties: ["bodyValues", "textBody"], fetchTextBodyValues: true }, "g"]])226  assert.equal(Object.values<any>(body[0][1].list[0].bodyValues)[0].value.trim(), "Réponse proposée 1")227  // l'extraction n'est pas en clair en base ; elle est rendue à son propriétaire par la route du journal228  assert.ok(on.run.actions[1].result_enc && !JSON.stringify(on.run.actions).includes("142.18"))229  const log = (await admin.call("GET", `/api/next/rules/${rules.Devis.id}/runs`)).data230  assert.deepEqual(log[0].actions[1].result, { amount: 142.18, due: "2026-12-05", paid: null })231  assert.equal(log[0].actions[1].result_enc, undefined)232  // le modèle a reçu le courriel dans le bloc non fiable, sans outil233  const chats = llmSeen.filter((x) => String(x.path).endsWith("/chat/completions"))234  assert.ok(chats.length >= 2 && chats.every((x) => /UNTRUSTED DATA/.test(x.body.messages[0].content) && !("tools" in x.body)))235  // condition ai_category : évaluée par le tri (essai à blanc)236  const dry = await admin.call("POST", "/api/next/rules/test", { rule: { conditions: [{ field: "ai_category", op: "is", value: "transaction" }], actions: [{ type: "star" }] }, mailbox_id: A.id, email_id: on.email.id })237  assert.deepEqual([dry.matched, dry.facts.ai_category], [true, "transaction"])238239  // LA garantie : aucune ligne d'envoi pour A, aucune soumission JMAP dans A, rien de A n'est arrivé chez B240  await sleep(3000)241  assert.deepEqual(await outboundEvidence(), before)242  assert.deepEqual(before, { outbound_from_a: 0, submissions_a: 0, b_inbox: 0 })243  assert.equal((await drafts()).length, 2, "deux brouillons, toujours à l'état de brouillon")244  assert.ok((await drafts()).every((e) => e.keywords.$draft && Object.keys(e.mailboxIds).length === 1))245  // les brouillons créés par une règle ne redéclenchent aucune règle (ce ne sont pas des réceptions)246  assert.equal((await sql`select count(*)::int as n from rule_runs where email_id = any(${(await drafts()).map((e) => e.id)})`)[0].n, 0)247})248249test("essai à blanc : dit ce que la règle ferait, sans rien faire ni consigner", async () => {250  const e = await inA("Message ordinaire")251  const runs = (await sql`select count(*)::int as n from rule_runs`)[0].n252  const hooks = rx.hits.length253  const r = await admin.call("POST", "/api/next/rules/test", { mailbox_id: A.id, email_id: e.id, rule: {254    match: "any", conditions: [{ field: "subject", op: "contains", value: "ordinaire" }, { field: "has_attachment", op: "is", value: true }, { field: "size", op: "gt", value: 10_000_000 }],255    actions: [{ type: "star" }, { type: "mark_read" }, { type: "draft_reply", snippet_id: snippet.id }, { type: "event", name: "x" }] } })256  assert.equal(r.matched, true)257  assert.equal(r.dry_run, true)258  assert.deepEqual(r.conditions.map((c: any) => c.result), [true, false, false])259  assert.deepEqual(r.actions.map((a: any) => a.type), ["star", "mark_read", "draft_reply", "event"])260  assert.match(r.actions[2].would, /DRAFT \(never sent\)/)261  const no = await admin.call("POST", "/api/next/rules/test", { mailbox_id: A.id, email_id: e.id, rule: { conditions: [{ field: "from", op: "equals", value: "quelquun@stg.spboucher.ai" }], actions: [{ type: "star" }] } })262  assert.deepEqual([no.matched, no.actions], [false, []])263  await sleep(1500)264  assert.deepEqual((await inA("Message ordinaire")).keywords, e.keywords, "aucun mot-clé posé")265  assert.equal((await sql`select count(*)::int as n from rule_runs`)[0].n, runs)266  assert.equal(rx.hits.length, hooks)267  await rejects(member.call("POST", "/api/next/rules/test", { mailbox_id: A.id, email_id: e.id, rule: { actions: [{ type: "star" }] } }), 404)268  await rejects(admin.call("POST", "/api/next/rules/test", { mailbox_id: A.id, email_id: "zzzzzzzz", rule: { actions: [{ type: "star" }] } }), 404)269})270271test("une règle ne s'applique qu'au courrier reçu après sa création ou sa réactivation ; le journal mène au fil", async () => {272  const drained = async (emailId: string) => until(async () => (await sql`select 1 from watched_messages where email_id = ${emailId}`).length && !(await sql`select 1 from rule_jobs`).length, "file des règles vidée")273  const requeue = (emailId: string) => sql`insert into rule_jobs (user_id, mailbox_id, email_id) select user_id, id, ${emailId} from mailboxes where id = ${A.id} on conflict do nothing`274  // 1. un message arrive AVANT la règle ; son évaluation (travail en file, repris plus tard) a lieu APRÈS la création de la règle275  await send("Rétroactif alpha")276  const before = await until(() => inA("Rétroactif alpha"), "arrivée")277  await drained(before.id)278  await sleep(2500)279  const rule = await mk("Rétroactif", { conditions: [{ field: "subject", op: "contains", value: "rétroactif" }], actions: [{ type: "star" }] })280  assert.ok(Math.abs(Date.parse(rule.enabled_at) - Date.now()) < 10_000, "active depuis sa création")281  await requeue(before.id)282  await until(async () => !(await sql`select 1 from rule_jobs`).length, "travail évalué")283  await sleep(500)284  assert.equal((await runsOf(rule.id)).length, 0, "aucune exécution pour un message reçu avant la règle")285  assert.ok(!(await inA("Rétroactif alpha")).keywords.$flagged)286  // 2. un message reçu après : la règle s'applique ; la ligne d'historique porte la boîte et le fil287  await send("Rétroactif bêta")288  const { email } = await ran(rule.id, "Rétroactif bêta")289  assert.equal(email.keywords.$flagged, true)290  const log = (await admin.call("GET", `/api/next/rules/${rule.id}/runs`)).data291  assert.deepEqual(log.map((r: any) => [r.email_id, r.mailbox_id, r.thread_id, r.status]), [[email.id, A.id, email.threadId, "ok"]])292  // 3. règle coupée puis réactivée : elle ne rattrape pas le courrier arrivé entre-temps293  await admin.call("PATCH", `/api/next/rules/${rule.id}`, { enabled: false })294  await send("Rétroactif gamma")295  const during = await until(() => inA("Rétroactif gamma"), "arrivée")296  await drained(during.id)297  await sleep(2500)298  const back = await admin.call("PATCH", `/api/next/rules/${rule.id}`, { enabled: true })299  assert.ok(Date.parse(back.enabled_at) > Date.parse(rule.enabled_at), "la réactivation déplace le point de départ")300  // modifier une règle active ne déplace pas son point de départ301  assert.equal((await admin.call("PATCH", `/api/next/rules/${rule.id}`, { name: "Rétroactif 2", enabled: true })).enabled_at, back.enabled_at)302  await requeue(during.id)303  await requeue(before.id)304  await until(async () => !(await sql`select 1 from rule_jobs`).length, "travaux évalués")305  await sleep(500)306  assert.deepEqual((await runsOf(rule.id)).map((r) => r.email_id), [email.id], "une seule exécution : le message reçu pendant que la règle était active")307  await admin.call("DELETE", `/api/next/rules/${rule.id}`)308})309310test("idempotence et durabilité : la file survit à l'arrêt du serveur ; un travail rejoué ne refait aucune action", async () => {311  // 1. un message arrive pendant que toutes les règles sont coupées : ni travail ni exécution312  await sql`update rules set enabled = false`313  await send("Facture tardive", "Montant : 10 $.")314  const late = await until(() => inA("Facture tardive"), "arrivée")315  await until(async () => (await sql`select 1 from watched_messages where email_id = ${late.id}`).length, "vu par le veilleur")316  assert.equal((await sql`select count(*)::int as n from rule_jobs`)[0].n, 0)317  // 2. serveur arrêté : le travail est inscrit en base (comme l'aurait fait le crochet), les règles réactivées318  await haltServer()319  await sql`update rules set enabled = true`320  await sql`insert into rule_jobs (user_id, mailbox_id, email_id) select user_id, id, ${late.id} from mailboxes where id = ${A.id}`321  const hooks = rx.hits.length322  await sleep(2000)323  assert.equal((await sql`select count(*)::int as n from rule_runs where email_id = ${late.id}`)[0].n, 0, "rien ne se passe sans serveur")324  // 3. redémarrage : le travail en file est repris depuis la base, une seule fois325  await restartServer(ENV)326  const run = await until(async () => (await sql`select * from rule_runs where rule_id = ${rules.Factures.id} and email_id = ${late.id} and status <> 'running'`)[0], "exécution après redémarrage")327  assert.equal(run.status, "ok", run.error)328  await until(async () => !(await sql`select 1 from rule_jobs`).length, "file vidée")329  await until(async () => hits((d) => d.name === "invoice.received").length === 2, "événement de la facture tardive")330  const state = async () => ({331    runs: (await sql`select rule_id, email_id, status from rule_runs order by id`).map((r) => `${r.rule_id}:${r.email_id}:${r.status}`),332    reminders: (await sql`select count(*)::int as n from reminders`)[0].n, events: (await sql`select count(*)::int as n from webhook_events where type = 'rule.matched'`)[0].n,333    drafts: (await list(A.id, acctA)).filter((e) => e.mailboxIds[folders.drafts]).length, out: await outboundEvidence(),334  })335  await until(async () => rx.hits.length >= hooks + 2, "les deux événements de la facture tardive (Factures, Tout)")336  assert.equal((await sql`select count(*)::int as n from rule_runs where email_id = ${late.id}`)[0].n, 2, "Factures et Tout, une fois chacune")337  // 4. règle supprimée : son journal part avec elle (et elle ne s'appliquera pas aux messages rejoués ci-dessous)338  await admin.call("DELETE", `/api/next/rules/${rules.Tout.id}`)339  assert.equal((await sql`select count(*)::int as n from rule_runs where rule_id = ${rules.Tout.id}`)[0].n, 0)340  // 5. tous les travaux rejoués (crochet relancé après un arrêt brutal), avec un second redémarrage : aucune action refaite.341  //    L'idempotence est par (règle, message) : chaque règle encore présente a déjà sa ligne d'exécution pour ces messages.342  const s1 = await state()343  await sql`insert into rule_jobs (user_id, mailbox_id, email_id) select distinct user_id, mailbox_id, email_id from rule_runs`344  const replayed = (await sql`select count(*)::int as n from rule_jobs`)[0].n345  assert.ok(replayed >= 6, `travaux rejoués : ${replayed}`)346  await restartServer(ENV)347  await until(async () => !(await sql`select 1 from rule_jobs`).length, "file rejouée vidée", 60_000)348  await sleep(2500)349  assert.deepEqual(await state(), s1)350  assert.equal(rx.hits.length, hooks + 2, "aucun événement de plus")351  assert.deepEqual(s1.out, { outbound_from_a: 0, submissions_a: 0, b_inbox: 0 })352  // y compris la règle dont les actions IA avaient été ignorées (IA coupée à l'époque) : une exécution par message, pas de rattrapage353  assert.equal((await sql`select count(*)::int as n from rule_runs where rule_id = ${rules.Devis.id}`)[0].n, 2)354})355
Files main
Menu

Theme