// Unit test for `notes.create` — driven through `@voltro/testing`. // // This executor is Effect-native: it `yield*`s the flag guard // `requireFlag(ctx, 'newEditor')` (fails typed `FlagDisabled` when the flag is // off for the caller) and then `yield* EffectStore` to insert. The flag registry // is populated by flagsPlugin at BOOT from app.config.ts — a step the unit // harness deliberately doesn't run, so under test every flag reads as off and // `requireFlag` fails `FlagDisabled`. We assert that REAL guard behavior (no // faking the plugin) + pin the descriptor's contract. `makeTestContext` exposes // the mixin-wrapped store as `ctx.store`, provided to the Effect as the // `EffectStore` layer via `makeEffectStoreLayer`. Run with `voltro test`. import { describe, it, expect } from 'vitest' import { Cause, Effect, Exit, Option, Schema } from 'effect' import { makeTestContext, mockStore } from '@voltro/testing' import { makeEffectStoreLayer } from '@voltro/runtime' import { database } from '../database/schema' // registers actors / tenants / notes import { createNote } from '../mutations/notes.create.mutation' import createNoteHandler from '../mutations/notes.create.mutation.server' import { FlagDisabled } from '@voltro/plugin-flags/errors' // Run the Effect-native executor with the test store provided as EffectStore, // returning the raw Exit so a typed failure can be inspected WITHOUT the // `FiberFailure` wrapper `Effect.runPromise` throws (which would defeat a plain // `instanceof FlagDisabled` assertion). Unwrap the Cause to get the real error. const runExit = ( input: { tenantId: string; title: string; body: string }, ctx: ReturnType, ) => Effect.runPromiseExit(createNoteHandler(input, ctx).pipe(Effect.provide(makeEffectStoreLayer(ctx.store)))) describe('notes.create', () => { it('fails typed FlagDisabled when the newEditor flag is off — nothing written', async () => { // No plugin boot under unit test ⇒ the flag registry is empty ⇒ the flag is // off ⇒ the in-handler requireFlag guard short-circuits before the insert. const ctx = makeTestContext({ subject: { type: 'user', id: 'u1', tenantId: 'acme' }, store: mockStore({ notes: [] }), }) const exit = await runExit({ tenantId: 'acme', title: 'Hello', body: 'World' }, ctx) expect(Exit.isFailure(exit)).toBe(true) const failure = Exit.isFailure(exit) ? Cause.failureOption(exit.cause) : Option.none() expect(Option.getOrThrow(failure)).toBeInstanceOf(FlagDisabled) const rows = await ctx.store.query(database.notes.descriptor) expect(rows).toHaveLength(0) }) it('pins the descriptor: name, input decode + the declared FlagDisabled error', () => { expect(createNote.name).toBe('notes.create') const decoded = Schema.decodeUnknownSync(createNote.input)({ tenantId: 'acme', title: 'ok', body: 'b', }) expect(decoded).toEqual({ tenantId: 'acme', title: 'ok', body: 'b' }) // Empty title is rejected at the input Schema (NonEmptyString). expect(() => Schema.decodeUnknownSync(createNote.input)({ tenantId: 'acme', title: '', body: '' }), ).toThrow() const disabled = new FlagDisabled({ flag: 'newEditor' }) expect(Schema.is(FlagDisabled)(disabled)).toBe(true) }) })