Open History to see the last commit
1import { describe, expectTypeOf, it } from 'vitest'
2import { z } from 'zod'
3import { createCollection } from '@tanstack/db'
4import { electricCollectionOptions } from '../src/electric'
5import type {
6 ChangeMessageOrDeleteKeyMessage,
7 WithVirtualProps,
8} from '@tanstack/db'
9
10const rowSchema = z.object({
11 id: z.string().brand<`ElectricRowId`>(),
12 createdAt: z
13 .union([z.string(), z.date()])
14 .transform((value) =>
15 typeof value === `string` ? new Date(value) : value,
16 ),
17 score: z
18 .union([z.string(), z.number()])
19 .transform((value) => (typeof value === `string` ? Number(value) : value)),
20 label: z.string().default(`untitled`),
21 note: z.string().nullish(),
22})
23
24type RowInput = z.input<typeof rowSchema>
25type RowOutput = z.output<typeof rowSchema>
26type RowId = RowOutput[`id`]
27type ItemOf<T> = T extends Array<infer U> ? U : T
28
29const rawInput = {
30 id: `row-1`,
31 createdAt: `2026-09-18T12:00:00.000Z`,
32 score: `42`,
33 note: null,
34} satisfies RowInput
35
36const synchronizedOutput = {
37 id: `row-1` as RowId,
38 createdAt: new Date(`2026-09-18T12:00:00.000Z`),
39 score: 42,
40 label: `untitled`,
41 note: null,
42} satisfies RowOutput
43
44/**
45 * Adapter mapping for the shared schema input/output law:
46 * `electricCollectionOptions` carries Electric shape rows into sync change
47 * messages as schema output. `getKey`, `compare`, Collection rows, and handler
48 * mutations observe that output. Public insert and update entry points accept
49 * schema input. Electric deliberately keeps its public Collection key domain
50 * at `string | number`; it does not infer the schema's branded ID type.
51 *
52 * The assertions observe those production type paths after overload
53 * resolution. Hostile controls reject an untransformed shape row at the sync
54 * boundary and an invalid value at the mutation boundary. This partial oracle
55 * does not execute Shape parsing, network I/O, or mutation-handler timing.
56 */
57describe(`Electric schema transform conformance`, () => {
58 it(`keeps the shape and mutation sides distinct`, () => {
59 const options = electricCollectionOptions({
60 shapeOptions: { url: `https://example.com/v1/shape` },
61 schema: rowSchema,
62 getKey: (row) => {
63 expectTypeOf(row).toEqualTypeOf<RowOutput>()
64 expectTypeOf(row.id).toEqualTypeOf<RowId>()
65 expectTypeOf(row.createdAt).toEqualTypeOf<Date>()
66 expectTypeOf(row.score).toEqualTypeOf<number>()
67 expectTypeOf(row.label).toEqualTypeOf<string>()
68 expectTypeOf(row.note).toEqualTypeOf<string | null | undefined>()
69 return row.id
70 },
71 compare: (left, right) => left.score - right.score,
72 onInsert: ({ transaction }) => {
73 expectTypeOf(
74 transaction.mutations[0].modified,
75 ).toEqualTypeOf<RowOutput>()
76 return Promise.resolve()
77 },
78 })
79 const collection = createCollection(options)
80
81 expectTypeOf(options.getKey).parameters.toEqualTypeOf<[RowOutput]>()
82 expectTypeOf(options.getKey).returns.toEqualTypeOf<string | number>()
83 expectTypeOf(collection.toArray).toEqualTypeOf<
84 Array<WithVirtualProps<RowOutput, string | number>>
85 >()
86
87 type Insert = ItemOf<Parameters<typeof collection.insert>[0]>
88 expectTypeOf<Insert>().toEqualTypeOf<RowInput>()
89 collection.update(`row-1`, (draft) => {
90 expectTypeOf(draft).toEqualTypeOf<RowInput>()
91 })
92
93 type SyncParams = Parameters<(typeof options)[`sync`][`sync`]>[0]
94 type SyncMessage = Parameters<SyncParams[`write`]>[0]
95 expectTypeOf<SyncMessage>().toEqualTypeOf<
96 ChangeMessageOrDeleteKeyMessage<RowOutput, string | number>
97 >()
98
99 const assertShapeBoundary = (write: SyncParams[`write`]) => {
100 const wrongDate = {
101 ...synchronizedOutput,
102 createdAt: rawInput.createdAt,
103 }
104
105 write({ type: `insert`, value: synchronizedOutput })
106 // @ts-expect-error one untransformed field cannot cross the sync boundary
107 write({ type: `insert`, value: wrongDate })
108 // @ts-expect-error an untransformed shape row is not collection output
109 write({ type: `insert`, value: rawInput })
110 }
111 expectTypeOf(assertShapeBoundary).toBeFunction()
112
113 const assertMutationInput = () => {
114 collection.insert(rawInput)
115 // @ts-expect-error mutation input does not accept unrelated numeric shapes
116 collection.insert({ ...rawInput, score: false })
117 }
118 expectTypeOf(assertMutationInput).toBeFunction()
119 })
120})