Files
sendou.ink/app/modules/persisted-state/persisted-state-test-utils.ts
2026-09-02 21:02:01 +03:00

37 lines
1008 B
TypeScript

import { expect } from "vitest";
import type {
PersistedDefinition,
PersistedMapDefinition,
} from "./persisted-state";
/** Asserts `decode(encode(x))` deep-equals `x` for every example, and that a missing value decodes to the default. */
export function assertRoundTrips<T>(
definition: PersistedDefinition<T> | PersistedMapDefinition<T>,
examples: NoInfer<T>[],
) {
expect
.soft(definition.decode(null), "missing → default")
.toEqual(definition.default);
for (const value of examples) {
expect
.soft(
definition.decode(definition.encode(value)),
`round trip of ${JSON.stringify(value)}`,
)
.toEqual(value);
}
}
/** Asserts that each given raw stored string decodes to the definition's default. */
export function assertDecodesToDefault<T>(
definition: PersistedDefinition<T> | PersistedMapDefinition<T>,
raws: string[],
) {
for (const raw of raws) {
expect
.soft(definition.decode(raw), `${JSON.stringify(raw)} → default`)
.toEqual(definition.default);
}
}