Skip to content

Commit ca015a3

Browse files
author
Sævar Berg
committed
test(sql-orm-client): preserve literal capability types in withCapabilities
Replace the `as unknown as TestContract` cast with a generic over `TCaps extends Record<string, Record<string, boolean>>`, returning `Omit<TestContract, 'capabilities'> & { readonly capabilities: TCaps }`. Capability-dependent type checks now see the caller's literal capability shape instead of the narrow `TestContract` slot. Addresses @aqrln's review note: "I'd make this helper produce an accurate contract types from day one if it's not too complex." Widens `contextForContract` and `createCollectionFor` in `collection-fixtures.ts` to accept `Contract<SqlStorage>` so the more precise return type flows through; the existing internal cast inside `contextForContract` already absorbs the structural gap. Drops the explicit `: TestContract` return annotations on the two thin `with…Capabilities` wrappers in `collection-dispatch.test.ts` so the inferred result keeps its literal capability shape. All 481 sql-orm-client tests pass.
1 parent 1446a0d commit ca015a3

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

packages/3-extensions/sql-orm-client/test/collection-dispatch.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { createCollectionFor } from './collection-fixtures';
88
import type { MockRuntime, TestContract } from './helpers';
99
import { createMockRuntime, getTestContract, withCapabilities } from './helpers';
1010

11-
function withSingleQueryCapabilities(contract: TestContract): TestContract {
11+
function withSingleQueryCapabilities(contract: TestContract) {
1212
return withCapabilities(contract, {
1313
...contract.capabilities,
1414
[contract.targetFamily]: {
@@ -30,7 +30,7 @@ function withSingleQueryCapabilities(contract: TestContract): TestContract {
3030
* contract" — the regression scenario the principled namespaced lookup
3131
* was introduced to handle.
3232
*/
33-
function withEmittedSqlCapabilities(contract: TestContract): TestContract {
33+
function withEmittedSqlCapabilities(contract: TestContract) {
3434
return withCapabilities(contract, {
3535
sql: { jsonAgg: true, returning: true },
3636
postgres: { jsonAgg: true, lateral: true, returning: true },

packages/3-extensions/sql-orm-client/test/collection-fixtures.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Contract } from '@prisma-next/contract/types';
2+
import type { SqlStorage } from '@prisma-next/sql-contract/types';
13
import type { ExecutionContext } from '@prisma-next/sql-relational-core/query-lane-context';
24
import { Collection } from '../src/collection';
35
import type { MockRuntime, TestContract } from './helpers';
@@ -7,15 +9,15 @@ export type TestModelName = Extract<keyof TestContract['models'], string>;
79

810
export const baseContract = getTestContract();
911

10-
function contextForContract(contract: TestContract): ExecutionContext<TestContract> {
12+
function contextForContract(contract: Contract<SqlStorage>): ExecutionContext<TestContract> {
1113
const base = getTestContext();
1214
if (contract === baseContract) return base;
1315
return { ...base, contract } as ExecutionContext<TestContract>;
1416
}
1517

1618
export function createCollectionFor<ModelName extends TestModelName>(
1719
modelName: ModelName,
18-
contract: TestContract = baseContract,
20+
contract: Contract<SqlStorage> = baseContract,
1921
): {
2022
collection: Collection<TestContract, ModelName>;
2123
runtime: MockRuntime;

packages/3-extensions/sql-orm-client/test/helpers.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ export function getTestContract(): TestContract {
2727
/**
2828
* Override the capabilities of a {@link TestContract} for a test scenario.
2929
*
30-
* The narrow `TestContract` type fixes capabilities to the exact shape
31-
* found in `fixtures/generated/contract.json` (e.g. the `postgres`
32-
* namespace's specific readonly fields). Tests need to construct
33-
* contracts with arbitrary capability shapes — empty, only-jsonAgg,
34-
* cross-namespace, etc. — that don't fit that narrow type.
30+
* The narrow `TestContract` type fixes `capabilities` to the literal shape
31+
* generated for `fixtures/generated/contract.json`. Tests need contracts
32+
* with arbitrary capability shapes — empty, only-jsonAgg, cross-namespace,
33+
* etc. — and want the override's literal types preserved so capability-
34+
* dependent type checks remain meaningful.
3535
*
36-
* This helper centralizes the structural cast so call sites stay clean.
36+
* The result widens `TestContract`'s `capabilities` slot to the caller's
37+
* `TCaps`, which the framework `Contract` interface already permits
38+
* (`capabilities: Record<string, Record<string, boolean>>`).
3739
*/
38-
export function withCapabilities(
40+
export function withCapabilities<TCaps extends Record<string, Record<string, boolean>>>(
3941
contract: TestContract,
40-
capabilities: Record<string, Record<string, boolean>>,
41-
): TestContract {
42-
return { ...contract, capabilities } as unknown as TestContract;
42+
capabilities: TCaps,
43+
): Omit<TestContract, 'capabilities'> & { readonly capabilities: TCaps } {
44+
return { ...contract, capabilities };
4345
}
4446

4547
const testContext: ExecutionContext<TestContract> = createExecutionContext({

0 commit comments

Comments
 (0)