TestQuery unstable
A test harness for effect/ui/Query with controllable host
signals, fetch records, read-only entry views, and problem collection.
Uses the public cache Layer.
Stale times, idle times and intervals use the Clock, so
TestClock.adjust drives them.
Example Asserting on what the cache fetched
import { Effect, Layer, Schema } from "effect"import { Query } from "effect/ui"import { TestQuery } from "effect/ui/testing"
class GetUser extends Query.Service<GetUser>()("users/GetUser", { args: { id: Schema.String }, success: Schema.String, error: Schema.Never, staleTime: "30 seconds"}) { static readonly layer = this.toLayer(({ id }) => Effect.succeed(`user-${id}`))}
const program = Effect.gen(function*() { yield* GetUser.get({ id: "1" }) yield* GetUser.get({ id: "1" }) return yield* TestQuery.fetches}).pipe(Effect.provide(Layer.merge(GetUser.layer, TestQuery.layer())))
await Effect.runPromise(program) // => [{ query: "users/GetUser", logKey: `{"id":"1"}`, trigger: "get", restart: false, stored: true }]Errors
ProblemsError
unstableThe defect a TestQuery Scope closes with when the cache reported problems
the test didn't take with expectProblem.
Signature
declare class ProblemsError extends YieldableError<this> & { readonly _tag: "ProblemsError";} & Readonly<{ readonly problems: readonly Array<Any>;}> { constructor(args: { readonly problems: readonly Array<Any>; }); message: string;}Layers
Layer that provides Query.Cache for a test, together with TestQuery.
Details
The cache is the one Query.layer builds, with the same options. Its
Query.Visibility and Query.Connectivity are settable and start true,
and its problems go to the collector instead of the console.
Gotchas
A reported problem fails the test: the Layer's Scope closes with a
ProblemsError defect unless expectProblem took every problem first.
Signature
declare function layer(options?: { readonly idleTimeToLive?: Input;}): Layer<Cache | TestQuery>Models
A query-only view of one cache entry.
Details
stored is the entry's server state, Initial during a first fetch.
view is that state with every patch applied, and patched whether it
includes one. stale is whether the next trigger would refetch it,
watchers how many watch it. fetch is held while a refetch waits for
a live patch's run to exit, and trigger is what started the fetch in
flight, or what the held one will start with. patches counts the live
patches and those held after their run succeeded. more, on a list
read only, is whether it holds more pages to load.
Signature
interface Entry<A, E> { readonly fetch: "none" | "running" | "held"; readonly more?: More<E>; readonly patched: boolean; readonly patches: { readonly held: number; readonly live: number; }; readonly stale: boolean; readonly stored: AsyncResult<A, E>; readonly trigger: Trigger | undefined; readonly view: AsyncResult<A, E>; readonly watchers: number;}A fetch the cache started: the query's name, the entry's log key, the trigger, whether it replaced an interrupted fetch, and whether its result is stored.
Details
The log key is the entry's key with every Schema.Redacted argument
redacted. A caller that joins a fetch in flight records nothing.
stored is false for a fetcher call made for get callers alone: a
run reading back an entry it invalidates, or a get on a stale entry
under a live patch. Its trigger is get.
An infinite query records each page it requests: page is its index, from 0,
and cursor the encoded cursor, absent for the first page.
Signature
interface Fetch { readonly cursor?: unknown; readonly logKey: string; readonly page?: number; readonly query: string; readonly restart: boolean; readonly stored: boolean; readonly trigger: Trigger;}Services
The TestQuery service.
Signature
declare const TestQuery: Service<TestQuery, TestQuery>The TestQuery service: the controls and the records of one test cache.
Signature
interface TestQuery { readonly entry: <R extends Any>(read: R, args: Args<R>) => Entry<Success<R>, Error<R>> | undefined; readonly expectProblem: <Tag extends string>(tag: Tag) => readonly Array<[Extract<DuplicateName, { readonly _tag: Tag; }> | Extract<IntervalThrew, { readonly _tag: Tag; }> | Extract<IntervalInvalid, { readonly _tag: Tag; }> | Extract<FetchRestarted, { readonly _tag: Tag; }> | Extract<PageRepeated, { readonly _tag: Tag; }> | Extract<DuplicateKeyInPage, { readonly _tag: Tag; }> | Extract<PatchInvalid, { readonly _tag: Tag; }> | Extract<PatchMutated, { readonly _tag: Tag; }> | Extract<UncoveredWrite, { readonly _tag: Tag; }> | Extract<InvalidateIgnored, { readonly _tag: Tag; }> | Extract<EncodeFailed, { readonly _tag: Tag; }> | Extract<HydrationMalformed, { readonly _tag: Tag; }> | Extract<HydrationDecodeFailed, { readonly _tag: Tag; }> | Extract<ReactivityMissing, { readonly _tag: Tag; }>] extends [never] ? Any : Extract<DuplicateName, { readonly _tag: Tag; }> | Extract<IntervalThrew, { readonly _tag: Tag; }> | Extract<IntervalInvalid, { readonly _tag: Tag; }> | Extract<FetchRestarted, { readonly _tag: Tag; }> | Extract<PageRepeated, { readonly _tag: Tag; }> | Extract<DuplicateKeyInPage, { readonly _tag: Tag; }> | Extract<PatchInvalid, { readonly _tag: Tag; }> | Extract<PatchMutated, { readonly _tag: Tag; }> | Extract<UncoveredWrite, { readonly _tag: Tag; }> | Extract<InvalidateIgnored, { readonly _tag: Tag; }> | Extract<EncodeFailed, { readonly _tag: Tag; }> | Extract<HydrationMalformed, { readonly _tag: Tag; }> | Extract<HydrationDecodeFailed, { readonly _tag: Tag; }> | Extract<ReactivityMissing, { readonly _tag: Tag; }>>; readonly fetches: () => readonly Array<Fetch>; readonly problems: () => readonly Array<Any>; readonly setOnline: (online: boolean) => void; readonly setVisible: (visible: boolean) => void;}Testing
Returns a query-only view of one entry, or undefined when the cache has no
entry for the query and arguments. It doesn't create the entry or keep it
alive.
Signature
declare function entry<R extends Any>(read: R, args: Args<R>): Effect<Entry<Success<R>, Error<R>> | undefined, never, TestQuery>expectProblem
unstableRemoves and returns every reported problem with the given tag, preventing those problems from failing the test.
Gotchas
Dies when the cache reported no problem with that tag.
Signature
declare function expectProblem<Tag extends string>(tag: Tag): Effect<readonly Array<[Extract<DuplicateName, { readonly _tag: Tag;}> | Extract<IntervalThrew, { readonly _tag: Tag;}> | Extract<IntervalInvalid, { readonly _tag: Tag;}> | Extract<FetchRestarted, { readonly _tag: Tag;}> | Extract<PageRepeated, { readonly _tag: Tag;}> | Extract<DuplicateKeyInPage, { readonly _tag: Tag;}> | Extract<PatchInvalid, { readonly _tag: Tag;}> | Extract<PatchMutated, { readonly _tag: Tag;}> | Extract<UncoveredWrite, { readonly _tag: Tag;}> | Extract<InvalidateIgnored, { readonly _tag: Tag;}> | Extract<EncodeFailed, { readonly _tag: Tag;}> | Extract<HydrationMalformed, { readonly _tag: Tag;}> | Extract<HydrationDecodeFailed, { readonly _tag: Tag;}> | Extract<ReactivityMissing, { readonly _tag: Tag;}>] extends [never] ? Any : Extract<DuplicateName, { readonly _tag: Tag;}> | Extract<IntervalThrew, { readonly _tag: Tag;}> | Extract<IntervalInvalid, { readonly _tag: Tag;}> | Extract<FetchRestarted, { readonly _tag: Tag;}> | Extract<PageRepeated, { readonly _tag: Tag;}> | Extract<DuplicateKeyInPage, { readonly _tag: Tag;}> | Extract<PatchInvalid, { readonly _tag: Tag;}> | Extract<PatchMutated, { readonly _tag: Tag;}> | Extract<UncoveredWrite, { readonly _tag: Tag;}> | Extract<InvalidateIgnored, { readonly _tag: Tag;}> | Extract<EncodeFailed, { readonly _tag: Tag;}> | Extract<HydrationMalformed, { readonly _tag: Tag;}> | Extract<HydrationDecodeFailed, { readonly _tag: Tag;}> | Extract<ReactivityMissing, { readonly _tag: Tag;}>>, never, TestQuery>Returns every fetch the test cache started so far, in order.
Signature
declare const fetches: Effect.Effect<ReadonlyArray<Fetch>, never, TestQuery>Returns reported problems not yet consumed by expectProblem.
Signature
declare const problems: Effect.Effect<ReadonlyArray<Query.Problem.Any>, never, TestQuery>Sets Query.Connectivity for the test cache.
Details
Going from false to true, with the cache visible, refetches every watched entry that is stale or whose interval elapsed while it was offline.
Signature
declare function setOnline(online: boolean): Effect<void, never, TestQuery>setVisible
unstableSets Query.Visibility for the test cache.
Details
Going from false to true, with the cache online, refetches every watched entry that is stale or whose interval elapsed while it was hidden.
Signature
declare function setVisible(visible: boolean): Effect<void, never, TestQuery>