Skip to content

Commit 141056b

Browse files
test(runtime): judge each share-link password pair once in the messages-audience file (#25426)
<!-- claude-projects-attribution: {"github_login":"os-sales"} --> _Requested by **Sales** · [project thread](https://claude.ai/code/project/chan_01D5bWFY3rFzgJcytD5gD4Uu?thread=cmsg_01D5bWFY3rFzgJcytD5gD4UuDWJsBGQpadCH8JR2xZQ5re)_ Part of #23181 (slice 7). The card stays open for later slices. Clause-②: no ## What changes One test file: `packages/runtime/src/domains/share-links-messages-audience.test.ts`. Its `beforeAll` wraps the `ShareLinkService` instance's own password verifier in a memo keyed by the `(password, hash)` pair. This is the same memo `share-links-refusal-order.test.ts` beside it got in PR #24704. The first request that presents a pair is judged by the production verifier (scrypt at the account parameters). Every later request with that pair gets the same verdict back. What stays the same: - every request is still made, on both mounts and both routes, so every answer is still a real request's answer; - every row, case and assertion: 85 tests before, 85 after; - the service is still built with both password members at their defaults, as production builds it; - if `verifyPassword` stops being the member name, `beforeAll` throws instead of quietly going back to paying for every call. ## Measured (local, shared box, `origin/main` `830a63a680` vs this head `6e979387cc`) Each run is `pnpm --filter @objectstack/runtime exec vitest run --maxWorkers=2 src/domains/share-links-messages-audience.test.ts` under `os-verify-lock`. The box is shared, so the ratios are the reading to trust, not the absolute numbers. | | run 1 | run 2 | run 3 | |:--|--:|--:|--:| | per-file duration before (json reporter, the unit CI's module line reports) | 3695 ms | 3816 ms | 3580 ms | | per-file duration after | 891 ms | 928 ms | 864 ms | | vitest `Duration` before (wall incl. ~15 s import) | 19.30 s | 18.93 s | 18.89 s | | vitest `Duration` after | 16.80 s | 17.98 s | 17.79 s | - scrypt derivations, counted with a measurement-only preload that wraps `node:crypto.scrypt` (not part of this PR): **32 before** (30 verifier calls + 2 password mints), **6 after** (4 distinct pairs + 2 mints), the same in all three after-runs. Before, the derivations added up to 3817 ms, which is about the whole per-file duration. - Per-file ratio after/before: about 0.24. Applied to the card's CI reading (13.51 s, run `39298888283`), that projects to about 3.3 s, under the 10 s line. The next refresh's list is the real check. - The rest of the wall is module import (~15 s), and it is the same before and after. The per-file reading the card grades does not count import time. ## Tests and gates (at `6e979387cc`) - `pnpm --filter @objectstack/runtime test`: Test Files 438 passed (438), Tests 7022 passed / 19 skipped; lock VERDICT command-exit 0. - `pnpm --filter @objectstack/runtime typecheck`: lock VERDICT command-exit 0, test layer included (`check:test-typecheck: OK`). - `pnpm lint`: exit 0. - `dispatch-gates --commands` derived 54 commands. 53 exited 0. `pnpm check:dual-build-cjs-loads` exited 3, PREREQUISITE NOT MET (it needs every package's `dist/`, and this worktree built only the runtime closure), so it is NOT MEASURED here. CI's Lint & Repo Gates runs it. `--ran`: 54 accounted, 53 run, 1 NOT-MEASURED, 0 UNRUN. ## Changeset `skip-changeset`: the only change is a `*.test.ts` file. `@objectstack/runtime` ships `files: [dist, README.md, CHANGELOG.md]`, and Build Core checks that no compiled test file ends up in any dist. ## Acceptance notes - The dispatch suggested importing PR #24704's memo instead of copying it. That memo is written inline in the sibling file's `beforeAll` and is not exported, so it can only be imported after being moved into a shared testkit. Doing that touches the sibling file and adds a new file, which is outside this slice's one-file surface. So the same 13 lines are copied here, and the comment names the sibling. Moving both copies into a `share-link-verifier-memo.testkit.ts` would be a small follow-up for whoever owns this lane. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01CxBKycaiUpA1yGx1XyoGqX --- _Generated by [Claude Code](https://claude.ai/code/session_01CxBKycaiUpA1yGx1XyoGqX)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 4374f3c commit 141056b

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎packages/runtime/src/domains/share-links-messages-audience.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,36 @@ beforeAll(async () => {
211211
await engine.insert('audience_doc', { id: DOC, title: 'Shared doc' }, sys);
212212

213213
const svc = new ShareLinkService({ engine: engine as any });
214+
// One scrypt verification per distinct (password, hash) pair, not one per
215+
// request: the refusal-order file beside this one does the same, for the
216+
// same reason. The production verifier derives a key at the account
217+
// parameters (scrypt N=16384, r=16) on every call, and every request below
218+
// that presents a password to a password link its audience let through
219+
// calls it: 30 calls, counted at `830a63a680`, plus the 2 password mints,
220+
// and those derivations were nearly all of the file's test time (it read
221+
// 13.51 s in CI, run 39298888283). The calls hold only 4 distinct pairs
222+
// (2 password links x a wrong and the correct password), and a verdict is
223+
// a pure function of its pair, so each pair is judged once by the real
224+
// verifier and its verdict replayed. Every request is still made, so every
225+
// answer is still a real request's, and the service is still built with
226+
// both password members at their defaults, as production builds it. The
227+
// memo wraps the instance's own verifier, which is not exported. If that
228+
// member moves, this throws rather than leave the file paying for every
229+
// call again.
230+
type Verify = (plain: string, hash: string) => Promise<boolean>;
231+
const productionVerify: unknown = (svc as any).verifyPassword;
232+
if (typeof productionVerify !== 'function') {
233+
throw new Error('ShareLinkService no longer keeps its password verifier as `verifyPassword`; re-point the memo below');
234+
}
235+
const verdicts = new Map<string, Map<string, Promise<boolean>>>();
236+
const memoVerify: Verify = (plain, hash) => {
237+
let byPlain = verdicts.get(hash);
238+
if (!byPlain) verdicts.set(hash, (byPlain = new Map()));
239+
let verdict = byPlain.get(plain);
240+
if (!verdict) byPlain.set(plain, (verdict = (productionVerify as Verify)(plain, hash)));
241+
return verdict;
242+
};
243+
(svc as any).verifyPassword = memoVerify;
214244
const creator = { isSystem: true, userId: 'usr_creator_23371' } as any;
215245
const mint = async (
216246
object: 'ai_conversations' | 'audience_doc',

0 commit comments

Comments
 (0)