kernel(app, { now }) from @bijectionhq/bijection/test installs your app on
the real kernel, in memory. The kernel admits each call (exposure, allow,
clearance and act.enter guards), runs the handler, judges its writes against
the guards at commit, and commits or refuses the transaction, exactly as a
deployment does.
Install Vitest in a project that already uses the
Bijection SDK:
vitest.config.ts
--harmony-temporal. Add your own options with mergeConfig:
vitest.config.ts
The app module
The kernel runs your handlers the way a deployment does: in a fresh guest context per call, with the same deterministic globals (the frame’s clock forDate and Temporal.Now, seeded Math.random and crypto, the guest
console, no timers or fetch). It loads them from the module that declares
the app, as a deployment loads app. So pass kernel an app
that is the default export of its own module, declared at that module’s top
level:
fixtures/notes.ts
notes.test.ts
app(…) call written at a module’s top level, outside every function and
class body, with app imported from @bijectionhq/bijection, is rewritten
to pass its module’s import.meta.url to app(). Nothing is read from stack
traces, so the module is the same under every runner configuration and
source map.
kernel refuses an app it has no module for, with a TypeError that names
the fix:
- An app built inside a test, or inside a function, has no module. Move it to a fixture module’s default export, as above.
- Without the preset, Vitest does not record the module. Add
vitestPreset()tovitest.config.ts.
node:fs,
node:crypto and the like). It imports no vitest either: keep test code
in the test file.
Outside Vitest
kernel runs under Vitest only. The app’s module is a fact the preset’s
plugin records at transform time, and only a transform pipeline knows it.
Under node --test, Bun or Deno no plugin runs, and kernel(app) refuses
with a TypeError that says to run the test under Vitest with the preset.
Tests that call no kernel, such as pure functions an app imports, run under
any runner.
A first test
The kernel’s tables and functions come from the app tree. This test uses the notes app of the default starter.tests/notes.test.ts
npm test, the starter’s vitest run. Make a new kernel
in each test: each one is an independent installation with its own clock.
Kernel options
Callers: k.as(principal)
k.as(principal) is the client a browser gets, connected to this kernel. It
has query, mutate, watch and trial, the frame methods .under,
.in and .at. Pass references from api<typeof App>() or api(App)
to its verbs. A client call answers a Result.
Signing in: k.signIn(issuer, claims)
k.signIn(issuer, claims) signs a person in through an issuer the app
declares, with the kernel playing the identity provider. It answers the same
client as k.as, for the person the identity binding names: the same
subject signs in as the same id. claims are the provider’s: subject,
and optionally email, emailVerified, phone, phoneVerified and
authenticatedAt. An email becomes ctx.frame.as.email only when the
provider verified it and the issuer trusts verified email
(trustVerifiedEmail: true), exactly as a deployment decides; the kernel
then also matches it against an invitation’s addressee. Pass a tenant
trust’s grant point (sys.grants(id) for the id returned by the Sessions issue call) as issuer to
sign in through that tenant: its
email never enters as.email, and redeems only that tenant’s invitations.
clerk.
under: tenant.of("acme"), and the caller then
acts in that member with bob.under(tenant.of("acme")).
role.anyone and role.signedIn are class grants the program issues at
install. They enter functions and clear no label, so entry through them
clears only public data. If such an entry requires more clearance, it
refuses Disallowed with reason NotCleared.
Give such an entry a role that clears the label.
Refusals
A client call answers the refusal as a value. Hereadd refuses an empty
note with EmptyNote, as the quickstart
adds it, and the app also declares the guard guards.noSpam: forbid(act.enter(notes.add), { message: "Spam is not a note", where: (a) => a.args.text.equals("spam") }):
guard, the guard’s tree path.
The kernel’s own verbs
Usek.as(principal) to call an app function under its real admission
rules. Use k.seed(table, facts) to prepare app data. The shell stages
the batch in a branch as the founder and lands it with an ordinary land,
which orders every fact together. Pass { by, under } to choose another
performer or a labelled commit. k.seed(sys.files, files, { by, under })
runs the ordinary File store act and defaults to the installation’s founder.
Grant and member seeds prepare starting state in the memory shell. Every
grant in a batch uses the same under label. Choose a label visible to the
app call that uses the grant. The owning program’s live grant bounds each
grant’s rights and lifetime. Member seeds create public registry
rows, including keyed lookup and family limits. Seed any member role
separately. The shell commits all the facts in a batch together and records
fixture provenance. Authorization tests call the declared app function
through k.as(principal) or k.signIn.
The memory shell’s reads inspect committed facts outside function admission.
A refused seed throws Refused with the failure as result.
Tasks: the test performs
Read task facts withk.list(sys.tasks.where({ state: "pending" }).first(n)).
Use k.answer to simulate an external receiver’s reply. k.advance runs function tasks and
due continuations. For a target answered by a mutation, call that mutation
with k.as(principal).
ctx.withdraw(task). Its
continuation receives Withdrawn. A task still pending at its within
deadline settles Expired.
The clock: k.advance
k.advance(duration) moves the clock and runs everything then due: expiries,
due tasks, function tasks, continuations, every and cron sources and
change reactions, until nothing is due. k.advance({ to: instant }) moves it
to an instant. It answers how many pieces of work ran. k.now is the clock.
k.refusals lists, in order, the work that ran and refused: a continuation, a
task’s run, a cron or a change reaction, each { on, refusal }.
Restore an installation
await k.restore() archives the installation at head and restores it
through the kernel’s restore path. Later calls use the restored store
under the same test handle. Kernels explicitly given the same
installation name share custody and keep separate stores.
Trial deployment
k.deploy(next, { trial: true }) runs deploy’s check against the facts
this kernel holds and discards the candidate. The check uses the candidate’s
declared column changes
and reports the table changes or a refusal such as DeclarationBreaksFacts.
{ table, change: { kind, field? }, verdict }. A verdict is
safe, or breaksFactsUnless with the condition that makes it safe
(unless.facts: tableEmpty, or fieldEmpty with its field). next only
supplies its declarations; its handlers never run, and the kernel keeps
running the app it was created with.
Routes: k.route
k.route(reaction, { method, body, file, headers, query, as }) delivers a
request through the declared route reaction’s verifier. The route’s allow admits as (signed out by default), the
reaction holds its roles, and its value or refusal answers as a Result.
file: { bytes, contentType } in place of
body. The kernel stores it at ingress, as the host does, and the reaction
receives the File point as request.file:
Trials and watches
client.trial(fn, args) runs the call in a discarded world and answers
{ result, writes, tasks, validUntil }. Nothing commits.
client.watch(fn, args) is an async iterable of Results that stays current
across commits. Stop it with watch.stop().
Mounted apps
Test a mountable app the way an installer uses it: mount it at a path of a root app, and call its functions at their paths. The root is a test app like any other, so it is the default export of its own module:fixtures/installer.ts
comments.test.ts