Skip to content

Commit 6d131b9

Browse files
committed
fix(scripting): address PR review findings
- Fix missing findChain in deployNewChain toWalletClient call (same bug class as the toPublicClient fix already applied) - Throw if assertSchemaCoverage finds zero testable fields (prevents silently passing on misconfigured schemas) - Fix misleading vi.mock hoisting comment - Change generateChainId mock to use registry for consistency - Document that optional fields are excluded from automatic testing and must use overrides
1 parent 06c00f0 commit 6d131b9

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/scripting/examples/deployNewChain.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ export const schema = createRollupDefaultSchema
5858
params: { config, ...params },
5959
account: toAccount(input.privateKey),
6060
parentChainPublicClient,
61-
walletClient: toWalletClient(input.parentChainRpcUrl, input.privateKey),
61+
walletClient: toWalletClient(
62+
input.parentChainRpcUrl,
63+
input.privateKey,
64+
findChain(input.parentChainId),
65+
),
6266
keyset: isAnytrust ? keyset ?? DEFAULT_KEYSET : undefined,
6367
};
6468
});

src/scripting/schemaCoverage.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
// mocks.fn(name, returnValue?) -- async mock, returns Promise.resolve(returnValue)
2525
// mocks.fnSync(name, returnValue?) -- sync mock, returns returnValue (or a valid hex string)
2626
// mocks.trackedObject(name) -- Proxy that records all method calls
27+
//
28+
// Note: optional/nullable fields are excluded from automatic testing because
29+
// they can't be given two distinct non-undefined values without knowing the
30+
// context (e.g. a refine may reject them). Use the overrides parameter to
31+
// test optional fields that matter:
32+
//
33+
// await assertSchemaCoverage(schema, execute, mocks, {
34+
// 'optionalField': (base) => ({ ...base, optionalField: 'some valid value' }),
35+
// });
2736

2837
import { vi } from 'vitest';
2938
import { type ZodType, type z } from 'zod';
@@ -82,8 +91,9 @@ const _mocks = vi.hoisted(() => {
8291
export const mocks = _mocks;
8392

8493
// -- Module mocks ------------------------------------------------------------
85-
// vi.mock is statically analyzed and hoisted in any file that imports this
86-
// module, so these take effect before imports in the consuming test file.
94+
// vi.mock calls are processed by vitest's module transform regardless of
95+
// which file they appear in, so these mocks are active when the consuming
96+
// test file's imports resolve.
8797

8898
vi.mock('./viemTransforms', () => ({
8999
toPublicClient: (rpcUrl: string, chain: unknown) =>
@@ -176,7 +186,7 @@ vi.mock('../createRollup', () => ({
176186
createRollup: _mocks.fn('createRollup', { coreContracts: {} }),
177187
}));
178188
vi.mock('../setValidKeyset', () => ({ setValidKeyset: _mocks.fn('setValidKeyset') }));
179-
vi.mock('../utils/generateChainId', () => ({ generateChainId: () => 999999 }));
189+
vi.mock('../utils/generateChainId', () => ({ generateChainId: _mocks.fnSync('generateChainId', 999999) }));
180190
vi.mock('../upgradeExecutorPrepareAddExecutorTransactionRequest', () => ({
181191
upgradeExecutorPrepareAddExecutorTransactionRequest: _mocks.fn('addExecutor'),
182192
}));
@@ -371,6 +381,16 @@ export async function assertSchemaCoverage<T extends ZodType>(
371381
overrides?: Record<string, (base: z.input<T>) => z.input<T>>,
372382
): Promise<void> {
373383
const leaves = getSchemaLeaves(schema);
384+
const testableLeaves = leaves.filter((l) => {
385+
const t = getDefType(l.schema);
386+
return t !== 'literal' && t !== 'null';
387+
});
388+
if (testableLeaves.length === 0) {
389+
throw new Error(
390+
'assertSchemaCoverage found 0 testable fields. ' +
391+
'The schema may be empty or getSchemaLeaves may not support a type it uses.',
392+
);
393+
}
374394

375395
resetCounter();
376396
const valuesA = new Map<string, unknown>();

0 commit comments

Comments
 (0)