// Unit test with `@voltro/testing` — no database, no running server, no webhook // delivery. The `orders.fulfill` executor emits an outgoing `order.completed` // event via `useWebhooks(ctx).emit(...)` — that fans out through the webhooks // plugin's durable delivery workflow (real HTTP POSTs, retries, signing), which // is dispatch-time infrastructure the unit harness does NOT wire, so we do not // fake it. Instead we PIN the descriptor's wire contract and prove the tenant() // scoping on the `orders` table. Run with `voltro test` (vitest). import { describe, it, expect } from 'vitest' import { Schema } from 'effect' import { makeTestContext, mockStore } from '@voltro/testing' import { database } from '../database/schema' // registers orders + webhook tables import { fulfillOrder } from '../mutations/orders.fulfill.mutation' describe('orders.fulfill descriptor', () => { it('decodes a valid input and rejects a blank sku', () => { const decode = Schema.decodeUnknownSync(fulfillOrder.input) expect(decode({ sku: 'SKU-1', totalCents: 4200 })).toEqual({ sku: 'SKU-1', totalCents: 4200 }) // `sku: Schema.NonEmptyString` — an empty string fails the wire decode. expect(() => decode({ sku: '', totalCents: 4200 })).toThrow() }) it('encodes the output shape the client expects', () => { const encode = Schema.encodeUnknownSync(fulfillOrder.output) const wire = encode({ id: 'order_1', sku: 'SKU-1', totalCents: 4200, status: 'fulfilled', tenantId: 'acme', }) expect(wire).toEqual({ id: 'order_1', sku: 'SKU-1', totalCents: 4200, status: 'fulfilled', tenantId: 'acme', }) }) }) describe('orders tenant isolation', () => { it('t2 never sees t1 orders (REAL tenant scoping)', async () => { const ctx = makeTestContext({ subject: { type: 'user', id: 'u1', tenantId: 't1' }, store: mockStore({ orders: [] }), }) // Raw insert through the mixin store — tenant() auto-stamps tenantId=t1. // `status` set explicitly (a column DEFAULT only fills on a SQL store). const row = await ctx.store.insert('orders', { sku: 'SKU-1', totalCents: 4200, status: 'fulfilled' }) expect(row['tenantId']).toBe('t1') expect(row['id'] as string).toMatch(/^order_/) const seenByT2 = await ctx.withTenant('t2', (c) => c.store.query(database.orders.descriptor)) expect(seenByT2).toHaveLength(0) const seenByT1 = await ctx.store.query(database.orders.descriptor) expect(seenByT1).toHaveLength(1) }) })