eleven / ocho Public

Clone
git clone https://amadan.net/eleven/ocho

Plain git — no account needed to clone.

Download

Download this file

1// The flow engine + setup flow, driven end-to-end in Node with a fake io —
2// the whole conversation (bubbles, chips, typed answers, photo, validation
3// branches, the passkey probe) without a browser. This is the transcript-level
4// proof that the flow-as-data refactor preserved setup.js's behaviour, and the
5// fixture discipline the Swift interpreter will inherit.
6import assert from "node:assert/strict";
7import { test } from "node:test";
8import { runFlow, renderText, evalCond, cleanNameAnswer, looksLikeEmail, confirmNeeded } from "../web/static/flow.js";
9import SETUP_FLOW from "../web/static/setup.flow.js";
10import WELCOME_FLOW from "../web/static/welcome.flow.js";
11
12const tick = () => new Promise((r) => setImmediate(r));
13
14// A fake io that mirrors the real engine surface: say dedupes by key||text
15// (like eleven.js) but always re-arms chips; verbs return scripted results.
16function harness({ verbs = {}, done = {}, enrolled = false, fromHome = false, canEnrol = true, myName = "", homeSignedIn = false, planBook = null, envExtra = {} } = {}) {
17 const h = {
18 transcript: [], // { text, chips: [labels], extra }
19 verbLog: [],
20 records: [],
21 said: new Set(),
22 lastChips: null,
23 pendingAsk: null,
24 pendingDelays: [],
25 doneSteps: { ...done },
26 enrolled,
27 fromHome,
28 canEnrol,
29 myName,
30 homeSignedIn,
31 };
32 h.env = {
33 threadId: "T",
34 respond: () => {},
35 typing: null,
36 sender: { name: "Eleven", kind: "bot" },
37 bridge: {
38 myName: () => h.myName,
39 enrolled: () => h.enrolled,
40 fromHome: () => h.fromHome,
41 // Native only: whether this device holds a live Eleven session, which
42 // decides whether claiming a space really did push its key anywhere.
43 // The web never defines it (undefined reads false), which is why the
44 // harness default is false — a web-shaped run must behave as the web
45 // does, or every settle that watches this fact fires on the wrong side.
46 homeSignedIn: () => h.homeSignedIn,
47 // Whether signing in is possible at all (#1377) — false on self-hosted
48 // spaces with no home server; the flows skip the un-actionable pitch.
49 canEnrol: () => h.canEnrol,
50 // The plan step's price book (#1374) — mirrors apps/setup.js's getters;
51 // absent (the default) skips the step, like a self-hosted space.
52 planPrices: () => !!planBook,
53 planTeam: () => !!(planBook && planBook.team),
54 planCommunity: () => !!(planBook && planBook.community),
55 planFamily: () => !!(planBook && planBook.family),
56 planNameTeam: () => (planBook && planBook.team && planBook.team.name) || "",
57 planPriceTeam: () => (planBook && planBook.team && planBook.team.price) || "",
58 planNameCommunity: () => (planBook && planBook.community && planBook.community.name) || "",
59 planPriceCommunity: () => (planBook && planBook.community && planBook.community.price) || "",
60 planNameFamily: () => (planBook && planBook.family && planBook.family.name) || "",
61 planPriceFamily: () => (planBook && planBook.family && planBook.family.price) || "",
62 },
63 space: { name: "Old Name", nameSet: false },
64 onEnd: () => (h.ended = true),
65 };
66 Object.assign(h.env, envExtra);
67 h.io = {
68 say: (env, text, chips, extra) => {
69 const key = (extra && extra.key) || text;
70 if (!(extra && extra.local) && h.said.has(key)) {
71 if (chips) h.lastChips = chips;
72 return;
73 }
74 if (!(extra && extra.local)) h.said.add(key);
75 h.transcript.push({ text, chips: (chips || []).map((c) => c.label), extra: extra || {} });
76 if (chips) h.lastChips = chips;
77 },
78 ask: (threadId, cb) => (h.pendingAsk = cb),
79 cancelAnswer: () => {
80 h.cancelled = (h.cancelled || 0) + 1;
81 h.pendingAsk = null;
82 },
83 verb: (name, args) => {
84 h.verbLog.push([name, ...args]);
85 const r = verbs[name];
86 return typeof r === "function" ? r(...args) : r;
87 },
88 record: (step, label, value) => {
89 h.records.push({ step, label, value });
90 h.doneSteps[step] = true;
91 },
92 done: (step) => !!h.doneSteps[step],
93 imgSource: (t) => `IMG(${t})`,
94 delay: (ms, fn) => h.pendingDelays.push({ ms, fn }),
95 };
96 h.answer = async (text, payload) => {
97 const cb = h.pendingAsk;
98 h.pendingAsk = null;
99 cb(text, payload);
100 await tick();
101 await tick();
102 };
103 h.tap = async (label) => {
104 const chip = (h.lastChips || []).find((c) => c.label === label);
105 assert.ok(chip, `chip "${label}" not offered (have: ${(h.lastChips || []).map((c) => c.label).join(", ")})`);
106 chip.run();
107 await tick();
108 await tick();
109 };
110 h.lastText = () => h.transcript[h.transcript.length - 1].text;
111 return h;
112}
113
114await test("cleanNameAnswer strips conversational replies down to the name", () => {
115 // The exact live failure: the "I'm {name}" chip primes typed replies too.
116 assert.equal(cleanNameAnswer("I'm Dermot"), "Dermot");
117 assert.equal(cleanNameAnswer("I’m Dermot"), "Dermot"); // iOS smart apostrophe
118 assert.equal(cleanNameAnswer("i am dermot"), "dermot"); // case kept as typed
119 assert.equal(cleanNameAnswer("My name is Ana."), "Ana");
120 assert.equal(cleanNameAnswer("Hi, I'm Dermot!"), "Dermot");
121 assert.equal(cleanNameAnswer("hello my name's Bo"), "Bo");
122 assert.equal(cleanNameAnswer("you can call me Dermo"), "Dermo");
123 assert.equal(cleanNameAnswer("It's Maud"), "Maud");
124 assert.equal(cleanNameAnswer("Im Dermot"), "Dermot"); // apostrophe skipped
125});
126
127await test("looksLikeEmail: whole-string only — a name with an @ in it is a name", () => {
128 for (const yes of ["paul@rslw.com", " Ada@example.co.uk ", "a.b+c@sub.domain.org"]) {
129 assert.equal(looksLikeEmail(yes), true, yes);
130 }
131 for (const no of ["Ada", "Ada @ the shop", "@ada", "ada@", "ada@localhost", "", null, "two@parts@here.com"]) {
132 assert.equal(looksLikeEmail(no), false, String(no));
133 }
134});
135
136await test("confirmNeeded: only a known rule fires, and only with a destination", () => {
137 assert.equal(confirmNeeded({ when: "email", goto: "x" }, "a@b.com"), true);
138 assert.equal(confirmNeeded({ when: "email", goto: "x" }, "Ada"), false);
139 assert.equal(confirmNeeded({ when: "email" }, "a@b.com"), false, "no goto, no jump");
140 // Forward compatibility: a rule this build doesn't know must behave as if
141 // absent rather than park the conversation on an unreachable step.
142 assert.equal(confirmNeeded({ when: "phone", goto: "x" }, "a@b.com"), false);
143 assert.equal(confirmNeeded(null, "a@b.com"), false);
144});
145
146// An email typed where a name was asked used to become the name the whole
147// space sees, silently (Paul, 2026-08-29). Now it is confirmed first — and
148// until it is, nothing is recorded and setMyName never runs.
149await test("setup: an email answered as a name is confirmed, not swallowed", async () => {
150 const h = harness({ verbs: { setMyName: "ok", setGroupName: "ok" } });
151 runFlow(SETUP_FLOW, h.env, h.io);
152 await tick();
153 await h.answer("paul@rslw.com");
154
155 assert.match(h.lastText(), /That's an email address/);
156 assert.equal(h.verbLog.some((v) => v[0] === "setMyName"), false, "nothing set yet");
157 assert.equal(h.records.some((r) => r.step === "name"), false, "nothing recorded yet");
158
159 // Changing your mind re-asks the name, still with nothing recorded.
160 await h.tap("Type a different name");
161 assert.equal(h.records.some((r) => r.step === "name"), false);
162 await h.answer("Paul");
163 assert.deepEqual(h.verbLog.find((v) => v[0] === "setMyName"), ["setMyName", "Paul"]);
164 assert.equal(h.records.find((r) => r.step === "name").value, "Paul");
165 assert.ok(h.transcript.find((t) => t.extra.key === "q-group"), "carried on to the space name");
166});
167
168await test("setup: 'Use it anyway' keeps the email as the name, recorded once", async () => {
169 const h = harness({ verbs: { setMyName: "ok", setGroupName: "ok" } });
170 runFlow(SETUP_FLOW, h.env, h.io);
171 await tick();
172 await h.answer("paul@rslw.com");
173 await h.tap("Use it anyway");
174
175 assert.deepEqual(h.verbLog.find((v) => v[0] === "setMyName"), ["setMyName", "paul@rslw.com"]);
176 const rec = h.records.filter((r) => r.step === "name");
177 assert.equal(rec.length, 1, "the confirm records the NAME step, once");
178 assert.equal(rec[0].value, "paul@rslw.com");
179 assert.ok(h.transcript.find((t) => t.extra.key === "q-group"), "carried on to the space name");
180});
181
182await test("cleanNameAnswer leaves plain names (and lookalikes) alone", () => {
183 assert.equal(cleanNameAnswer("Dermot"), "Dermot");
184 assert.equal(cleanNameAnswer(" Dermot "), "Dermot");
185 assert.equal(cleanNameAnswer("Hilary"), "Hilary"); // "hi" needs a boundary
186 assert.equal(cleanNameAnswer("Ima Jean"), "Ima Jean"); // "im" needs a space
187 assert.equal(cleanNameAnswer("Mary-Kate O'Brien"), "Mary-Kate O'Brien");
188 // Stripping must never clean a name to nothing — keep the original.
189 assert.equal(cleanNameAnswer("I'm"), "I'm");
190 assert.equal(cleanNameAnswer(""), "");
191});
192
193await test("the name step cleans the typed answer — verb, record, and echo all use it", async () => {
194 const h = harness();
195 runFlow(SETUP_FLOW, h.env, h.io);
196 await tick();
197 await h.answer("Hi, I'm Dermot!");
198 assert.deepEqual(h.verbLog[0], ["setMyName", "Dermot"], "the screen name is the cleaned name");
199 assert.deepEqual(h.records[0], { step: "name", label: "noted: you're Dermot", value: "Dermot" });
200 const group = h.transcript.find((t) => /Lovely to meet you/.test(t.text));
201 assert.match(group.text, /^Lovely to meet you, Dermot! 🤝/, "the echo shows the cleaned name");
202});
203
204await test("flows are pure JSON — shippable to native interpreters", () => {
205 for (const FLOW of [SETUP_FLOW, WELCOME_FLOW]) {
206 assert.deepStrictEqual(JSON.parse(JSON.stringify(FLOW)), FLOW);
207 assert.equal(FLOW.schema, 1);
208 // Every goto/skipTo/auto/probe/record target names a real step.
209 const ids = new Set(FLOW.steps.map((s) => s.id));
210 const targets = JSON.stringify(FLOW).match(/"(?:goto|skipTo|auto|step)":"([^"]+)"/g) || [];
211 for (const t of targets) {
212 const id = t.split(":")[1].replaceAll('"', "");
213 assert.ok(ids.has(id), `${FLOW.flow}: dangling target ${id}`);
214 }
215 }
216});
217
218await test("setup: the full first-run transcript, happy path with detours", async () => {
219 const h = harness({
220 verbs: {
221 setGroupName: (name) => (name === "X" ? "invalid" : "ok"),
222 setContactEmail: (e) => (e.includes("@") ? "ok" : "invalid"),
223 adoptServerPhoto: () => true,
224 },
225 });
226 runFlow(SETUP_FLOW, h.env, h.io);
227 await tick();
228
229 // Greeting + name question (fresh run: not resumed, no "Welcome back").
230 assert.match(h.transcript[0].text, /^Hi — I'm Eleven! 👋/);
231 assert.equal(h.transcript[1].extra.label, "Your name:");
232 assert.equal(h.transcript[1].extra.hint, "e.g. Finn Wolfhard");
233 assert.equal(h.transcript.some((t) => /Welcome back/.test(t.text)), false);
234
235 // Name: 40 chars caps to 32 (the profile-rename cap).
236 await h.answer("A".repeat(40));
237 assert.deepEqual(h.verbLog[0], ["setMyName", "A".repeat(32)]);
238 assert.equal(h.records[0].step, "name");
239
240 // Group: ack folded into the context bubble; no Keep chip (nameSet=false).
241 // (lastChips still holds the name step's quick-form chip — a chipless say
242 // doesn't reset it — so check the Keep chip's absence, not emptiness.)
243 const group = h.transcript.find((t) => t.extra.key === "q-group");
244 assert.match(group.text, /^Lovely to meet you, A{32}! 🤝\n\nNext, this space needs a name/);
245 assert.equal((h.lastChips || []).some((c) => /^Keep/.test(c.label)), false, "no Keep chip on a fresh instance");
246
247 // Rejected name says the honest error and re-asks; accepted name records.
248 await h.answer("X");
249 assert.match(h.lastText(), /didn't stick — it needs to be 1–60/);
250 await h.answer("Team Awesome");
251 assert.equal(h.env.space.name, "Team Awesome");
252 assert.deepEqual(h.records[1], { step: "group", label: "named this space “Team Awesome”", value: "Team Awesome" });
253
254 // Email: invalid re-asks in place with the Skip chip still live.
255 await h.answer("not-an-email");
256 assert.match(h.lastText(), /doesn't look like an email address/);
257 assert.ok(h.lastChips.find((c) => c.label === "Skip for now"));
258 await h.answer("paul@example.com");
259 assert.equal(h.records[2].label, "noted a contact email");
260
261 // Photo: a text-only reply is rejected and the waiter re-arms.
262 await h.answer("here you go", { attachments: [] });
263 assert.match(h.lastText(), /Send it as a picture/);
264 await h.answer("Our logo", { attachments: [{ type: "image/jpeg", id: "img1" }] });
265 const adopt = h.verbLog.find((v) => v[0] === "adoptServerPhoto");
266 assert.equal(adopt[2], "Our logo", "message text rides as alt text");
267
268 // Preview: alt-text publicity warning verbatim, image bubble, Keep records.
269 assert.match(h.lastText(), /“Our logo” — and like the photo itself it's PUBLIC/);
270 assert.equal(h.transcript[h.transcript.length - 1].extra.img, "IMG($serverAvatar)");
271 await h.tap("Keep this photo");
272 assert.deepEqual(h.records[3], { step: "photo", label: "set the space photo", value: "Our logo" });
273
274 // Invites → extras → the AI opt-in → keys: the one-tap common path records
275 // and moves on, and AI is always its own explicit question.
276 await h.tap("Only me");
277 assert.ok(h.verbLog.find((v) => v[0] === "setUserInvites" && v[1] === false));
278 assert.ok(h.transcript.find((t) => /comes with a couple of extras/.test(t.text)));
279 await h.tap("Keep both on");
280 assert.ok(h.records.find((r) => r.step === "extras" && r.value === "all-on"));
281 assert.equal(h.verbLog.some((v) => v[0] === "setFeature"), false, "keeping defaults writes nothing");
282 assert.ok(h.transcript.find((t) => /read the chats it's invited to/.test(t.text)), "the AI question carries the honest note");
283 await h.tap("Bring Eleven in");
284 assert.deepEqual(h.verbLog.at(-1), ["setFeature", "ai", true], "opting in writes the gate explicitly");
285 assert.ok(h.records.find((r) => r.step === "ai-optin" && r.value === "on"));
286 assert.ok(h.transcript.find((t) => /Now the most important thing of all/.test(t.text)));
287 await h.tap("Sign in");
288 assert.ok(h.verbLog.find((v) => v[0] === "savePasskey"));
289
290 // The probe fires with no enrolment → the stayed-in-place waiting step. (The
291 // step also arms a background settle poll; target the probe by its interval.)
292 const keysProbe = h.pendingDelays.find((d) => d.ms === 1500);
293 assert.ok(keysProbe, "the Sign in probe schedules a 1500ms check");
294 keysProbe.fn();
295 await tick();
296 assert.match(h.lastText(), /I'll wait here while you finish/);
297
298 // They finish the ceremony elsewhere; Continue records the verified label.
299 h.enrolled = true;
300 await h.tap("Continue");
301 assert.deepEqual(h.records.find((r) => r.step === "keys"), { step: "keys", label: "Signed in — your space's key is safe", value: "passkey" });
302 assert.deepEqual(h.records.at(-1), { step: "finish", label: "Your space is ready", value: "done" }, "finish persists its completion marker");
303
304 // Finish: complete marked, no reminder deferral (backed up), no nag line.
305 assert.ok(h.verbLog.find((v) => v[0] === "markSetupComplete"));
306 assert.equal(h.verbLog.some((v) => v[0] === "deferBackupReminders"), false);
307 const fin = h.transcript.find((t) => /Team Awesome is ready! 🎉/.test(t.text));
308 assert.equal(/One more reminder/.test(fin.text), false);
309 await h.tap("Open admin");
310 assert.ok(h.verbLog.find((v) => v[0] === "openAdmin"));
311 assert.ok(h.ended, "flow signalled completion");
312});
313
314await test("setup: a device that already knows the name RECORDS the skip so other clients converge", async () => {
315 // The 2026-07-19 web-vs-Mac divergence: a second device joining mid-flow
316 // with the member's name already known (an add-device link ships it in the
317 // fragment) skipped the name step via a bare skipIf — nothing landed in
318 // the log, so a client that did NOT know the name stayed armed on
319 // "Your name:" forever while this one moved on. The skip must be a
320 // recorded entry: done.name folds to every client.
321 const h = harness({ myName: "Paul" });
322 runFlow(SETUP_FLOW, h.env, h.io);
323 await tick();
324
325 // First run with a name: still greeted (resumedIf deliberately ignores
326 // bridge.myName — the creator's first run keeps its hello), the name
327 // question never asked, and — the fix — the skip is IN THE LOG.
328 assert.match(h.transcript[0].text, /^Hi — I'm Eleven!/);
329 assert.equal(h.transcript.some((t) => /what should everyone here call you/.test(t.text)), false);
330 assert.ok(h.records.find((r) => r.step === "name"), "the known-name skip writes done.name");
331
332 // A nameless client folding that record lands on the same step (group)
333 // instead of re-arming the name question.
334 const h2 = harness({ done: { name: true } });
335 runFlow(SETUP_FLOW, h2.env, h2.io);
336 await tick();
337 assert.equal(h2.transcript.some((t) => /what should everyone here call you/.test(t.text)), false);
338 assert.ok(h2.transcript.find((t) => t.extra.key === "q-group"), "converged on the group step");
339});
340
341await test("setup: a reentry re-derive resumes quietly — no 'Welcome back' spam", async () => {
342 // env.reentry marks a mid-session restart (the setup host reboots the flow
343 // when ANOTHER client answers the parked step). It re-derives position from
344 // the folded log like any resume, but must not re-greet: one "Welcome back"
345 // per foreign answer would spam the feed.
346 const h = harness({ myName: "Paul", done: { name: true, group: true }, envExtra: { reentry: true } });
347 runFlow(SETUP_FLOW, h.env, h.io);
348 await tick();
349 assert.equal(h.transcript.some((t) => /^Welcome back/.test(t.text)), false, "no re-greeting");
350 // …and it still lands parked on the first unanswered step (email).
351 assert.ok(h.transcript.find((t) => /How can people here reach you outside the chat/.test(t.text)));
352});
353
354await test("setup: resumed run — skips, Keep chip, skip-everything path", async () => {
355 // Resumed = a recorded step (done.name), not merely a set name — a name
356 // alone is the native creator's FIRST run and still gets the greeting.
357 const h = harness({ myName: "Paul", done: { name: true }, verbs: { setContactEmail: () => "ok" } });
358 h.env.space = { name: "Old Name", nameSet: true };
359 runFlow(SETUP_FLOW, h.env, h.io);
360 await tick();
361
362 // Resumed: brief local re-greeting, name step skipped entirely.
363 assert.match(h.transcript[0].text, /^Welcome back/);
364 assert.equal(h.transcript[0].extra.local, true);
365 assert.equal(h.transcript.some((t) => /what should everyone here call you/.test(t.text)), false);
366
367 // Group offers keeping the explicitly-set current name.
368 assert.match(h.transcript.find((t) => t.extra.key === "q-group").text, /^Next, this space needs a name/);
369 await h.tap("Keep “Old Name”");
370 assert.equal(h.verbLog.some((v) => v[0] === "setGroupName"), false, "Keep never re-saves the name");
371
372 await h.tap("Skip for now"); // email
373 await h.tap("Skip for now"); // photo — must disarm the waiter
374 assert.equal(h.cancelled, 1, "photo skip cancels the pending answer");
375 await h.tap("Members can invite people");
376 await h.tap("Keep both on");
377 await h.tap("Not now"); // the AI opt-in — declining writes an explicit off
378 assert.deepEqual(h.verbLog.at(-1), ["setFeature", "ai", false]);
379
380 // Keys: decline twice through the plain-words confirm.
381 await h.tap("Not now");
382 assert.match(h.lastText(), /I have to be straight with you/);
383 await h.tap("I understand the risk");
384 assert.deepEqual(h.records.find((r) => r.step === "keys"), { step: "keys", label: "Skipped signing in for now", value: "skipped" });
385 assert.deepEqual(h.records.at(-1), { step: "finish", label: "Your space is ready", value: "done" });
386
387 // Finish without a backup: reminders deferred, the nag line present.
388 assert.ok(h.verbLog.find((v) => v[0] === "deferBackupReminders"));
389 assert.ok(h.verbLog.find((v) => v[0] === "markSetupComplete"));
390 assert.ok(h.transcript.find((t) => /One more reminder before you go/.test(t.text)));
391 assert.ok(h.transcript.find((t) => /Old Name is ready! 🎉/.test(t.text)));
392});
393
394await test("setup: already enrolled — keys short-circuits with the once-only ack", async () => {
395 const h = harness({
396 myName: "Paul",
397 enrolled: true,
398 done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true },
399 });
400 runFlow(SETUP_FLOW, h.env, h.io);
401 await tick();
402 assert.deepEqual(h.records[0], { step: "keys", label: "Signed in — your space's key is safe", value: "passkey" });
403 assert.ok(h.transcript.find((t) => /your space's key is safe. ✅/.test(t.text)));
404 assert.equal(h.verbLog.some((v) => v[0] === "deferBackupReminders"), false);
405 assert.ok(h.verbLog.find((v) => v[0] === "markSetupComplete"));
406
407 // Second entry with keys done: no duplicate record, straight to finish.
408 const h2 = harness({ myName: "Paul", enrolled: true, done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true, keys: true } });
409 runFlow(SETUP_FLOW, h2.env, h2.io);
410 await tick();
411 // No duplicate keys record — only finish stamping its completion marker.
412 assert.deepEqual(h2.records, [{ step: "finish", label: "Your space is ready", value: "done" }]);
413 assert.equal(h2.transcript.some((t) => /key is safe. ✅/.test(t.text)), false);
414});
415
416await test("setup: extras — 'Let me choose' walks the pair, then the AI opt-in", async () => {
417 const h = harness({
418 myName: "Paul",
419 done: { name: true, group: true, email: true, photo: true, invites: true },
420 });
421 runFlow(SETUP_FLOW, h.env, h.io);
422 await tick();
423 assert.ok(h.transcript.find((t) => /comes with a couple of extras/.test(t.text)));
424
425 await h.tap("Let me choose");
426 assert.ok(h.transcript.find((t) => /Bots — helpers/.test(t.text)));
427 await h.tap("Enable bots");
428 assert.deepEqual(h.verbLog.at(-1), ["setFeature", "bots", true]);
429 await h.tap("No apps here");
430 assert.deepEqual(h.verbLog.at(-1), ["setFeature", "apps", false]);
431 // The AI opt-in follows the granular pair too — never skippable by path.
432 assert.ok(h.transcript.find((t) => /read the chats it's invited to/.test(t.text)));
433 await h.tap("Not now");
434 assert.deepEqual(h.verbLog.at(-1), ["setFeature", "ai", false]);
435 assert.ok(h.records.find((r) => r.step === "extras-apps" && r.value === "off"));
436 assert.ok(h.records.find((r) => r.step === "ai-optin" && r.value === "off"));
437 // The path lands on the keys pitch like the one-tap path.
438 assert.ok(h.transcript.find((t) => /Now the most important thing of all/.test(t.text)));
439
440 // Resume after the granular path: the extras entry closes the step out and
441 // neither the pair nor the AI question replays.
442 const h2 = harness({
443 myName: "Paul",
444 enrolled: true,
445 done: { ...h.doneSteps },
446 });
447 runFlow(SETUP_FLOW, h2.env, h2.io);
448 await tick();
449 assert.equal(h2.transcript.some((t) => /read the chats it's invited to/.test(t.text)), false, "answered steps don't replay");
450 assert.ok(h2.records.find((r) => r.step === "extras" && r.value === "chosen"), "the entry closes extras");
451 assert.ok(h2.records.find((r) => r.step === "keys"), "resume reaches keys");
452});
453
454await test("setup: from-home key step + settle auto-detects the enrolment return", async () => {
455 const h = harness({
456 myName: "Paul",
457 fromHome: true, // arrived from the Eleven sign-in page → the keys-home variant
458 done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true },
459 });
460 runFlow(SETUP_FLOW, h.env, h.io);
461 await tick();
462
463 // fromHome routes to keys-home ("add to your passkey"), not the from-scratch pitch.
464 assert.ok(h.transcript.find((t) => /Last thing — the key/.test(t.text)), "keys-home shown");
465 assert.equal(h.transcript.some((t) => /Now the most important thing of all/.test(t.text)), false);
466
467 // The ceremony returns asynchronously: enrolment lands, and the settle poll —
468 // NOT a manual "Continue" tap — carries the flow on to finish.
469 const settle = h.pendingDelays.find((d) => d.ms === 700);
470 assert.ok(settle, "keys-home arms a settle poll while it waits");
471 h.enrolled = true;
472 settle.fn();
473 await tick();
474 await tick();
475 assert.deepEqual(h.records.find((r) => r.step === "keys-home"), { step: "keys-home", label: "Signed in — your space's key is safe", value: "passkey" });
476 assert.deepEqual(h.records.at(-1), { step: "finish", label: "Your space is ready", value: "done" });
477 assert.ok(h.transcript.find((t) => /your space's key is safe. ✅/.test(t.text)));
478 assert.ok(h.verbLog.find((v) => v[0] === "markSetupComplete"), "reached finish without a tap");
479});
480
481await test("setup: native creator — greeting shows, name pre-set, passkey never pitched, no admin hop", async () => {
482 // The native create-space landing: screen name typed in the create UI, the
483 // conversation runs in the app-thread's embedded webview with env.native.
484 const h = harness({ myName: "Paul", homeSignedIn: true,
485 verbs: { setGroupName: () => "ok", setContactEmail: () => "ok" } });
486 h.env.native = true;
487 runFlow(SETUP_FLOW, h.env, h.io);
488 await tick();
489
490 // First run WITH a name: still greeted (not "resumed"), name step skipped.
491 assert.match(h.transcript[0].text, /^Hi — I'm Eleven! 👋/);
492 assert.equal(h.transcript.some((t) => /Welcome back/.test(t.text)), false);
493 assert.equal(h.transcript.some((t) => /what should everyone here call you/.test(t.text)), false);
494 assert.ok(h.transcript.find((t) => t.extra.key === "q-group"));
495
496 await h.answer("Team Awesome");
497 await h.tap("Skip for now"); // email; photo auto-skips under native
498 assert.equal(h.transcript.some((t) => t.extra.key === "q-photo"), false);
499 await h.tap("Members can invite people");
500 await h.tap("Keep both on");
501 await h.tap("Not now"); // the AI opt-in runs under native too
502
503 // Keys: never pitched — recorded as covered by the Eleven account.
504 assert.equal(h.transcript.some((t) => /Now the most important thing of all/.test(t.text)), false);
505 assert.equal(h.verbLog.some((v) => v[0] === "savePasskey"), false);
506 assert.deepEqual(h.records.find((r) => r.step === "keys"), { step: "keys", label: "Your space's key is saved with your Eleven account", value: "native" });
507
508 // Finish: completion marker recorded, complete marked, no backup nag or
509 // reminder deferral (the key IS backed up), and no web-only chips (admin
510 // hop, tour) — the finish bubble's /admin pointer covers it.
511 assert.deepEqual(h.records.at(-1), { step: "finish", label: "Your space is ready", value: "done" });
512 assert.ok(h.verbLog.find((v) => v[0] === "markSetupComplete"));
513 assert.equal(h.verbLog.some((v) => v[0] === "deferBackupReminders"), false);
514 assert.equal(h.transcript.some((t) => /One more reminder/.test(t.text)), false);
515 const chipLabels = h.transcript.flatMap((t) => t.chips || []); // harness stores labels
516 assert.equal(chipLabels.includes("Take the tour"), false, "the tour stays web-only");
517 assert.ok(chipLabels.includes("Open admin"), "native hops to Admin");
518});
519
520await test("setup: keys answered-but-not-enrolled resumes to FINISH — never re-pitched (#1377)", async () => {
521 // The bug: done.keys with the enrolled flag lost ("Continue" after a
522 // detour, "I understand the risk", cleared storage) re-pitched the whole
523 // sign-in speech on every resume and never reached markSetupComplete.
524 const h = harness({
525 myName: "Paul",
526 enrolled: false,
527 done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true, keys: true },
528 });
529 runFlow(SETUP_FLOW, h.env, h.io);
530 await tick();
531 assert.equal(h.transcript.some((t) => /Now the most important thing of all/.test(t.text)), false, "the pitch never replays");
532 assert.deepEqual(h.records, [{ step: "finish", label: "Your space is ready", value: "done" }]);
533 assert.ok(h.verbLog.find((v) => v[0] === "markSetupComplete"), "setup completes");
534 // Not enrolled in THIS browser: the finish reminder stays, honestly.
535 assert.ok(h.transcript.find((t) => /One more reminder before you go/.test(t.text)));
536 // And the finish bubble carries its stable dedup key (renames and backup
537 // flips must not persist a second copy).
538 assert.equal(h.transcript.find((t) => /is ready! 🎉/.test(t.text)).extra.key, "q-finish");
539});
540
541await test("setup: no home server — the sign-in pitch vanishes, recorded honestly (#1377)", async () => {
542 const h = harness({
543 myName: "Paul",
544 canEnrol: false,
545 done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true },
546 });
547 runFlow(SETUP_FLOW, h.env, h.io);
548 await tick();
549 assert.equal(h.transcript.some((t) => /Now the most important thing of all/.test(t.text)), false, "no un-actionable pitch");
550 assert.deepEqual(h.records[0], { step: "keys", label: "This browser holds this space's key", value: "local" });
551 assert.ok(h.verbLog.find((v) => v[0] === "markSetupComplete"));
552 // No sign-in to nag about, and no dead Sign in chip on the finish line.
553 assert.equal(h.transcript.some((t) => /One more reminder before you go/.test(t.text)), false);
554 assert.equal(h.transcript.flatMap((t) => t.chips || []).includes("Sign in"), false);
555});
556
557await test("welcome: no home server — the access ask vanishes, recorded honestly (#1377)", async () => {
558 // Inline env — welcomeEnv() is declared with the welcome section below.
559 const h = harness({
560 canEnrol: false,
561 envExtra: { suggested: "", inviterName: "", adminName: "", hasPendingThread: false, space: { name: "Test Space", nameSet: true } },
562 });
563 runFlow(WELCOME_FLOW, h.env, h.io);
564 await tick();
565 assert.equal(h.transcript.some((t) => /make sure you can't lose your way in/.test(t.text)), false, "no un-actionable ask");
566 assert.deepEqual(h.records[0], { step: "access", label: "Your access lives in this browser", value: "local" });
567 // Straight to the name question instead.
568 assert.ok(h.transcript.find((t) => /call you/.test(t.text) || /your name/i.test(t.text)));
569});
570
571await test("engine: a re-derive consumes env.reentry — the NEXT resume greets again (#1377)", async () => {
572 const h = harness({ myName: "Paul", done: { name: true, group: true }, envExtra: { reentry: true } });
573 runFlow(SETUP_FLOW, h.env, h.io);
574 await tick();
575 assert.equal(h.transcript.some((t) => /Welcome back/.test(t.text)), false, "the re-derive itself stays quiet");
576 assert.equal(h.env.reentry, false, "consumed — a later genuine resume isn't silenced");
577});
578
579await test("setup: the plan step — asked with a price book, priced from it, recorded on 'plan'", async () => {
580 // #1374: "who is this space for?" lives in the conversation; the create
581 // slider is never pay-gated. Names + prices interpolate from the book.
582 const book = {
583 team: { key: "business", name: "Business", price: "€11 per person per year" },
584 community: { key: "community", name: "Community & Friends", price: "€0.11 per member per year (€11 minimum)" },
585 family: { key: "family", name: "Family", price: "€5.50 a year" },
586 };
587 const h = harness({
588 myName: "Paul",
589 planBook: book,
590 done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true },
591 });
592 runFlow(SETUP_FLOW, h.env, h.io);
593 await tick();
594
595 const q = h.transcript.find((t) => t.extra.key === "q-plan");
596 assert.ok(q, "the plan question is asked");
597 assert.match(q.text, /who is this space for\?/);
598 assert.deepEqual(q.chips, ["A team", "A community", "Family & friends", "Keep it free"]);
599
600 await h.tap("A team");
601 const price = h.transcript.find((t) => t.extra.key === "q-plan-team");
602 assert.match(price.text, /Business: €11 per person per year/, "name + price come from the book, not the script");
603 await h.tap("Choose Business");
604 assert.deepEqual(h.records.find((r) => r.step === "plan"), {
605 step: "plan",
606 label: "chose the Business plan — payment waits at the finish line",
607 value: "team",
608 });
609
610 // The keys pitch follows; decline through to finish — which carries the
611 // plan note and the payment chip (an action chip, never a side effect).
612 await h.tap("Not now");
613 await h.tap("I understand the risk");
614 assert.ok(h.transcript.find((t) => /the plan you picked — tap below/.test(t.text)));
615 await h.tap("Set up your plan");
616 assert.ok(h.verbLog.find((v) => v[0] === "openPlanCheckout"), "the chip fires the checkout verb");
617 assert.ok(h.verbLog.find((v) => v[0] === "markSetupComplete"), "the flow still completes");
618});
619
620await test("setup: the plan step — 'Keep it free' records free; a partial book trims its chips", async () => {
621 const h = harness({
622 myName: "Paul",
623 planBook: { family: { key: "family", name: "Family", price: "€5.50 a year" } },
624 done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true },
625 });
626 runFlow(SETUP_FLOW, h.env, h.io);
627 await tick();
628 const q = h.transcript.find((t) => t.extra.key === "q-plan");
629 assert.deepEqual(q.chips, ["Family & friends", "Keep it free"], "only tiers the book offers get chips");
630 await h.tap("Family & friends");
631 await h.tap("Keep it free");
632 assert.deepEqual(h.records.find((r) => r.step === "plan"), { step: "plan", label: "kept this space free", value: "free" });
633 // No plan chosen → no payment note and no payment chip at the finish.
634 await h.tap("Not now");
635 await h.tap("I understand the risk");
636 assert.equal(h.transcript.some((t) => /the plan you picked/.test(t.text)), false);
637 assert.equal(h.transcript.flatMap((t) => t.chips || []).includes("Set up your plan"), false);
638});
639
640await test("setup: the plan step vanishes without a price book, on native, and once done", async () => {
641 // No book (self-hosted, billing off, unreachable): straight to keys.
642 const h = harness({ myName: "Paul", done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true } });
643 runFlow(SETUP_FLOW, h.env, h.io);
644 await tick();
645 assert.equal(h.transcript.some((t) => t.extra.key === "q-plan"), false);
646 assert.ok(h.transcript.find((t) => /Now the most important thing of all/.test(t.text)));
647
648 // Done (answered on another client): never re-asked, even with a book.
649 const book = { family: { key: "family", name: "Family", price: "€5.50 a year" } };
650 const h2 = harness({
651 myName: "Paul",
652 planBook: book,
653 done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true, plan: true },
654 });
655 runFlow(SETUP_FLOW, h2.env, h2.io);
656 await tick();
657 assert.equal(h2.transcript.some((t) => t.extra.key === "q-plan"), false);
658});
659
660await test("engine: template + condition mini-language", () => {
661 const env = { vars: { x: "hi" }, space: { name: "" }, __ack: "A" };
662 assert.equal(renderText(env, "{vars.x} {space.name|fallback}"), "hi fallback");
663 assert.equal(renderText(env, "{ack?}rest"), "A\n\nrest");
664 assert.equal(renderText(env, "{ack?}rest"), "rest", "ack is consumed");
665 assert.equal(renderText(env, ["a", { if: "vars.x", text: "-b" }, { if: "vars.missing", text: "-c" }]), "a-b");
666 assert.equal(renderText(env, { if: "vars.missing", then: "T", else: "E" }), "E");
667 assert.equal(evalCond(env, "!vars.missing"), true);
668});
669
670// ---- welcome flow -----------------------------------------------------------
671
672const welcomeEnv = (over = {}) => ({
673 suggested: "",
674 inviterName: "",
675 adminName: "",
676 hasPendingThread: false,
677 space: { name: "Test Space", nameSet: true },
678 ...over,
679});
680
681await test("welcome: the joiner's name step confirms an email the same way", async () => {
682 // Past the access step (already signed in) so the name question is up.
683 const h = harness({ verbs: { setMyName: "ok" }, enrolled: true,
684 done: { access: true }, envExtra: welcomeEnv() });
685 h.env.accessBackedUp = () => true;
686 runFlow(WELCOME_FLOW, h.env, h.io);
687 await tick();
688 await h.answer("paul@rslw.com");
689 assert.match(h.lastText(), /That's an email address/);
690 await h.tap("Use it anyway");
691 assert.deepEqual(h.verbLog.find((v) => v[0] === "setMyName"), ["setMyName", "paul@rslw.com"]);
692 assert.equal(h.records.filter((r) => r.step === "name").length, 1);
693});
694
695// Native, signed OUT of Eleven: claiming a space pushes nothing (HomeSync
696// no-ops without a session), so the conversation must not claim an account
697// that isn't there (Paul, 2026-08-29).
698// A native shell that CAN offer a sign-in (the app wired signInToEleven, so
699// canEnrol is true) pitches it like the web does — in the app's own words —
700// and settles on the session landing, with no "Continue" tap.
701await test("setup: native signed out — pitched in device words, settles on the sign-in", async () => {
702 const h = harness({ myName: "Paul", homeSignedIn: false, canEnrol: true,
703 verbs: { setGroupName: () => "ok", setContactEmail: () => "ok" } });
704 h.env.native = true;
705 runFlow(SETUP_FLOW, h.env, h.io);
706 await tick();
707 await h.answer("Team Awesome");
708 await h.tap("Skip for now");
709 await h.tap("Members can invite people");
710 await h.tap("Keep both on");
711 await h.tap("Not now"); // AI opt-in
712
713 const pitch = h.transcript.find((t) => /Now the most important thing of all/.test(t.text));
714 assert.ok(pitch, "the key pitch runs");
715 assert.match(pitch.text, /this device holds the only key/, "device words, not browser words");
716 assert.ok(h.transcript.some((t) => /every device you sign in on can open this space/.test(t.text)));
717
718 await h.tap("Sign in");
719 assert.ok(h.verbLog.some((v) => v[0] === "savePasskey"), "the host is asked to open its sign-in");
720
721 // The app signs in; the step's settle poll notices without a tap.
722 h.homeSignedIn = true;
723 const settle = h.pendingDelays.filter((d) => d.ms === 700).at(-1);
724 assert.ok(settle, "the keys step arms a settle poll");
725 settle.fn();
726 await tick();
727 await tick();
728 assert.deepEqual(h.records.find((r) => r.step === "keys"),
729 { step: "keys", label: "Your space's key is saved with your Eleven account", value: "native" });
730 assert.ok(h.verbLog.some((v) => v[0] === "markSetupComplete"), "carried on to finish");
731 assert.equal(h.transcript.some((t) => /One more reminder/.test(t.text)), false, "no nag — it's saved");
732});
733
734await test("setup: native without an Eleven session says where the key really is", async () => {
735 // canEnrol false mirrors the native bridge: a sign-in can't be run from
736 // inside the conversation, so the flows must not offer one.
737 const h = harness({ myName: "Paul", homeSignedIn: false, canEnrol: false,
738 verbs: { setGroupName: () => "ok", setContactEmail: () => "ok" } });
739 h.env.native = true;
740 runFlow(SETUP_FLOW, h.env, h.io);
741 await tick();
742 await h.answer("Team Awesome");
743 await h.tap("Skip for now");
744 await h.tap("Members can invite people");
745 await h.tap("Keep both on");
746 await h.tap("Not now");
747
748 assert.deepEqual(h.records.find((r) => r.step === "keys"),
749 { step: "keys", label: "This device holds this space's key", value: "local" });
750 // Still no pitch and no dead chip — signing in lives in the app's own
751 // surfaces, not in this conversation.
752 assert.equal(h.transcript.some((t) => /Now the most important thing of all/.test(t.text)), false);
753 assert.equal(h.verbLog.some((v) => v[0] === "savePasskey"), false);
754 assert.equal(h.transcript.flatMap((t) => t.chips).includes("Sign in"), false);
755 assert.ok(h.verbLog.some((v) => v[0] === "markSetupComplete"), "still reaches finish");
756});
757
758await test("welcome: native without an Eleven session records the device, not an account", async () => {
759 const h = harness({ homeSignedIn: false, canEnrol: false, envExtra: welcomeEnv() });
760 h.env.native = true;
761 h.env.accessBackedUp = () => false;
762 runFlow(WELCOME_FLOW, h.env, h.io);
763 await tick();
764 assert.deepEqual(h.records.find((r) => r.step === "access"),
765 { step: "access", label: "Your access lives on this device", value: "local" });
766 assert.equal(h.transcript.some((t) => /Before anything else/.test(t.text)), false, "no pitch");
767});
768
769// The quick-form fast path: both conversations' FIRST interactive step offers
770// a right-aligned "Fill in a form instead" chip whose tap opens the host form
771// (the openQuickForm verb) WITHOUT consuming the step — the conversation stays
772// answerable while the form is up (Cancel walks away scot-free).
773await test("quick form: the trailing fast-path chip on welcome access + setup name", async () => {
774 const h = harness({ envExtra: welcomeEnv() });
775 runFlow(WELCOME_FLOW, h.env, h.io);
776 await tick();
777 const chip = (h.lastChips || []).find((c) => c.label === "Fill in a form instead");
778 assert.ok(chip, "the welcome access step offers the quick-form chip");
779 assert.equal(chip.trailing, true, "flagged trailing — the renderer right-aligns it");
780 assert.equal((h.lastChips || []).find((c) => c.label === "Sign in (recommended)").trailing, undefined, "primary chips stay untagged");
781 await h.tap("Fill in a form instead");
782 assert.ok(h.verbLog.find((v) => v[0] === "openQuickForm"), "the tap fires the openQuickForm verb");
783 await h.tap("Sign in (recommended)"); // non-advancing: the step is still live after peeking at the form
784 assert.ok(h.verbLog.find((v) => v[0] === "savePasskey"));
785
786 const h2 = harness();
787 runFlow(SETUP_FLOW, h2.env, h2.io);
788 await tick();
789 const chip2 = (h2.lastChips || []).find((c) => c.label === "Fill in a form instead");
790 assert.ok(chip2, "the setup name step offers the quick-form chip");
791 assert.equal(chip2.trailing, true);
792 await h2.tap("Fill in a form instead");
793 assert.ok(h2.verbLog.find((v) => v[0] === "openQuickForm"));
794 await h2.answer("Paul"); // the typed answer still lands after the chip tap
795 assert.deepEqual(h2.verbLog.find((v) => v[0] === "setMyName"), ["setMyName", "Paul"]);
796});
797
798await test("welcome: invited fresh join — passkey probe, suggested name, photo nokey retry", async () => {
799 let photoTries = 0;
800 const h = harness({
801 verbs: { adoptProfilePhoto: () => (++photoTries === 1 ? "nokey" : true) },
802 envExtra: welcomeEnv({ suggested: "Luan", inviterName: "Ciara", hasPendingThread: true }),
803 });
804 h.env.accessBackedUp = () => h.enrolled;
805 runFlow(WELCOME_FLOW, h.env, h.io);
806 await tick();
807
808 // The greeting assumes nothing: inviter named, admin unknown → generic.
809 assert.match(h.transcript[0].text, /^Welcome! 👋 I'm Eleven, your guide here\./);
810 assert.match(h.transcript[0].text, /Ciara invited you to Test Space/);
811 assert.match(h.transcript[0].text, /run by one of its members/);
812 assert.match(h.transcript[0].text, /locked with keys only members hold/);
813
814 // Passkey first; the ceremony stays put → probe routes to the waiting step.
815 // (The step also arms a background settle poll; target the probe by interval.)
816 await h.tap("Sign in (recommended)");
817 const accessProbe = h.pendingDelays.find((d) => d.ms === 1500);
818 assert.ok(accessProbe, "the Save-my-access probe schedules a 1500ms check");
819 accessProbe.fn();
820 await tick();
821 assert.match(h.lastText(), /I'll wait here while you finish/);
822 h.enrolled = true;
823 await h.tap("Continue");
824 assert.deepEqual(h.records[0], { step: "access", label: "Signed in — your access is safe", value: "passkey" });
825
826 // The ack folds into the name question; the suggested-name chip is offered.
827 const nameQ = h.transcript.find((t) => t.extra.key === "q-name");
828 assert.match(nameQ.text, /^Perfect — you're all set\. 🔒\n\nFirst things first/);
829 await h.tap("I'm Luan");
830 assert.deepEqual(h.verbLog.find((v) => v[0] === "setMyName"), ["setMyName", "Luan"]);
831 assert.equal(h.records[1].label, "noted: you're Luan");
832
833 // Photo: instance key not here yet → honest nokey line, then a retry works.
834 const photoQ = h.transcript.find((t) => t.extra.key === "q-photo");
835 assert.match(photoQ.text, /^Lovely to meet you, Luan! 🤝\n\nWant to add a profile photo\?/);
836 await h.answer("me on holiday", { attachments: [{ type: "image/png" }] });
837 assert.ok(h.transcript.find((t) => /still finishing your secure setup/.test(t.text)));
838 assert.match(h.lastText(), /Ready when you are — send that photo again/);
839 await h.answer("me on holiday", { attachments: [{ type: "image/png" }] });
840 assert.deepEqual(h.verbLog.filter((v) => v[0] === "adoptProfilePhoto").length, 2);
841
842 // Preview is client-only with the round profile image; Keep records "photo".
843 assert.equal(h.transcript.at(-1).extra.img, "IMG($myAvatar)");
844 assert.equal(h.transcript.at(-1).extra.imgRound, true);
845 await h.tap("Keep this photo");
846 assert.deepEqual(h.records.find((r) => r.step === "photo"), { step: "photo", label: "set your profile photo", value: "" });
847 assert.deepEqual(h.records.at(-1), { step: "finish", label: "You're all set", value: "done" }, "finish persists its completion marker");
848
849 // Finish: welcome marked done, no backup nag (enrolled), inviter chip first.
850 assert.ok(h.verbLog.find((v) => v[0] === "markWelcomeDone"));
851 const fin = h.transcript.find((t) => t.extra.key === "q-finish");
852 assert.match(fin.text, /you're all set\. 🎉 Enjoy Test Space!/);
853 assert.equal(/One more reminder/.test(fin.text), false);
854 await h.tap("Say hi to Ciara");
855 assert.ok(h.verbLog.find((v) => v[0] === "openPendingThread"));
856 assert.ok(h.lastChips.find((c) => c.label === "Take the tour"));
857 assert.ok(h.ended);
858});
859
860await test("welcome: skip everything — nudge acks, photo skip disarms, nag on finish", async () => {
861 const h = harness({ envExtra: welcomeEnv({ space: { name: "", nameSet: false } }) });
862 h.env.accessBackedUp = () => false;
863 runFlow(WELCOME_FLOW, h.env, h.io);
864 await tick();
865
866 // No inviter → the generic invitation line, with the space-name fallback.
867 assert.match(h.transcript[0].text, /You've been invited to this space/);
868
869 await h.tap("Not now");
870 assert.ok(h.verbLog.find((v) => v[0] === "deferBackupReminders"), "declining defers the reminders");
871 const nameQ = h.transcript.find((t) => t.extra.key === "q-name");
872 assert.match(nameQ.text, /^No problem — I'll nudge you again in a day or so\.\n\nFirst things first/);
873 assert.deepEqual(nameQ.chips, [], "no suggested-name chip without a suggestion");
874
875 await h.answer("Paul");
876 await h.tap("Skip for now");
877 assert.equal(h.cancelled, 1, "photo skip disarms the waiter");
878
879 // Finish: skipped-photo comfort line + the backup nag; no pending-thread chip.
880 const fin = h.transcript.find((t) => t.extra.key === "q-finish");
881 assert.match(fin.text, /^No problem — you can add one anytime from your profile\./);
882 assert.match(fin.text, /One more reminder before you go/);
883 assert.deepEqual(h.lastChips.map((c) => c.label), ["Sign in", "Take the tour", "Done for now"],
884 "the parting sign-in chip rides the finish, and every finish offers a way to just be done");
885 // No open groups in this env — the mention and its chip stay hidden.
886 assert.equal(/open groups/.test(fin.text), false);
887});
888
889// "Where would you like to start?" used to offer only destinations, so being
890// finished had no answer (Paul, 2026-08-29).
891await test("both finishes offer 'Done for now', which says you're set", async () => {
892 for (const [flow, label] of [[SETUP_FLOW, "setup"], [WELCOME_FLOW, "welcome"]]) {
893 const h = harness({ done: { name: true, group: true, email: true, photo: true, invites: true,
894 extras: true, "ai-optin": true, plan: true, keys: true,
895 access: true, nudge: true, tour: true },
896 enrolled: true, envExtra: label === "welcome" ? welcomeEnv() : {} });
897 if (label === "welcome") h.env.accessBackedUp = () => true;
898 runFlow(flow, h.env, h.io);
899 await tick();
900 assert.ok(h.lastChips.map((c) => c.label).includes("Done for now"), `${label} offers it`);
901 await h.tap("Done for now");
902 assert.match(h.lastText(), /^You're all set\. 👍/, `${label} acknowledges it`);
903 }
904});
905
906await test("welcome: open groups get a finish mention + a Browse chip", async () => {
907 const h = harness({ enrolled: true, envExtra: welcomeEnv({ openGroups: true }) });
908 h.env.accessBackedUp = () => true;
909 runFlow(WELCOME_FLOW, h.env, h.io);
910 await tick();
911 await h.answer("Paul");
912 await h.tap("Skip for now"); // photo
913 const fin = h.transcript.find((t) => t.extra.key === "q-finish");
914 assert.match(fin.text, /This space also has open groups anyone here can join/);
915 await h.tap("Browse open groups");
916 assert.ok(h.verbLog.find((v) => v[0] === "openDiscover"), "the chip hands over to the directory");
917});
918
919await test("welcome: native embed — passkey step skipped, access secured by the sign-in, finish records completion", async () => {
920 // A native shell WITH an Eleven session — what "secured by the sign-in"
921 // actually rests on (a signed-out one gets the ask, see below).
922 const h = harness({ homeSignedIn: true, envExtra: welcomeEnv({ native: true, inviterName: "Ciara" }) });
923 h.env.accessBackedUp = () => true; // native + signed in: secured by that sign-in
924 runFlow(WELCOME_FLOW, h.env, h.io);
925 await tick();
926
927 // The passkey ask never renders; the access step records the native reason
928 // and the conversation goes straight from the greeting to the name.
929 assert.equal(h.transcript.some((t) => /Sign in with Eleven and your access is safe/.test(t.text)), false);
930 assert.deepEqual(h.records[0], { step: "access", label: "Your access is already secured by your Eleven sign-in", value: "native" });
931 const nameQ = h.transcript.find((t) => t.extra.key === "q-name");
932 assert.ok(nameQ);
933 // The question carries the composer's inset field label + an example hint.
934 assert.equal(nameQ.extra.label, "Your name:");
935 assert.equal(nameQ.extra.hint, "e.g. Finn Wolfhard");
936
937 // Run it to the end: finish persists its completion marker (what the native
938 // embed gates on — no localStorage marker there) and never nags about the
939 // access backup.
940 await h.answer("Paul");
941 // The photo step auto-skips under native (no attachment flow in the
942 // conversation composer yet) — finish follows the name directly.
943 const fin = h.transcript.find((t) => t.extra.key === "q-finish");
944 assert.ok(fin, "finish reached without a photo ask");
945 assert.equal(h.transcript.some((t) => t.extra.key === "q-photo"), false);
946 assert.equal(/One more reminder/.test(fin.text), false, "no sign-in nag — access is the sign-in");
947 assert.ok(h.records.find((r) => r.step === "finish" && r.value === "done"), "finish records the completion marker");
948});
949
950await test("welcome: passkey-return leg — richer ack, no generic re-greet, question never re-asked", async () => {
951 const h = harness({ enrolled: true, envExtra: welcomeEnv() });
952 h.env.accessBackedUp = () => true;
953 runFlow(WELCOME_FLOW, h.env, h.io);
954 await tick();
955
956 // Not "resumed" (nothing recorded, no name) so no "Welcome back —" line; the
957 // access entry supplies the richer ack instead, and the passkey question
958 // never renders.
959 assert.equal(h.transcript.some((t) => /^Welcome back — let's pick up/.test(t.text)), false);
960 assert.ok(h.transcript.find((t) => /^Great — you're signed in/.test(t.text)));
961 assert.equal(h.transcript.some((t) => /Sign in with Eleven and your access is safe/.test(t.text)), false);
962 assert.deepEqual(h.records[0], { step: "access", label: "Signed in — your access is safe", value: "passkey" });
963 assert.ok(h.transcript.find((t) => t.extra.key === "q-name"));
964
965 // A later reload (name set, access recorded): generic re-greet, hello skipped.
966 const h2 = harness({ enrolled: true, myName: "Paul", done: { access: true, name: true }, envExtra: welcomeEnv() });
967 h2.env.accessBackedUp = () => true;
968 runFlow(WELCOME_FLOW, h2.env, h2.io);
969 await tick();
970 assert.match(h2.transcript[0].text, /^Welcome back — let's pick up/);
971 assert.equal(h2.transcript.some((t) => /^Welcome! 👋/.test(t.text)), false, "hello step skipped on resume");
972 assert.ok(h2.transcript.find((t) => t.extra.key === "q-photo"), "resumes at the first undone step");
973});
974
975// The ceremony can finish in ANOTHER browsing context (iOS home-screen apps run
976// it in an in-app overlay; the native wrapper in the system browser) — the
977// completion poll flips bridge.enrolled while the flow sits on the WAITING
978// step. The waiting step's settle must then carry the flow on, with the access
979// step's proper record + ack, and never require the manual "Continue" tap.
980await test("welcome: enrolment confirmed while on access-waiting settles through", async () => {
981 const h = harness({ envExtra: welcomeEnv() });
982 runFlow(WELCOME_FLOW, h.env, h.io);
983 await tick();
984
985 await h.tap("Sign in (recommended)"); // fire-and-forget verb; page stays (stay mode)
986 const probe = h.pendingDelays.find((d) => d.ms === 1500);
987 assert.ok(probe, "the save chip arms its fall-to-waiting probe");
988 probe.fn();
989 await tick();
990 assert.ok(h.transcript.find((t) => /I'll wait here while you finish signing in/.test(t.text)), "fell to access-waiting");
991
992 // The poll confirms; access-waiting's settle re-enters access, whose entry
993 // records the backup and acks — no Continue tap.
994 h.enrolled = true;
995 const settle = h.pendingDelays.filter((d) => d.ms === 700).at(-1);
996 assert.ok(settle, "access-waiting arms a settle poll");
997 settle.fn();
998 await tick();
999 await tick();
1000 assert.deepEqual(h.records.find((r) => r.step === "access"),
1001 { step: "access", label: "Signed in — your access is safe", value: "passkey" });
1002 assert.ok(h.transcript.find((t) => /^Great — you're signed in/.test(t.text)));
1003 assert.ok(h.transcript.find((t) => t.extra.key === "q-name"), "carried on to the name step");
1004});
1005
1006await test("setup: enrolment confirmed while on keys-waiting settles through", async () => {
1007 const h = harness({
1008 myName: "Paul",
1009 done: { name: true, group: true, email: true, photo: true, invites: true, extras: true, "ai-optin": true },
1010 });
1011 runFlow(SETUP_FLOW, h.env, h.io);
1012 await tick();
1013 assert.ok(h.transcript.find((t) => /Now the most important thing of all/.test(t.text)), "keys pitch shown");
1014
1015 await h.tap("Sign in");
1016 const probe = h.pendingDelays.find((d) => d.ms === 1500);
1017 probe.fn();
1018 await tick();
1019 assert.ok(h.transcript.find((t) => /I'll wait here while you finish signing in/.test(t.text)), "fell to keys-waiting");
1020
1021 h.enrolled = true;
1022 const settle = h.pendingDelays.filter((d) => d.ms === 700).at(-1);
1023 assert.ok(settle, "keys-waiting arms a settle poll");
1024 settle.fn();
1025 await tick();
1026 await tick();
1027 assert.deepEqual(h.records.find((r) => r.step === "keys"),
1028 { step: "keys", label: "Signed in — your space's key is safe", value: "passkey" });
1029 assert.ok(h.transcript.find((t) => /your space's key is safe. ✅/.test(t.text)));
1030 assert.ok(h.verbLog.find((v) => v[0] === "markSetupComplete"), "reached finish without a tap");
1031 // Backed up → the finish bubble must NOT nag about coming back to it.
1032 assert.equal(h.transcript.some((t) => /One more reminder/.test(t.text)), false);
1033});
1034
1035// ---- native bundle drift gate ------------------------------------------------
1036// The Swift interpreter (ios/.../FlowEngine.swift) runs byte-derived copies of
1037// these flow documents (Flows/*.json). Regenerate after editing a flow:
1038// node -e 'import("./web/static/setup.flow.js").then(async m => { (await import("fs")).writeFileSync("ios/LChatCore/Sources/LChatCore/Flows/setup.json", JSON.stringify(m.default, null, 2) + "\n"); })'
1039// (and the welcome twin).
1040import { readFileSync } from "fs";
1041import { fileURLToPath } from "url";
1042import { dirname, join } from "path";
1043const __dir = dirname(fileURLToPath(import.meta.url));
1044await test("native flow bundles match the canonical flow documents", () => {
1045 for (const [name, flow] of [["setup", SETUP_FLOW], ["welcome", WELCOME_FLOW]]) {
1046 const bundled = JSON.parse(readFileSync(join(__dir, "..", "ios", "LChatCore", "Sources", "LChatCore", "Flows", `${name}.json`), "utf8"));
1047 assert.deepEqual(bundled, flow, `${name}.json drifted — regenerate from ${name}.flow.js (see comment above)`);
1048 }
1049});
1050