Skip to content
This repository was archived by the owner on Nov 4, 2026. It is now read-only.

Commit a4a85f5

Browse files
os-justinclaude
andauthored
test(plugin-tree): split the contractEnvelope-6839 waits by expected outcome (#8664)
* test(plugin-tree): split the contractEnvelope-6839 waits by expected outcome The file handed all three cases one wait — the table wrapper's mere presence — and read `tbody tr` the instant it passed. That testid is a MOUNT signal, but the rows arrive a commit later: `ObjectTree` keeps expansion in a `useState<Set<string>>(new Set())` mirror that a `useEffect` re-seeds from the forest, so the commit that first paints the table still carries the empty mirror and draws the root without its child. Probed on this fixture the DOM sequence is `loading -> table:1rows -> table:2rows`, and the old wait's first passing state was `table:1rows`, yielding 1 — the CI red, `expected 1 to be 2`. The positive arms now wait for the DESCENDANT row, the row the mirror gates, and assert the drawn shape plus the root toggle reading `Collapse`, so the pin is the seeded-open hierarchy rather than an eventual count. The refusal arm takes a settled read anchored on the "No records" panel, which the tree renders only after `loading` flips false, so it cannot pass by timing out on an absence. No timeout was raised, no assertion loosened. Part of objectui#6839 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YBWFb5YgMU5dw8p2VKj16S * chore(changeset): declare the plugin-tree envelope-wait test change as releasing nothing Empty frontmatter — `node scripts/check-changeset-presence.mjs` names this the explicit exemption for a test-only change under a released package's tree. Part of objectui#6839 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YBWFb5YgMU5dw8p2VKj16S --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5798df4 commit a4a85f5

2 files changed

Lines changed: 189 additions & 20 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
---
3+
4+
Test-only change in `@object-ui/plugin-tree`: the `contractEnvelope-6839` pin now
5+
splits its waits by expected outcome instead of reading the row count the instant
6+
the tree-grid wrapper mounts. No published behaviour changes.

‎packages/plugin-tree/src/ObjectTree.contractEnvelope-6839.test.tsx‎

Lines changed: 183 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,64 @@
2828
* ⚠️ The refusal case is ALSO satisfied by an `extractRecords` that returns
2929
* `[]` for everything — an implementation strictly worse than the bug. The
3030
* `data` and bare-array cases refuse it: same rows, same mount.
31+
*
32+
* ## ⭐ Why the two outcomes wait DIFFERENTLY
33+
*
34+
* This file used to hand all three cases ONE wait, and it went red on `main`
35+
* on PRs that cannot reach `plugin-tree` at all — `expected 1 to be 2`.
36+
*
37+
* That wait was `container.querySelector('[data-testid="object-tree"]') ??
38+
* queryByText('No records')`, and the row count was read the instant it passed.
39+
* The testid is on the TABLE WRAPPER, so it is a MOUNT signal, not a rows
40+
* signal — and the rows this file counts arrive at a LATER commit than the
41+
* table does.
42+
*
43+
* MEASURED, the mechanism is one race, in `ObjectTree` itself: expansion is a
44+
* `useState<Set<string>>(new Set())` MIRROR that a `useEffect` keyed on
45+
* `[roots, defaultExpandedDepth]` re-seeds from the forest. Rows are then
46+
* `flattenVisible(roots, expanded)`. So when `find()`'s rows land, the commit
47+
* that first paints the table still carries the PREVIOUS (empty) mirror: the
48+
* root draws, its child does not, and `tbody tr` is 1. The mirror is seeded in
49+
* the passive effect that follows, and a second commit takes it to 2. Probed
50+
* on this file's own fixture, the DOM sequence is exactly
51+
* `loading → table:1rows → table:2rows`, and the OLD wait's first passing state
52+
* was `table:1rows`, where it yielded 1 — the CI failure, by construction and
53+
* not by luck. Which of the two commits the read lands on is decided by machine
54+
* load, which is why it was green locally and red on a saturated shard.
55+
*
56+
* ⚠️ NOT the same shape as the `plugin-kanban` twin (objectui#8532 / PR #8533),
57+
* although it is the same family. That board had TWO independent races —
58+
* `React.lazy(() => import('./KanbanImpl'))` chunk reveal AND a prop-mirrored
59+
* `boardColumns` — and its symptom was `expected +0 to be 2`, nothing drawn at
60+
* all. `plugin-tree` has NO lazy boundary anywhere in its source (`index.tsx`
61+
* imports `./ObjectTree` eagerly), so only the mirrored-state half transfers,
62+
* and the symptom is a PARTIAL draw: the root without its child.
63+
*
64+
* The two outcomes therefore no longer share a wait:
65+
*
66+
* - the POSITIVE arms wait FOR the DESCENDANT row — the row that only exists
67+
* once the mirror has been seeded. That is the condition the race is
68+
* about, so waiting on it is what makes these arms immune to which commit
69+
* the read lands on. It is NOT a wider window on the same race, which is
70+
* all a raised timeout would have bought. They then assert the drawn SHAPE
71+
* (label + depth per row) plus the root's toggle reading `Collapse`, so the
72+
* pin is the seeded-open hierarchy and not merely "eventually 2 rows" — a
73+
* count alone is also satisfied by a tree that flattens everything.
74+
* - the REFUSAL arm cannot wait for an absence, so it takes a SETTLED read
75+
* anchored on something that DOES appear in that scenario: the tree's own
76+
* "No records" panel, which `ObjectTree` renders only once `loading` has
77+
* flipped false. Probed on the `records` fixture the sequence is
78+
* `loading → empty-state` with no table at any point, and the panel is
79+
* absent while loading — so this arm cannot pass by timing out on an
80+
* absence, which is the failure mode every absence-shaped pin has.
81+
*
82+
* ⛔ Do not fold these back into one wait, and ⛔ do not "fix" a future red here
83+
* with a longer timeout: the failure was never slowness, it was reading a
84+
* signal that does not carry the answer.
3185
*/
3286

3387
import React from 'react';
34-
import { render, screen, waitFor, cleanup } from '@testing-library/react';
88+
import { render, waitFor, cleanup, within, act } from '@testing-library/react';
3589
import { describe, it, expect, vi, afterEach } from 'vitest';
3690
import { ObjectTree } from './ObjectTree';
3791

@@ -66,21 +120,87 @@ const asData: Envelope = (rows) => ({ data: rows, total: rows.length });
66120
const asBareArray: Envelope = (rows) => rows;
67121
const asRecords: Envelope = (rows) => ({ records: rows, total: rows.length });
68122

123+
/** One row the forest actually painted, as the tree-grid describes it. */
124+
interface DrawnRow {
125+
readonly label: string;
126+
readonly depth: number;
127+
}
128+
69129
/**
70-
* Mount the tree over a `find()` answering `envelope`, and hand back what it
71-
* settled on: the number of rows it drew, or `'empty-state'` when it drew its
72-
* own "No records" panel instead.
130+
* Everything this file reads off a settled tree.
73131
*
74-
* The two outcomes are DIFFERENT DOM, not a count of 0 — `ObjectTree` returns
75-
* early on an empty forest and never mounts the table. Reporting which one
76-
* happened is what keeps a refused envelope distinguishable from a mount that
77-
* never rendered.
132+
* `tableDrawn` and `emptyPanelDrawn` are carried SEPARATELY rather than
133+
* collapsed into a row count: `ObjectTree` returns early on an empty forest and
134+
* never mounts the table, so a refused envelope and a mount that never rendered
135+
* are DIFFERENT DOM, not both "zero rows". Reporting which one happened is what
136+
* keeps them distinguishable.
137+
*/
138+
interface Settled {
139+
readonly rows: readonly DrawnRow[];
140+
readonly tableDrawn: boolean;
141+
readonly emptyPanelDrawn: boolean;
142+
/** The root row's toggle, by accessible name — the mirror's state, named. */
143+
readonly rootToggle: string | null;
144+
}
145+
146+
/** The rows the tree-grid has painted, in document order. */
147+
function drawnRows(container: HTMLElement): DrawnRow[] {
148+
return Array.from(
149+
container.querySelectorAll('tbody tr[data-testid="object-tree-row"]'),
150+
).map((tr) => ({
151+
label: (tr.querySelector('td')?.textContent ?? '').trim(),
152+
depth: Number(tr.getAttribute('data-depth')),
153+
}));
154+
}
155+
156+
/**
157+
* Has the tree drawn its own "No records" panel?
158+
*
159+
* ⚠️ `queryAllByText`, not `queryByText`: the singular form THROWS on multiple
160+
* matches as well as answering `null` on none, so it cannot express "how many"
161+
* without the throw becoming the result.
162+
*/
163+
function emptyPanelDrawn(container: HTMLElement): boolean {
164+
return within(container).queryAllByText('No records').length > 0;
165+
}
166+
167+
function readSettled(container: HTMLElement): Settled {
168+
const root = container.querySelector('tbody tr[data-depth="0"]');
169+
return {
170+
rows: drawnRows(container),
171+
tableDrawn: container.querySelector('[data-testid="object-tree"]') !== null,
172+
emptyPanelDrawn: emptyPanelDrawn(container),
173+
rootToggle: root?.querySelector('button')?.getAttribute('aria-label') ?? null,
174+
};
175+
}
176+
177+
/**
178+
* What a case expects the tree to settle on — which is also what decides HOW it
179+
* waits. See this file's header.
180+
*/
181+
type Outcome =
182+
/** Wait FOR that shape. `because` is carried into the timeout message. */
183+
| { readonly draws: readonly DrawnRow[]; readonly because: string }
184+
/** No absence to wait for: anchor on the empty panel, settle, then read. */
185+
| { readonly refuses: true };
186+
187+
const REFUSES: Outcome = { refuses: true };
188+
189+
/**
190+
* Mount the tree over a `find()` answering `envelope`, and hand back what it
191+
* settled on once `outcome` says it has settled.
78192
*
79193
* ⛔ Call ONCE per case, never inside a `waitFor` predicate (objectui#7802):
80194
* it renders, and `waitFor` re-runs its callback on DOM mutations, so a
81195
* predicate that renders feeds itself and leaks a container div per run.
196+
*
197+
* ⚠️ The predicates BELOW are inside `waitFor` on purpose and stay sound under
198+
* that same rule: `drawnRows` and `emptyPanelDrawn` are pure reads of a
199+
* container that is already mounted. They mount nothing, so re-running them on
200+
* a DOM mutation is free — which is exactly the property this helper itself
201+
* does not have.
82202
*/
83-
async function settledOn(envelope: Envelope): Promise<number | 'empty-state'> {
203+
async function settledOn(envelope: Envelope, outcome: Outcome): Promise<Settled> {
84204
const find = vi.fn(async () => envelope(ROWS));
85205
const ds: any = {
86206
find,
@@ -103,39 +223,82 @@ async function settledOn(envelope: Envelope): Promise<number | 'empty-state'> {
103223
// touches no DOM. Without it "no rows" is satisfied by the mount's initial
104224
// empty state, which every arm renders identically.
105225
await find.mock.results[0].value;
226+
227+
if ('refuses' in outcome) {
228+
// A refusal has no arrival to wait for, so this is a SETTLED read built
229+
// from the two things that CAN be observed: `find` has answered (above),
230+
// and the tree has drawn the "No records" panel — a node it renders only
231+
// after `loading` flips false, so it is a COMPLETION ANCHOR and not the
232+
// mere passage of time. `act` then flushes what React still had queued; it
233+
// is the opposite of widening a timeout.
234+
await waitFor(() =>
235+
expect(
236+
emptyPanelDrawn(container),
237+
'the tree must have drawn its own "No records" panel before it is read — without that anchor an absence assertion passes by timing out',
238+
).toBe(true),
239+
);
240+
await act(async () => {});
241+
return readSettled(container);
242+
}
243+
244+
// Wait on the SHAPE, whose deepest row is the one the expansion mirror gates.
245+
// The table wrapper appears a commit earlier, carrying the root alone.
106246
await waitFor(() =>
107-
expect(
108-
container.querySelector('[data-testid="object-tree"]') ??
109-
screen.queryByText('No records'),
110-
).not.toBeNull(),
247+
expect(drawnRows(container), outcome.because).toEqual(outcome.draws),
111248
);
112-
return container.querySelector('[data-testid="object-tree"]')
113-
? container.querySelectorAll('tbody tr').length
114-
: 'empty-state';
249+
return readSettled(container);
115250
}
116251

252+
/** Both positive arms draw the same seeded-open hierarchy. */
253+
const OPEN_FOREST: readonly DrawnRow[] = [
254+
{ label: 'Root', depth: 0 },
255+
{ label: 'Child', depth: 1 },
256+
];
257+
117258
afterEach(() => {
118259
cleanup();
119260
vi.clearAllMocks();
120261
});
121262

122263
describe('ObjectTree — the find() envelope it reads (objectui#6839)', () => {
123264
it("still reads the contract's `data` member", async () => {
124-
expect(await settledOn(asData), 'the declared rows member must still draw').toBe(2);
265+
const because = 'the declared rows member must still draw the whole forest';
266+
const settled = await settledOn(asData, { draws: OPEN_FOREST, because });
267+
expect(settled.rows, because).toEqual(OPEN_FOREST);
268+
// The CONDITION the wait above is keyed to, asserted rather than assumed:
269+
// the child is on screen because the root was seeded OPEN, not because the
270+
// tree flattens its forest regardless of expansion.
271+
expect(settled.rootToggle, 'the root must have settled open').toBe('Collapse');
272+
expect(settled.tableDrawn, 'the tree-grid must be the node that drew').toBe(true);
273+
expect(settled.emptyPanelDrawn, 'and not the empty panel').toBe(false);
125274
});
126275

127276
it('still reads a bare array — the live non-envelope shape fakes answer with', async () => {
128-
expect(await settledOn(asBareArray), 'the bare-array arm must still draw').toBe(2);
277+
const because = 'the bare-array arm must still draw the whole forest';
278+
const settled = await settledOn(asBareArray, { draws: OPEN_FOREST, because });
279+
expect(settled.rows, because).toEqual(OPEN_FOREST);
280+
expect(settled.rootToggle, 'the root must have settled open').toBe('Collapse');
281+
expect(settled.tableDrawn, 'the tree-grid must be the node that drew').toBe(true);
282+
expect(settled.emptyPanelDrawn, 'and not the empty panel').toBe(false);
129283
});
130284

131285
it('does NOT read `records` — not a QueryResult member', async () => {
132286
// Before the fix these two nodes drew off a key `QueryResult` does not
133287
// declare, and did so AHEAD of `data`. The tree now settles on its own
134288
// "No records" panel — a DIFFERENT node from the table, which is what makes
135289
// this a reading of the refusal rather than of a mount that never happened.
290+
const settled = await settledOn(asRecords, REFUSES);
136291
expect(
137-
await settledOn(asRecords),
292+
settled.emptyPanelDrawn,
293+
'the refusal must be OBSERVED: the tree drew its "No records" panel',
294+
).toBe(true);
295+
expect(
296+
settled.rows,
138297
'a `records` envelope must reach the forest as no rows at all, not as the rows it names',
139-
).toBe('empty-state');
298+
).toEqual([]);
299+
expect(
300+
settled.tableDrawn,
301+
'a refused envelope must not mount the tree-grid at all',
302+
).toBe(false);
140303
});
141304
});

0 commit comments

Comments
 (0)