Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

Merge latest dev into sequence note wrapping

tt-a1ia0f08a647529
diff --cc archify.zip
index bc48e7df,4d998eae..acf9599d
Binary files differ
diff --cc archify/renderers/sequence/render-sequence.mjs
index 9d11f695,7feb167b..cfb52e8b
--- a/archify/renderers/sequence/render-sequence.mjs
+++ b/archify/renderers/sequence/render-sequence.mjs
@@@ -46,21 -47,51 +47,40 @@@ function legendEntries()
// needs LEGEND_BLOCK_HEIGHT (title glyphs, row, and the 54px baseline inset).
const LEGEND_CONTENT_GAP = 12;
const LEGEND_BLOCK_HEIGHT = 86;
-const contentBottom = Math.max(
- 0,
- ...asArray(sequence.messages).map((message) => message.y + (message.note ? 22 : 6)),
- ...asArray(sequence.activations).map((activation) => activation.to),
- ...asArray(sequence.segments).map((segment) => segment.to),
-);
-function legendRequiredHeight(width) {
- const entries = legendEntries();
- if (!entries.length) return 0;
- return Math.ceil(contentBottom + LEGEND_CONTENT_GAP + LEGEND_BLOCK_HEIGHT
- + legendFootprint(entries, { width: width - 80 }).extraHeight);
-}
+
+ // A renderer-sized spread canvas widens until every participant label fits,
+ // but never past the width where 7px sublabels would project below the
+ // desktop reading minimum; an inherently crowded row still fails below.
+ const spreadParticipantWidth = (canvasWidth) => Math.max(86,
+ Math.min(190, Math.round((canvasWidth - 124) / Math.max(1, asArray(sequence.participants).length)) - 24));
+ const readableCanvasWidth = Math.floor(DESKTOP_READER_DIAGRAM_WIDTH * participantTextFit.sublabelPreferred / MIN_PROJECTED_NODE_TEXT_PX);
+ function automaticCanvasWidth() {
+ const participantsFit = (width) => asArray(sequence.participants).every((participant) => (
+ textUnits(participant.label) * 6.8 <= width + 6
+ && (!participant.sublabel || minimumNodeTextWidth(participant.sublabel, participantTextFit.sublabelMinimum) <= availableNodeTextWidth(width))
+ ));
+ if ((sequence.meta?.column_fit || 'spread') !== 'spread' || participantsFit(spreadParticipantWidth(920))) return 920;
+ const needed = Math.max(...asArray(sequence.participants).map((participant) => Math.max(
+ textUnits(participant.label) * 6.8 - 6,
+ participant.sublabel ? minimumNodeTextWidth(participant.sublabel, participantTextFit.sublabelPreferred) + 86 - availableNodeTextWidth(86) : 0,
+ )));
+ const count = Math.max(1, asArray(sequence.participants).length);
+ return Math.min(readableCanvasWidth, Math.max(920, Math.ceil((Math.min(190, needed) + 25) * count + 124)));
+ }
-// A renderer-sized canvas grows to keep the legend clear of late messages;
-// an authored viewBox is honored and validated below.
+ const automaticWidth = sequence.meta?.viewBox ? null : automaticCanvasWidth();
-const viewBox = sequence.meta?.viewBox || [automaticWidth, Math.max(760, legendRequiredHeight(automaticWidth))];
-// The timeline scales with viewBox height: a taller viewBox gains message room,
-// a shorter one shrinks the readable band (validated below) instead of clipping.
++
+// Column geometry depends only on the canvas width, which is known before the
+// automatic height is: notes wrap inside lane gaps, and their wrapped height
+// feeds that automatic height.
// `column_fit: "spread"` widens the lanes with the viewBox instead of keeping
// the fixed 108px gap, so a wide canvas gains column distance and label room
// rather than dead space on the right. Spread is the default on both automatic
// and authored canvases; explicit fixed retains historical coordinates.
- const canvasWidth = sequence.meta?.viewBox?.[0] ?? 920;
++const canvasWidth = sequence.meta?.viewBox?.[0] ?? automaticWidth;
const columnFit = sequence.meta?.column_fit || 'spread';
const participantCount = Math.max(1, asArray(sequence.participants).length);
const preferredSideMargin = 62;
- const participantW = columnFit === 'spread'
- ? Math.max(86, Math.min(190, Math.round((canvasWidth - preferredSideMargin * 2) / participantCount) - 24))
- : 86;
-const participantW = columnFit === 'spread' ? spreadParticipantWidth(viewBox[0]) : 86;
++const participantW = columnFit === 'spread' ? spreadParticipantWidth(canvasWidth) : 86;
// Narrow feasible frames can reduce the left margin, while ordinary frames
// keep 62px. Compute card width first so this does not change its sizing rule.
const minimumParticipantSpan = participantCount * participantW + (participantCount - 1) * 16;
@@@ -70,140 -101,8 +90,140 @@@ const sideMargin = columnFit === 'sprea
// Fit the authored width when feasible, preserving a real 16px card gutter.
// Infeasible frames retain that minimum and fail the capacity check below.
const colGap = columnFit === 'spread' && participantCount > 1
- ? Math.max(participantW + 16, (viewBox[0] - 40 - sideMargin - participantW) / (participantCount - 1))
+ ? Math.max(participantW + 16, (canvasWidth - 40 - sideMargin - participantW) / (participantCount - 1))
: 108;
+const leftX = columnFit === 'spread' ? sideMargin + participantW / 2 : sideMargin;
+const participantIndexById = new Map(asArray(sequence.participants).map((participant, index) => [participant.id, index]));
+
+function participantX(index) {
+ return leftX + index * colGap;
+}
+
+// A note wraps inside one gap between neighbouring lifelines, so it never
+// crosses an unrelated lifeline or activation bar (#676). Every character
+// stays visible, including in image exports: long unbroken text (a URL or
+// path) breaks after its punctuation, otherwise at any character.
+const NOTE_FONT_SIZE = 7;
+const NOTE_LINE_HEIGHT = NOTE_FONT_SIZE + 4;
+// The first line keeps its original position: 12px past the arrow start,
+// which sits 7px from the lifeline.
+const NOTE_ARROW_INSET = 7;
+const NOTE_TEXT_INSET = 12;
+// Activation bars reach 5px past a lifeline; keep 6px of air beyond them.
+const NOTE_LIFELINE_CLEARANCE = 11;
+// Spread columns share one computed gap, but subtracting floating-point column
+// centres can differ in the last digits; such gaps still count as equal.
+const NOTE_GAP_TOLERANCE = 0.5;
+const NOTE_BREAK_AFTER = new Set(['/', '.', '-', '?', '&', '=', '#', '_']);
+const noteSegmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
+const noteLayouts = new Map();
+
+function noteTextWidth(text) {
+ return minimumNodeTextWidth(text, NOTE_FONT_SIZE);
+}
+
+// Index after the last punctuation break in `piece`, or its full length.
+function noteBreakIndex(piece) {
+ for (let index = piece.length - 1; index > 0; index -= 1) {
+ if (NOTE_BREAK_AFTER.has(piece[index - 1])) return index;
+ }
+ return piece.length;
+}
+
+function wrapNoteText(text, width) {
+ const lines = [];
+ let line = '';
+ const flush = () => {
+ const trimmed = line.trimEnd();
+ if (trimmed) lines.push(trimmed);
+ line = '';
+ };
+ for (const word of String(text).match(/\s+|\S+/gu) || []) {
+ if (/^\s+$/u.test(word)) {
+ if (line) line += ' ';
+ continue;
+ }
+ if (noteTextWidth(line + word) <= width) {
+ line += word;
+ continue;
+ }
+ flush();
+ let piece = '';
+ for (const { segment } of noteSegmenter.segment(word)) {
+ while (piece && noteTextWidth(piece + segment) > width) {
+ const cut = noteBreakIndex(piece);
+ lines.push(piece.slice(0, cut));
+ piece = piece.slice(cut);
+ }
+ piece += segment;
+ }
+ line = piece;
+ }
+ flush();
+ return lines;
+}
+
+// The widest gap the message spans; equal gaps resolve to the left-most one,
+// where a note has always started, so notes keep their original position.
+function noteGapIndex(fromIndex, toIndex) {
+ const low = Math.min(fromIndex, toIndex);
+ const high = Math.max(fromIndex, toIndex);
+ let best = null;
+ for (let gap = low; gap < high; gap += 1) {
+ const width = participantX(gap + 1) - participantX(gap);
+ if (!best || width > best.width + NOTE_GAP_TOLERANCE) best = { gap, width };
+ }
+ return best?.gap ?? null;
+}
+
+function noteRect(message) {
+ const note = noteLayout(message);
+ return note && { x: note.x, y: note.top, width: note.width, height: note.bottom - note.top };
+}
+
+function noteLayout(message) {
+ if (noteLayouts.has(message)) return noteLayouts.get(message);
+ let result = null;
+ const fromIndex = participantIndexById.get(message.from);
+ const toIndex = participantIndexById.get(message.to);
+ if (message.note && typeof message.y === 'number' && fromIndex !== undefined && toIndex !== undefined && fromIndex !== toIndex) {
+ const gap = noteGapIndex(fromIndex, toIndex);
+ const x = participantX(gap) + NOTE_ARROW_INSET + NOTE_TEXT_INSET;
+ const maxWidth = Math.max(1, participantX(gap + 1) - NOTE_LIFELINE_CLEARANCE - x);
+ const lines = noteTextWidth(message.note) <= maxWidth ? [message.note] : wrapNoteText(message.note, maxWidth);
+ const baseline = message.y + 18;
+ result = {
+ x,
+ baseline,
+ lines,
+ width: Math.max(...lines.map(noteTextWidth)),
+ top: baseline - NOTE_FONT_SIZE,
+ bottom: message.y + 22 + (lines.length - 1) * NOTE_LINE_HEIGHT,
+ };
+ }
+ noteLayouts.set(message, result);
+ return result;
+}
+
+const contentBottom = Math.max(
+ 0,
+ ...asArray(sequence.messages).map((message) => (message.note
+ ? noteLayout(message)?.bottom ?? message.y + 22
+ : message.y + 6)),
+ ...asArray(sequence.activations).map((activation) => activation.to),
+ ...asArray(sequence.segments).map((segment) => segment.to),
+);
+function legendRequiredHeight(width) {
+ const entries = legendEntries();
+ if (!entries.length) return 0;
+ return Math.ceil(contentBottom + LEGEND_CONTENT_GAP + LEGEND_BLOCK_HEIGHT
+ + legendFootprint(entries, { width: width - 80 }).extraHeight);
+}
+// A renderer-sized canvas grows to keep the legend clear of late messages;
+// an authored viewBox is honored and validated below.
- const viewBox = sequence.meta?.viewBox || [920, Math.max(760, legendRequiredHeight(920))];
++const viewBox = sequence.meta?.viewBox || [automaticWidth, Math.max(760, legendRequiredHeight(automaticWidth))];
+// The timeline scales with viewBox height: a taller viewBox gains message room,
+// a shorter one shrinks the readable band (validated below) instead of clipping.
// Showcase is the fast-authoring default; standard retains legacy label geometry.
const readableMessages = sequence.meta?.quality_profile === 'showcase';
diff --cc test/sequence-note-wrap.test.mjs
index 9db6de9a,00000000..f8530723
mode 100644,000000..100644
--- a/test/sequence-note-wrap.test.mjs
+++ b/test/sequence-note-wrap.test.mjs
@@@ -1,295 -1,0 +1,325 @@@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { spawnSync } from 'node:child_process';
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { minimumNodeTextWidth } from '../archify/renderers/shared/text-fit.mjs';
+
+// Sequence message notes wrap inside one gap between neighbouring lifelines and
+// keep every character (#676).
+const skillRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'archify');
+const cli = path.join(skillRoot, 'bin/archify.mjs');
+const LONG_NOTE = 'this note explains the whole retry and backoff policy in a lot of detail so it is very long';
+const URL_NOTE = 'see https://api.example.com/v2/users/profile/settings/notifications/email-preferences';
+
+function sequence({ messages, participants = ['client', 'api', 'db'], quality = 'showcase', activations } = {}) {
+ return {
+ schema_version: 1,
+ diagram_type: 'sequence',
+ meta: { title: 'Note wrap', output: 'note.html', quality_profile: quality, column_fit: 'spread' },
+ participants: participants.map((id) => ({ id, type: 'backend', label: id.toUpperCase() })),
+ messages,
+ ...(activations ? { activations } : {}),
+ };
+}
+
+function run(t, command, spec) {
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'archify-sequence-note-'));
+ t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
+ const input = path.join(dir, 'candidate.json');
+ const output = path.join(dir, 'note.html');
+ fs.writeFileSync(input, JSON.stringify(spec));
+ const args = command === 'render'
+ ? [cli, 'render', 'sequence', input, output]
+ : [cli, 'validate', 'sequence', input, '--json'];
+ const result = spawnSync(process.execPath, args, { cwd: skillRoot, encoding: 'utf8', maxBuffer: 16 * 1024 * 1024 });
+ return { result, html: command === 'render' && result.status === 0 ? fs.readFileSync(output, 'utf8') : '' };
+}
+
+function render(t, spec) {
+ const { result, html } = run(t, 'render', spec);
+ assert.equal(result.status, 0, result.stdout + result.stderr);
+ return html;
+}
+
+function lifelines(html) {
+ return [...html.matchAll(/<path d="M ([\d.]+) 142 L \1 [\d.]+" class="a-default" stroke-width="0.8" stroke-dasharray="3,7"\/>/g)]
+ .map((match) => Number(match[1]));
+}
+
+function notes(html) {
+ return [...html.matchAll(/<text data-detail="fine" x="([\d.]+)" y="([\d.]+)" class="t-dim" font-size="7">([\s\S]*?)<\/text>/g)]
+ .map((match) => {
+ const tspans = [...match[3].matchAll(/<tspan x="([\d.]+)" dy="([\d.]+)">([^<]*)<\/tspan>/g)];
+ return {
+ x: Number(match[1]),
+ y: Number(match[2]),
+ raw: match[3],
+ lines: tspans.length ? tspans.map((line) => line[3]) : [match[3]],
+ tspanX: tspans.map((line) => Number(line[1])),
+ dy: tspans.map((line) => Number(line[2])),
+ };
+ });
+}
+
+function decode(text) {
+ return text.replaceAll('&amp;', '&');
+}
+
+test('the reported long note wraps inside its own lane and keeps every word', (t) => {
+ const html = render(t, sequence({
+ messages: [
+ { id: 'request', from: 'client', to: 'api', y: 200, label: 'request', note: LONG_NOTE },
+ { id: 'reply', from: 'api', to: 'client', y: 380, label: 'reply', variant: 'return' },
+ ],
+ activations: [{ participant: 'api', from: 190, to: 390 }],
+ }));
+ const [client, api] = lifelines(html);
+ const [note] = notes(html);
+ assert.ok(note.lines.length > 1, 'the long note wraps');
+ assert.equal(note.lines.join(' '), LONG_NOTE, 'no word is dropped or shortened');
+ assert.equal(note.x, client + 19, 'the first line keeps its original position');
+ assert.deepEqual(note.dy, note.lines.map((_, index) => (index ? 11 : 0)));
+ for (const line of note.lines) {
+ assert.ok(note.x + minimumNodeTextWidth(line, 7) <= api - 11, `"${line}" stays clear of the API activation bar`);
+ }
+});
+
+test('long unbroken text breaks after URL punctuation and keeps every character', (t) => {
+ const html = render(t, sequence({
+ participants: ['client', 'api', 'db', 'cache', 'queue'],
+ messages: [{ id: 'link', from: 'client', to: 'api', y: 200, label: 'docs', note: URL_NOTE }],
+ }));
+ const [client, api] = lifelines(html);
+ const [note] = notes(html);
+ assert.ok(note.lines.length > 2, 'the address spans several lines');
+ assert.equal(note.lines[0], 'see');
+ assert.equal(note.lines.slice(1).join(''), URL_NOTE.slice('see '.length), 'every character of the address is kept');
+ for (const line of note.lines.slice(1, -1)) {
+ assert.match(line, /[/.\-?&=#_]$/, `"${line}" breaks after punctuation`);
+ }
+ for (const line of note.lines) {
+ assert.ok(client + 19 + minimumNodeTextWidth(line, 7) <= api - 11, `"${line}" fits its lane`);
+ }
+});
+
+test('text without any punctuation breaks at a character boundary', (t) => {
+ const token = 'x'.repeat(160);
+ const html = render(t, sequence({ messages: [{ from: 'client', to: 'api', y: 200, label: 'token', note: token }] }));
+ const [note] = notes(html);
+ assert.ok(note.lines.length > 1);
+ assert.equal(note.lines.join(''), token);
+});
+
+test('a short note keeps its original single-line markup', (t) => {
+ const html = render(t, sequence({ messages: [{ from: 'api', to: 'client', y: 200, label: 'reply', note: 'cached for 5 minutes' }] }));
+ const [client] = lifelines(html);
+ assert.match(html, new RegExp(`<text data-detail="fine" x="${client + 19}" y="218" class="t-dim" font-size="7">cached for 5 minutes</text>`));
+ assert.doesNotMatch(html, /<tspan/);
+});
+
+test('a note on a message that skips participants stays in the left-most gap, in either direction', (t) => {
+ for (const [from, to] of [['client', 'db'], ['db', 'client']]) {
+ const html = render(t, sequence({ messages: [{ from, to, y: 200, label: 'lookup', note: LONG_NOTE }] }));
+ const [client, api] = lifelines(html);
+ const [note] = notes(html);
+ assert.equal(note.x, client + 19, `${from} → ${to} starts where notes always started`);
+ for (const line of note.lines) {
+ assert.ok(note.x + minimumNodeTextWidth(line, 7) <= api - 11, `${from} → ${to}: "${line}" stays clear of the skipped API lifeline`);
+ }
+ }
+});
+
+test('a short note on a reverse message that skips a participant keeps its original position', (t) => {
+ const html = render(t, sequence({ messages: [{ from: 'db', to: 'client', y: 200, label: 'result', note: 'from replica' }] }));
+ const [client] = lifelines(html);
+ assert.match(html, new RegExp(`<text data-detail="fine" x="${client + 19}" y="218" class="t-dim" font-size="7">from replica</text>`));
+});
+
+test('equal spread gaps that differ only by floating-point rounding keep the left-most gap', (t) => {
+ // Six and seven spread columns produce middle gaps such as 141.80000000000007.
+ for (const count of [6, 7]) {
+ const ids = Array.from({ length: count }, (_, index) => `p${index}`);
+ for (const [from, to] of [[ids.at(-1), ids[0]], [ids[0], ids.at(-1)]]) {
+ const html = render(t, sequence({ participants: ids, messages: [{ from, to, y: 200, label: 'reply', note: 'ok' }] }));
+ const [first] = lifelines(html);
+ const [note] = notes(html);
+ assert.ok(Math.abs(note.x - (first + 19)) < 1e-9, `${count} participants, ${from} → ${to}: note at ${note.x}, expected ${first + 19}`);
+ }
+ }
+});
+
+// Fixed columns keep the first note beside the segment label column at x=56.
+test('a segment label steps up clear of a wrapped note', (t) => {
+ const spec = sequence({
+ messages: [{ from: 'client', to: 'api', y: 200, label: 'request', note: 'retries with backoff when the cache misses' }],
+ });
+ spec.meta.column_fit = 'fixed';
+ spec.segments = [{ label: 'Fallback', from: 260, to: 360 }];
+ const { result } = run(t, 'validate', spec);
+ assert.equal(result.status, 0, result.stdout);
+ const html = render(t, spec);
+ const [note] = notes(html);
+ const labelY = Number(html.match(/<rect x="56" y="(-?[\d.]+)" width="[\d.]+" height="18"/)[1]);
+ const noteTop = note.y - 7;
+ assert.ok(labelY + 18 + 2 <= noteTop || labelY >= note.y + (note.lines.length - 1) * 11 + 4,
+ `segment label at y=${labelY} clears the note from ${noteTop}`);
+});
+
+// Read one structured diagnostic from `validate --json` and apply its single
+// supported fix ("set /json/pointer to value") to a copy of the input.
+function failure(t, spec, code) {
+ const { result } = run(t, 'validate', spec);
+ assert.notEqual(result.status, 0, 'validate must fail');
+ const diagnostic = JSON.parse(result.stdout).diagnostics.find((entry) => entry.code === code);
+ assert.ok(diagnostic, `expected ${code} in ${result.stdout}`);
+ assert.equal(diagnostic.severity, 'error');
+ return diagnostic;
+}
+
+function applyFix(spec, fix) {
+ const [, pointer, value] = fix.match(/^set (\/\S+) to (.+)$/);
+ const copy = structuredClone(spec);
+ const keys = pointer.slice(1).split('/');
+ const parent = keys.slice(0, -1).reduce((node, key) => {
+ node[key] ??= {};
+ return node[key];
+ }, copy);
+ parent[keys.at(-1)] = JSON.parse(value);
+ return copy;
+}
+
+test('a segment label left covering a message label reports it with a verified fix', (t) => {
+ // The reviewer's case: the wrapped note pushes the label up onto "request".
+ const spec = sequence({ messages: [{ from: 'client', to: 'api', y: 200, label: 'request', note: LONG_NOTE }] });
+ spec.meta.column_fit = 'fixed';
+ spec.segments = [{ label: 'Fallback', from: 280, to: 360 }];
+ const diagnostic = failure(t, spec, 'sequence/segment-label-overlap');
+ assert.deepEqual(diagnostic.subject, { diagramType: 'sequence', segment: 'Fallback', path: '/segments/0/from' });
+ assert.equal(diagnostic.evidence.attempts, 4);
+ assert.equal(diagnostic.evidence.covered.kind, 'message label');
+ assert.equal(diagnostic.evidence.covered.path, '/messages/0/label');
+ assert.equal(diagnostic.evidence.covered.message, 'request');
+ assert.equal(diagnostic.supportedFixes.length, 1);
+ assert.match(diagnostic.supportedFixes[0], /^set \/segments\/0\/from to \d+$/);
+ const repaired = run(t, 'validate', applyFix(spec, diagnostic.supportedFixes[0])).result;
+ assert.equal(repaired.status, 0, repaired.stdout);
+});
+
+test('a segment label with no clear position names the note it would cover', (t) => {
+ const spec = sequence({
+ messages: [
+ { from: 'client', to: 'api', y: 160, label: 'open' },
+ { from: 'client', to: 'api', y: 200, label: 'request', note: LONG_NOTE.repeat(3) },
+ ],
+ });
+ spec.meta.column_fit = 'fixed';
+ spec.segments = [{ label: 'Fallback', from: 360, to: 400 }];
+ const diagnostic = failure(t, spec, 'sequence/segment-label-overlap');
+ assert.equal(diagnostic.evidence.covered.kind, 'note');
+ assert.equal(diagnostic.evidence.covered.path, '/messages/1/note');
+ for (const fix of diagnostic.supportedFixes) {
+ assert.equal(run(t, 'validate', applyFix(spec, fix)).result.status, 0, `${fix} must hold`);
+ }
+});
+
+test('a wrapped note below an authored canvas reports the height it needs', (t) => {
+ const spec = (height) => ({
+ ...sequence({ messages: [{ id: 'late', from: 'client', to: 'api', y: 660, label: 'late', note: LONG_NOTE.repeat(3) }] }),
+ meta: { title: 'Fixed canvas', output: 'note.html', quality_profile: 'showcase', column_fit: 'spread', viewBox: [920, height], legend: { mode: 'hidden' } },
+ });
+ const diagnostic = failure(t, spec(760), 'sequence/note-canvas-limit');
+ assert.equal(diagnostic.subject.path, '/messages/0/note');
+ assert.equal(diagnostic.subject.message, 'late');
+ assert.equal(diagnostic.evidence.viewBoxHeight, 760);
+ assert.equal(diagnostic.evidence.canvasLimitY, 760 - 65 + 20);
+ assert.ok(diagnostic.evidence.note.y + diagnostic.evidence.note.height > diagnostic.evidence.canvasLimitY);
+ const required = diagnostic.evidence.requiredViewBoxHeight;
+ assert.deepEqual(diagnostic.supportedFixes, [`set /meta/viewBox/1 to ${required}`]);
+ const repaired = run(t, 'validate', applyFix(spec(760), diagnostic.supportedFixes[0])).result;
+ assert.equal(repaired.status, 0, repaired.stdout);
+ const standard = spec(760);
+ standard.meta.quality_profile = 'standard';
+ assert.equal(run(t, 'validate', standard).result.status, 0, 'standard keeps accepting the authored canvas');
+});
+
+test('a wrapped note that reaches the next message names the message and the y it needs', (t) => {
+ const crowded = (replyY) => sequence({
+ participants: ['client', 'api', 'db', 'cache', 'queue'],
+ messages: [
+ { id: 'request', from: 'client', to: 'api', y: 200, label: 'request', note: LONG_NOTE },
+ { id: 'reply', from: 'api', to: 'client', y: replyY, label: 'reply', variant: 'return' },
+ ],
+ });
+ const diagnostic = failure(t, crowded(230), 'sequence/note-overlap');
+ assert.match(diagnostic.message, /Note on message "request" wraps to \d+ lines/);
+ assert.deepEqual(diagnostic.subject, { diagramType: 'sequence', message: 'request', path: '/messages/0/note', from: 'client', to: 'api' });
+ assert.equal(diagnostic.evidence.blocker.path, '/messages/1/y');
+ assert.equal(diagnostic.evidence.blocker.message, 'reply');
+ assert.equal(diagnostic.evidence.blocker.y, 230);
+ assert.ok(diagnostic.evidence.lines > 1);
+ const requiredY = diagnostic.evidence.requiredY;
+ assert.ok(requiredY > 230);
+ assert.deepEqual(diagnostic.supportedFixes, [`set /messages/1/y to ${requiredY}`]);
+
+ const repaired = run(t, 'validate', applyFix(crowded(230), diagnostic.supportedFixes[0])).result;
+ assert.equal(repaired.status, 0, repaired.stdout);
+ const html = render(t, crowded(requiredY));
+ assert.equal(decode(notes(html)[0].lines.join(' ')), LONG_NOTE);
+});
+
+test('standard keeps accepting a crowded note', (t) => {
+ const spec = sequence({
+ quality: 'standard',
+ messages: [
+ { from: 'client', to: 'api', y: 200, label: 'request', note: LONG_NOTE },
+ { from: 'api', to: 'client', y: 230, label: 'reply', variant: 'return' },
+ ],
+ });
+ assert.equal(run(t, 'validate', spec).result.status, 0);
+});
+
+test('an automatic canvas grows to hold a wrapped note on the last message', (t) => {
+ const html = render(t, sequence({
+ participants: ['client', 'api', 'db', 'cache', 'queue'],
+ messages: [{ from: 'client', to: 'api', y: 700, label: 'late', note: LONG_NOTE.repeat(2) }],
+ }));
+ const height = Number(html.match(/<svg viewBox="0 0 920 (\d+)"/)[1]);
+ const [note] = notes(html);
+ const lastBaseline = note.y + (note.lines.length - 1) * 11;
+ const legendTitle = Number(html.match(/<text x="[\d.]+" y="([\d.]+)"[^>]*>Legend<\/text>/)[1]);
+ assert.ok(lastBaseline + 2 < legendTitle - 12, `last note line ${lastBaseline} stays above the legend title ${legendTitle}`);
+ assert.ok(height > 760);
+});
++
++test('a widened automatic canvas wraps a late note using its final lane geometry', (t) => {
++ const ids = Array.from({ length: 8 }, (_, index) => `p${index}`);
++ const token = 'x'.repeat(160);
++ const spec = sequence({
++ participants: ids,
++ messages: [{ from: 'p1', to: 'p2', y: 700, label: 'late', note: token }],
++ });
++ // This dev-supported label widens the automatic canvas beyond 920px.
++ spec.participants[4].label = 'Team operations';
++ const html = render(t, spec);
++ const [, widthText, heightText] = html.match(/<svg viewBox="0 0 (\d+) (\d+)"/);
++ const width = Number(widthText);
++ const height = Number(heightText);
++ assert.ok(width > 920 && width <= 1085, `automatic width ${width} stays within the readable bound`);
++ const lanes = lifelines(html);
++ const [note] = notes(html);
++ assert.equal(note.x, lanes[1] + 19, 'note starts in the final sender lane');
++ assert.ok(note.lines.length > 1);
++ assert.equal(note.lines.join(''), token);
++ for (const line of note.lines) {
++ assert.ok(note.x + minimumNodeTextWidth(line, 7) <= lanes[2] - 11, `${line} clears the final next lifeline`);
++ }
++ const lastBaseline = note.y + (note.lines.length - 1) * 11;
++ const legendTitle = Number(html.match(/<text x="[\d.]+" y="([\d.]+)"[^>]*>Legend<\/text>/)[1]);
++ assert.ok(lastBaseline + 2 < legendTitle - 12, 'automatic height includes the wrapped note');
++ const authored = render(t, { ...spec, meta: { ...spec.meta, viewBox: [width, height] } });
++ assert.deepEqual(lifelines(authored), lanes, 'authored and computed canvas widths give the same lanes');
++ assert.deepEqual(notes(authored), notes(html), 'note wrapping uses the final width before automatic height');
++});