// Unit test with `@voltro/testing` — no database, no running server, no real // model calls. `makeTestContext` hands the handler the SAME mixin-wrapped // `ctx.store` it gets in production, so tenant auto-scoping + audit auto-fill // behave exactly as at runtime. Run with `voltro test` (vitest). import { describe, it, expect } from 'vitest' import { makeTestContext, mockStore } from '@voltro/testing' import { database } from '../database/schema' // registers notes / actors / tenants import createNote from '../mutations/notes.create.mutation.server' describe('notes.create', () => { it('inserts a note, auto-stamped to the caller tenant', async () => { const ctx = makeTestContext({ subject: { type: 'user', id: 'user_1', tenantId: 'acme' }, store: mockStore({ notes: [] }), }) const row = await createNote({ title: 'Hello', body: 'World' }, ctx) expect(row.title).toBe('Hello') expect(row.tenantId).toBe('acme') // tenant() stamped it from the subject expect(row.id).toMatch(/^note_/) // TypeID auto-injected }) it('isolates tenants — t2 never sees t1 rows (REAL tenant scoping)', async () => { const ctx = makeTestContext({ subject: { type: 'user', id: 'u1', tenantId: 't1' }, store: mockStore({ notes: [] }), }) await createNote({ title: 'secret', body: '' }, ctx) // Re-scope the SAME store to a different tenant — `withTenant` hands the // callback a re-scoped `c`; query THAT (the real WHERE filter hides t1's row). const seenByT2 = await ctx.withTenant('t2', (c) => c.store.query(database.notes.descriptor)) expect(seenByT2).toHaveLength(0) }) })