Skip to content

Commit 87c0407

Browse files
test(auth): AuthProvider 401 re-check case waits on the re-check read with an explicit chain budget (#32433)
Fixes #32426 Refs #32420 Clause-②: no Metadata-protocol: none ## 维护者速读 - 改了什么:AuthProvider 的「401 后重查会话」测试用例,改为先等重查请求真正发出,再等身份被清掉,等待预算由默认 1000 ms 改为显式的 10 s。 - 为什么改:调度全量跑里该用例在 Test UI 分片负载下耗时 1022 ms 才失败,原因是等待预算过紧,不是产品时序缺陷。 - 风险与回滚:仅改测试,断言一字未动;回滚即还原该文件。 - 先看:`ui/packages/auth/src/__tests__/AuthProvider.test.tsx` 中 `CHAIN_MS` 常量与「the server answers "no session"」用例。 - 两分钟验证:在 `ui/` 下跑 `pnpm exec vitest run packages/auth/src/__tests__/AuthProvider.test.tsx`,预期 20 个用例全绿。 - 需要你做的:无,席位落地。 ## What changes `ui/packages/auth/src/__tests__/AuthProvider.test.tsx`, test-only (`skip-changeset`): - The "the server answers no session" case now first waits on the event it asserts on (the re-check `get-session` read reaching the fetch seam), then waits for identity to clear. Both assertions are unchanged; the trailing read-count `expect` is kept. - The `waitFor` calls in the 401 describe (`signedIn`, the three re-check cases) carry an explicit `CHAIN_MS = 10_000` budget with the reasoning in a comment. A passing run returns as soon as the DOM says so; the budget bounds only a failure's wait. - No skip, retry wrapper or quarantine; no assertion weakened. ## Tests Premise check (not a product race): in `AuthProvider.tsx` the 401 subscription is registered by an effect keyed on `holdsSession`, and `loadSession` clears `sessionLoadInFlight` in the same continuation that sets the user, so by the time `signedIn()` sees "Alice" the subscription is live and the in-flight flag is down. The chain 401 -> `sessionEndEvents` -> `client.getSession()` -> `setUser(null)` has no timer or debounce; the red was only the 1000 ms default budget. - `pnpm exec vitest run packages/auth/src/__tests__/AuthProvider.test.tsx` (from `ui/`, under `os-verify-lock.sh`): 20/20 passed. - Repeat x50 under shard-like concurrency: 10 batches of 5 simultaneous `vitest run --maxWorkers=2` processes of this file under the lock: pass=50 fail=0 (`Tests 20 passed (20)` per run). - `pnpm --filter @object-ui/auth type-check`: exit 0. - Ablation: not run (test-only change; no product behaviour changed). ## Acceptance notes Landing is the expected file; the producer (`AuthProvider.tsx`) needed no change. ## Verification Local gates: CI-owned families (Test UI shards, lint) are read from the PR check-runs; the `--commands` derivation for this diff lists only checker-health self-tests, none a PR verdict. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_012MmDsRT6hvZsQH9dTzn4CA Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0dc84b0 commit 87c0407

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

‎ui/packages/auth/src/__tests__/AuthProvider.test.tsx‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,15 @@ describe('a 401 on an API call re-checks the session (objectstack#32138)', () =>
439439
const AUTH_URL = 'http://localhost/api/v1/auth';
440440
const ALICE = { id: '1', name: 'Alice', email: 'alice@test.com' };
441441

442+
/**
443+
* Budget for each step of the chain (mount read -> identity set; 401 -> re-check
444+
* read -> identity cleared). Each step is a few awaited microtasks plus one React
445+
* commit, tens of ms on an idle box; the 1000 ms `waitFor` default was measured
446+
* too tight when a Test UI shard runs ~5000 tests alongside (one 1022 ms red).
447+
* It bounds only a failure's wait: a passing run returns as soon as the DOM says so.
448+
*/
449+
const CHAIN_MS = 10_000;
450+
442451
/** What `get-session` answers next. */
443452
let getSessionAnswer: () => Promise<Response>;
444453

@@ -462,7 +471,9 @@ describe('a 401 on an API call re-checks the session (objectstack#32138)', () =>
462471
<TestConsumer />
463472
</AuthProvider>,
464473
);
465-
await waitFor(() => expect(screen.getByTestId('user-name').textContent).toBe('Alice'));
474+
await waitFor(() => expect(screen.getByTestId('user-name').textContent).toBe('Alice'), {
475+
timeout: CHAIN_MS,
476+
});
466477
return fetchFn;
467478
}
468479

@@ -489,7 +500,13 @@ describe('a 401 on an API call re-checks the session (objectstack#32138)', () =>
489500
getSessionAnswer = async () => new Response('null', { status: 401 });
490501
await apiAnswers401();
491502

492-
await waitFor(() => expect(screen.getByTestId('authenticated').textContent).toBe('false'));
503+
// Wait on the event first: the re-check read itself reaching the server.
504+
await waitFor(() => expect(vi.mocked(fetchFn).mock.calls.length).toBeGreaterThan(reads), {
505+
timeout: CHAIN_MS,
506+
});
507+
await waitFor(() => expect(screen.getByTestId('authenticated').textContent).toBe('false'), {
508+
timeout: CHAIN_MS,
509+
});
493510
expect(screen.getByTestId('user-name').textContent).toBe('none');
494511
expect(vi.mocked(fetchFn).mock.calls.length).toBeGreaterThan(reads);
495512
});
@@ -500,7 +517,9 @@ describe('a 401 on an API call re-checks the session (objectstack#32138)', () =>
500517

501518
await apiAnswers401();
502519

503-
await waitFor(() => expect(vi.mocked(fetchFn).mock.calls.length).toBeGreaterThan(reads));
520+
await waitFor(() => expect(vi.mocked(fetchFn).mock.calls.length).toBeGreaterThan(reads), {
521+
timeout: CHAIN_MS,
522+
});
504523
expect(screen.getByTestId('authenticated').textContent).toBe('true');
505524
expect(screen.getByTestId('user-name').textContent).toBe('Alice');
506525
});
@@ -514,7 +533,9 @@ describe('a 401 on an API call re-checks the session (objectstack#32138)', () =>
514533
};
515534
await apiAnswers401();
516535

517-
await waitFor(() => expect(vi.mocked(fetchFn).mock.calls.length).toBeGreaterThan(reads));
536+
await waitFor(() => expect(vi.mocked(fetchFn).mock.calls.length).toBeGreaterThan(reads), {
537+
timeout: CHAIN_MS,
538+
});
518539
await act(async () => {
519540
await new Promise((r) => setTimeout(r, 20));
520541
});

0 commit comments

Comments
 (0)