Skip to content

Open source personal finance application

test: add Convex backend integration tests for wallets, categories, and trns

Ilya Komichev15fb9b7b016f
diff --git a/convex/categories.test.ts b/convex/categories.test.ts
new file mode 100644
index 00000000..02da8288
--- /dev/null
+++ b/convex/categories.test.ts
@@ -0,0 +1,238 @@
+import { convexTest } from 'convex-test'
+import { describe, expect, it } from 'vitest'
+
+import { api } from './_generated/api'
+import schema from './schema'
+import { modules, setMockUser } from './test-utils/setup'
+
+const validCategory = {
+ color: '#ff0000',
+ icon: 'food',
+ name: 'Food',
+ showInLastUsed: true,
+ showInQuickSelector: true,
+}
+
+const validWallet = {
+ color: '#000', currency: 'USD', name: 'Cash', order: 0, type: 'cash' as const,
+}
+
+describe('convex/categories', () => {
+ describe('create', () => {
+ it('creates a root category', async () => {
+ const t = convexTest(schema, modules)
+ const id = await t.mutation(api.categories.create, validCategory)
+
+ const categories = await t.query(api.categories.list)
+ expect(categories).toHaveLength(1)
+ expect(categories![0]).toMatchObject({
+ ...validCategory,
+ _id: id,
+ parentId: 0,
+ userId: 'test-user-id',
+ })
+ })
+
+ it('creates a child category', async () => {
+ const t = convexTest(schema, modules)
+ const parentId = await t.mutation(api.categories.create, validCategory)
+ const childId = await t.mutation(api.categories.create, {
+ ...validCategory, name: 'Groceries', parentId,
+ })
+
+ const categories = await t.query(api.categories.list)
+ const child = categories!.find(c => c._id === childId)!
+ expect(child.parentId).toBe(parentId)
+ })
+
+ it('throws for empty name', async () => {
+ const t = convexTest(schema, modules)
+ await expect(
+ t.mutation(api.categories.create, { ...validCategory, name: ' ' }),
+ ).rejects.toThrow('Name is required')
+ })
+
+ it('throws for empty icon', async () => {
+ const t = convexTest(schema, modules)
+ await expect(
+ t.mutation(api.categories.create, { ...validCategory, icon: '' }),
+ ).rejects.toThrow('Icon is required')
+ })
+
+ it('throws for empty color', async () => {
+ const t = convexTest(schema, modules)
+ await expect(
+ t.mutation(api.categories.create, { ...validCategory, color: '' }),
+ ).rejects.toThrow('Color is required')
+ })
+
+ it('throws for duplicate name at same level', async () => {
+ const t = convexTest(schema, modules)
+ await t.mutation(api.categories.create, validCategory)
+ await expect(
+ t.mutation(api.categories.create, validCategory),
+ ).rejects.toThrow('Category with this name already exists')
+ })
+
+ it('allows same name under different parents', async () => {
+ const t = convexTest(schema, modules)
+ const parent1 = await t.mutation(api.categories.create, validCategory)
+ const parent2 = await t.mutation(api.categories.create, { ...validCategory, name: 'Transport' })
+
+ // Same child name under different parents — OK
+ await t.mutation(api.categories.create, { ...validCategory, name: 'Taxi', parentId: parent1 })
+ await t.mutation(api.categories.create, { ...validCategory, name: 'Taxi', parentId: parent2 })
+
+ const categories = await t.query(api.categories.list)
+ const taxis = categories!.filter(c => c.name === 'Taxi')
+ expect(taxis).toHaveLength(2)
+ })
+
+ it('throws for nesting more than 2 levels deep', async () => {
+ const t = convexTest(schema, modules)
+ const parentId = await t.mutation(api.categories.create, validCategory)
+ const childId = await t.mutation(api.categories.create, {
+ ...validCategory, name: 'Groceries', parentId,
+ })
+
+ await expect(
+ t.mutation(api.categories.create, {
+ ...validCategory, name: 'Dairy', parentId: childId,
+ }),
+ ).rejects.toThrow('Cannot nest more than 2 levels deep')
+ })
+
+ it('throws for unauthenticated user', async () => {
+ const t = convexTest(schema, modules)
+ setMockUser(null)
+ await expect(
+ t.mutation(api.categories.create, validCategory),
+ ).rejects.toThrow('Unauthorized')
+ })
+ })
+
+ describe('update', () => {
+ it('updates category fields', async () => {
+ const t = convexTest(schema, modules)
+ const id = await t.mutation(api.categories.create, validCategory)
+ await t.mutation(api.categories.update, { id, name: 'Dining', icon: 'restaurant' })
+
+ const categories = await t.query(api.categories.list)
+ expect(categories![0]).toMatchObject({ name: 'Dining', icon: 'restaurant' })
+ })
+
+ it('throws for duplicate name on rename', async () => {
+ const t = convexTest(schema, modules)
+ await t.mutation(api.categories.create, validCategory)
+ const id2 = await t.mutation(api.categories.create, { ...validCategory, name: 'Transport' })
+
+ await expect(
+ t.mutation(api.categories.update, { id: id2, name: 'Food' }),
+ ).rejects.toThrow('Category with this name already exists')
+ })
+
+ it('throws for category owned by another user', async () => {
+ const t = convexTest(schema, modules)
+ const id = await t.mutation(api.categories.create, validCategory)
+
+ setMockUser('other-user')
+ await expect(
+ t.mutation(api.categories.update, { id, name: 'Hack' }),
+ ).rejects.toThrow('Not found')
+ })
+
+ it('prevents setting self as parent', async () => {
+ const t = convexTest(schema, modules)
+ const id = await t.mutation(api.categories.create, validCategory)
+ await expect(
+ t.mutation(api.categories.update, { id, parentId: id }),
+ ).rejects.toThrow('Category cannot be its own parent')
+ })
+ })
+
+ describe('updateWithChildren', () => {
+ it('propagates color to children', async () => {
+ const t = convexTest(schema, modules)
+ const parentId = await t.mutation(api.categories.create, validCategory)
+ const childId = await t.mutation(api.categories.create, {
+ ...validCategory, name: 'Groceries', parentId,
+ })
+
+ await t.mutation(api.categories.updateWithChildren, {
+ childIds: [childId],
+ color: '#00ff00',
+ id: parentId,
+ })
+
+ const categories = await t.query(api.categories.list)
+ const parent = categories!.find(c => c._id === parentId)!
+ const child = categories!.find(c => c._id === childId)!
+ expect(parent.color).toBe('#00ff00')
+ expect(child.color).toBe('#00ff00')
+ })
+
+ it('throws if child does not belong to parent', async () => {
+ const t = convexTest(schema, modules)
+ const parentId = await t.mutation(api.categories.create, validCategory)
+ const otherId = await t.mutation(api.categories.create, { ...validCategory, name: 'Other' })
+
+ await expect(
+ t.mutation(api.categories.updateWithChildren, {
+ childIds: [otherId],
+ color: '#00ff00',
+ id: parentId,
+ }),
+ ).rejects.toThrow('Child does not belong to this parent')
+ })
+ })
+
+ describe('list', () => {
+ it('returns null for unauthenticated user', async () => {
+ const t = convexTest(schema, modules)
+ setMockUser(null)
+ expect(await t.query(api.categories.list)).toBeNull()
+ })
+
+ it('returns only own categories', async () => {
+ const t = convexTest(schema, modules)
+ await t.mutation(api.categories.create, validCategory)
+
+ setMockUser('other-user')
+ await t.mutation(api.categories.create, { ...validCategory, name: 'Other' })
+
+ const result = await t.query(api.categories.list)
+ expect(result).toHaveLength(1)
+ expect(result![0].name).toBe('Other')
+ })
+ })
+
+ describe('remove (cascade)', () => {
+ it('deletes category and its transactions', async () => {
+ const t = convexTest(schema, modules)
+ const walletId = await t.mutation(api.wallets.create, validWallet)
+ const catId = await t.mutation(api.categories.create, validCategory)
+
+ await t.mutation(api.trns.create, {
+ amount: 50, categoryId: catId, date: Date.now(), type: 0, walletId,
+ })
+
+ await t.action(api.categories.remove, { id: catId })
+
+ expect(await t.query(api.categories.list)).toHaveLength(0)
+ const trns = await t.query(api.trns.list, { paginationOpts: { cursor: null, numItems: 10 } })
+ expect(trns!.page).toHaveLength(0)
+ })
+
+ it('throws when deleting category with children', async () => {
+ const t = convexTest(schema, modules)
+ const parentId = await t.mutation(api.categories.create, validCategory)
+ await t.mutation(api.categories.create, {
+ ...validCategory, name: 'Child', parentId,
+ })
+
+ await expect(
+ t.action(api.categories.remove, { id: parentId }),
+ ).rejects.toThrow('Cannot delete category with children')
+ })
+ })
+})
diff --git a/convex/test-utils/setup.ts b/convex/test-utils/setup.ts
new file mode 100644
index 00000000..f361b10c
--- /dev/null
+++ b/convex/test-utils/setup.ts
@@ -0,0 +1,23 @@
+/// <reference types="vite/client" />
+import { beforeEach, vi } from 'vitest'
+
+export let mockUserId: string | null = 'test-user-id'
+
+export function setMockUser(userId: string | null) {
+ mockUserId = userId
+}
+
+beforeEach(() => {
+ mockUserId = 'test-user-id'
+})
+
+vi.mock('../auth', () => ({
+ authComponent: {
+ safeGetAuthUser: vi.fn(async () => {
+ if (!mockUserId) return null
+ return { _id: mockUserId }
+ }),
+ },
+}))
+
+export const modules = import.meta.glob('../**/*.ts')
diff --git a/convex/trns.test.ts b/convex/trns.test.ts
new file mode 100644
index 00000000..0193e1c6
--- /dev/null
+++ b/convex/trns.test.ts
@@ -0,0 +1,334 @@
+import { convexTest } from 'convex-test'
+import { describe, expect, it } from 'vitest'
+
+import { api } from './_generated/api'
+import schema from './schema'
+import { modules, setMockUser } from './test-utils/setup'
+
+const validWallet = {
+ color: '#000', currency: 'USD', name: 'Cash', order: 0, type: 'cash' as const,
+}
+
+const validCategory = {
+ color: '#ff0000', icon: 'food', name: 'Food',
+ showInLastUsed: true, showInQuickSelector: true,
+}
+
+async function setupWalletAndCategory(t: ReturnType<typeof convexTest>) {
+ const walletId = await t.mutation(api.wallets.create, validWallet)
+ const categoryId = await t.mutation(api.categories.create, validCategory)
+ return { categoryId, walletId }
+}
+
+async function setupTwoWallets(t: ReturnType<typeof convexTest>) {
+ const w1 = await t.mutation(api.wallets.create, { ...validWallet, order: 0 })
+ const w2 = await t.mutation(api.wallets.create, { ...validWallet, name: 'Card', order: 1 })
+ return { w1, w2 }
+}
+
+describe('convex/trns', () => {
+ describe('create — expense/income', () => {
+ it('creates an expense transaction', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+
+ const id = await t.mutation(api.trns.create, {
+ amount: 42.5, categoryId, date: Date.now(), type: 0, walletId,
+ })
+
+ const trns = await t.query(api.trns.list, { paginationOpts: { cursor: null, numItems: 10 } })
+ expect(trns!.page).toHaveLength(1)
+ expect(trns!.page[0]).toMatchObject({
+ _id: id,
+ amount: 42.5,
+ categoryId,
+ type: 0,
+ userId: 'test-user-id',
+ walletId,
+ })
+ })
+
+ it('creates an income transaction', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+
+ await t.mutation(api.trns.create, {
+ amount: 1000, categoryId, date: Date.now(), type: 1, walletId,
+ })
+
+ const trns = await t.query(api.trns.list, { paginationOpts: { cursor: null, numItems: 10 } })
+ expect(trns!.page[0].type).toBe(1)
+ })
+
+ it('creates an adjustment transaction', async () => {
+ const t = convexTest(schema, modules)
+ const { walletId } = await setupWalletAndCategory(t)
+
+ const id = await t.mutation(api.trns.create, {
+ amount: 500, categoryId: 'adjustment', date: Date.now(), type: 0, walletId,
+ })
+ expect(id).toBeDefined()
+ })
+
+ it('throws for zero amount', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+
+ await expect(
+ t.mutation(api.trns.create, {
+ amount: 0, categoryId, date: Date.now(), type: 0, walletId,
+ }),
+ ).rejects.toThrow('Amount must be positive')
+ })
+
+ it('throws for negative amount', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+
+ await expect(
+ t.mutation(api.trns.create, {
+ amount: -10, categoryId, date: Date.now(), type: 0, walletId,
+ }),
+ ).rejects.toThrow('Amount must be positive')
+ })
+
+ it('throws without wallet', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId } = await setupWalletAndCategory(t)
+
+ await expect(
+ t.mutation(api.trns.create, {
+ amount: 100, categoryId, date: Date.now(), type: 0,
+ }),
+ ).rejects.toThrow('Wallet is required')
+ })
+
+ it('throws without category', async () => {
+ const t = convexTest(schema, modules)
+ const { walletId } = await setupWalletAndCategory(t)
+
+ await expect(
+ t.mutation(api.trns.create, {
+ amount: 100, date: Date.now(), type: 0, walletId,
+ }),
+ ).rejects.toThrow('Category is required')
+ })
+
+ it('throws for wallet owned by another user', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+
+ // Create category as other-user
+ setMockUser('other-user')
+ const otherCat = await t.mutation(api.categories.create, { ...validCategory, name: 'Other' })
+
+ await expect(
+ t.mutation(api.trns.create, {
+ amount: 100, categoryId: otherCat, date: Date.now(), type: 0, walletId,
+ }),
+ ).rejects.toThrow('Wallet not found')
+ })
+
+ it('throws for "transfer" categoryId on non-transfer type', async () => {
+ const t = convexTest(schema, modules)
+ const { walletId } = await setupWalletAndCategory(t)
+
+ await expect(
+ t.mutation(api.trns.create, {
+ amount: 100, categoryId: 'transfer', date: Date.now(), type: 0, walletId,
+ }),
+ ).rejects.toThrow('categoryId "transfer" is only allowed for Transfer type')
+ })
+ })
+
+ describe('create — transfer', () => {
+ it('creates a transfer between two wallets', async () => {
+ const t = convexTest(schema, modules)
+ const { w1, w2 } = await setupTwoWallets(t)
+
+ const id = await t.mutation(api.trns.create, {
+ date: Date.now(),
+ expenseAmount: 100,
+ expenseWalletId: w1,
+ incomeAmount: 95,
+ incomeWalletId: w2,
+ type: 2,
+ })
+
+ const trns = await t.query(api.trns.list, { paginationOpts: { cursor: null, numItems: 10 } })
+ expect(trns!.page[0]).toMatchObject({
+ _id: id,
+ categoryId: 'transfer',
+ expenseAmount: 100,
+ expenseWalletId: w1,
+ incomeAmount: 95,
+ incomeWalletId: w2,
+ type: 2,
+ })
+ })
+
+ it('throws for same wallet on both sides', async () => {
+ const t = convexTest(schema, modules)
+ const { w1 } = await setupTwoWallets(t)
+
+ await expect(
+ t.mutation(api.trns.create, {
+ date: Date.now(),
+ expenseAmount: 100, expenseWalletId: w1,
+ incomeAmount: 100, incomeWalletId: w1,
+ type: 2,
+ }),
+ ).rejects.toThrow('Transfer wallets must be different')
+ })
+
+ it('throws for missing expense wallet', async () => {
+ const t = convexTest(schema, modules)
+ const { w2 } = await setupTwoWallets(t)
+
+ await expect(
+ t.mutation(api.trns.create, {
+ date: Date.now(),
+ expenseAmount: 100,
+ incomeAmount: 100, incomeWalletId: w2,
+ type: 2,
+ }),
+ ).rejects.toThrow('Transfer requires both wallets')
+ })
+
+ it('throws for zero expense amount', async () => {
+ const t = convexTest(schema, modules)
+ const { w1, w2 } = await setupTwoWallets(t)
+
+ await expect(
+ t.mutation(api.trns.create, {
+ date: Date.now(),
+ expenseAmount: 0, expenseWalletId: w1,
+ incomeAmount: 100, incomeWalletId: w2,
+ type: 2,
+ }),
+ ).rejects.toThrow('Transfer expense amount must be positive')
+ })
+
+ it('throws for non-"transfer" categoryId', async () => {
+ const t = convexTest(schema, modules)
+ const { w1, w2 } = await setupTwoWallets(t)
+ const catId = await t.mutation(api.categories.create, validCategory)
+
+ await expect(
+ t.mutation(api.trns.create, {
+ categoryId: catId,
+ date: Date.now(),
+ expenseAmount: 100, expenseWalletId: w1,
+ incomeAmount: 100, incomeWalletId: w2,
+ type: 2,
+ }),
+ ).rejects.toThrow('Transfer categoryId must be "transfer"')
+ })
+ })
+
+ describe('update', () => {
+ it('updates transaction fields', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+ const id = await t.mutation(api.trns.create, {
+ amount: 100, categoryId, date: Date.now(), type: 0, walletId,
+ })
+
+ await t.mutation(api.trns.update, { id, amount: 200, desc: 'Lunch' })
+
+ const trns = await t.query(api.trns.list, { paginationOpts: { cursor: null, numItems: 10 } })
+ expect(trns!.page[0]).toMatchObject({ amount: 200, desc: 'Lunch' })
+ })
+
+ it('throws for trn owned by another user', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+ const id = await t.mutation(api.trns.create, {
+ amount: 100, categoryId, date: Date.now(), type: 0, walletId,
+ })
+
+ setMockUser('other-user')
+ await expect(
+ t.mutation(api.trns.update, { id, amount: 999 }),
+ ).rejects.toThrow('Not found')
+ })
+ })
+
+ describe('remove', () => {
+ it('removes a transaction', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+ const id = await t.mutation(api.trns.create, {
+ amount: 100, categoryId, date: Date.now(), type: 0, walletId,
+ })
+
+ await t.mutation(api.trns.remove, { id })
+
+ const trns = await t.query(api.trns.list, { paginationOpts: { cursor: null, numItems: 10 } })
+ expect(trns!.page).toHaveLength(0)
+ })
+
+ it('silently ignores trn owned by another user', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+ const id = await t.mutation(api.trns.create, {
+ amount: 100, categoryId, date: Date.now(), type: 0, walletId,
+ })
+
+ setMockUser('other-user')
+ await t.mutation(api.trns.remove, { id })
+
+ // Trn still exists
+ setMockUser('test-user-id')
+ const trns = await t.query(api.trns.list, { paginationOpts: { cursor: null, numItems: 10 } })
+ expect(trns!.page).toHaveLength(1)
+ })
+ })
+
+ describe('idsHash', () => {
+ it('returns null for unauthenticated user', async () => {
+ const t = convexTest(schema, modules)
+ setMockUser(null)
+ expect(await t.query(api.trns.idsHash)).toBeNull()
+ })
+
+ it('returns consistent hash after create+delete cycle (XOR symmetry)', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+
+ const hashBefore = await t.query(api.trns.idsHash)
+
+ const id = await t.mutation(api.trns.create, {
+ amount: 50, categoryId, date: Date.now(), type: 0, walletId,
+ })
+ await t.mutation(api.trns.remove, { id })
+
+ const hashAfter = await t.query(api.trns.idsHash)
+ expect(hashAfter!.hash).toBe(hashBefore!.hash)
+ })
+ })
+
+ describe('delta', () => {
+ it('returns null for unauthenticated user', async () => {
+ const t = convexTest(schema, modules)
+ setMockUser(null)
+ expect(await t.query(api.trns.delta, {
+ paginationOpts: { cursor: null, numItems: 10 }, since: 0,
+ })).toBeNull()
+ })
+
+ it('returns transactions for authenticated user', async () => {
+ const t = convexTest(schema, modules)
+ const { categoryId, walletId } = await setupWalletAndCategory(t)
+
+ await t.mutation(api.trns.create, {
+ amount: 100, categoryId, date: Date.now(), type: 0, walletId,
+ })
+
+ const result = await t.query(api.trns.delta, {
+ paginationOpts: { cursor: null, numItems: 10 }, since: 0,
+ })
+ expect(result!.page).toHaveLength(1)
+ })
+ })
+})
diff --git a/convex/wallets.test.ts b/convex/wallets.test.ts
new file mode 100644
index 00000000..55526a1d
--- /dev/null
+++ b/convex/wallets.test.ts
@@ -0,0 +1,209 @@
+import { convexTest } from 'convex-test'
+import { describe, expect, it } from 'vitest'
+
+import { api, internal } from './_generated/api'
+import schema from './schema'
+import { modules, setMockUser } from './test-utils/setup'
+
+const validWallet = {
+ color: '#ff0000',
+ currency: 'USD',
+ name: 'Cash',
+ order: 0,
+ type: 'cash' as const,
+}
+
+describe('convex/wallets', () => {
+ describe('create', () => {
+ it('creates a wallet with defaults', async () => {
+ const t = convexTest(schema, modules)
+ const id = await t.mutation(api.wallets.create, validWallet)
+
+ const wallets = await t.query(api.wallets.list)
+ expect(wallets).toHaveLength(1)
+ expect(wallets![0]).toMatchObject({
+ ...validWallet,
+ _id: id,
+ isArchived: false,
+ isExcludeInTotal: false,
+ isWithdrawal: false,
+ userId: 'test-user-id',
+ })
+ })
+
+ it('creates with optional fields', async () => {
+ const t = convexTest(schema, modules)
+ await t.mutation(api.wallets.create, {
+ ...validWallet,
+ creditLimit: 5000,
+ desc: 'My wallet',
+ isArchived: true,
+ isWithdrawal: true,
+ type: 'credit',
+ })
+
+ const wallets = await t.query(api.wallets.list)
+ expect(wallets![0]).toMatchObject({
+ creditLimit: 5000,
+ desc: 'My wallet',
+ isArchived: true,
+ isWithdrawal: true,
+ type: 'credit',
+ })
+ })
+
+ it('throws for empty name', async () => {
+ const t = convexTest(schema, modules)
+ await expect(
+ t.mutation(api.wallets.create, { ...validWallet, name: ' ' }),
+ ).rejects.toThrow('Name is required')
+ })
+
+ it('throws for empty color', async () => {
+ const t = convexTest(schema, modules)
+ await expect(
+ t.mutation(api.wallets.create, { ...validWallet, color: '' }),
+ ).rejects.toThrow('Color is required')
+ })
+
+ it('throws for empty currency', async () => {
+ const t = convexTest(schema, modules)
+ await expect(
+ t.mutation(api.wallets.create, { ...validWallet, currency: '' }),
+ ).rejects.toThrow('Currency is required')
+ })
+
+ it('throws for name exceeding 100 chars', async () => {
+ const t = convexTest(schema, modules)
+ await expect(
+ t.mutation(api.wallets.create, { ...validWallet, name: 'a'.repeat(101) }),
+ ).rejects.toThrow('Name must be at most 100 characters')
+ })
+
+ it('throws for credit limit exceeding max', async () => {
+ const t = convexTest(schema, modules)
+ await expect(
+ t.mutation(api.wallets.create, { ...validWallet, creditLimit: 2_000_000_000 }),
+ ).rejects.toThrow('Credit limit must be between')
+ })
+
+ it('throws for unauthenticated user', async () => {
+ const t = convexTest(schema, modules)
+ setMockUser(null)
+ await expect(
+ t.mutation(api.wallets.create, validWallet),
+ ).rejects.toThrow('Unauthorized')
+ })
+ })
+
+ describe('update', () => {
+ it('updates wallet fields', async () => {
+ const t = convexTest(schema, modules)
+ const id = await t.mutation(api.wallets.create, validWallet)
+ await t.mutation(api.wallets.update, { id, name: 'Updated', color: '#00ff00' })
+
+ const wallets = await t.query(api.wallets.list)
+ expect(wallets![0]).toMatchObject({ name: 'Updated', color: '#00ff00' })
+ })
+
+ it('throws for wallet owned by another user', async () => {
+ const t = convexTest(schema, modules)
+ const id = await t.mutation(api.wallets.create, validWallet)
+
+ setMockUser('other-user')
+ await expect(
+ t.mutation(api.wallets.update, { id, name: 'Hack' }),
+ ).rejects.toThrow('Not found')
+ })
+
+ it('throws for empty name on update', async () => {
+ const t = convexTest(schema, modules)
+ const id = await t.mutation(api.wallets.create, validWallet)
+ await expect(
+ t.mutation(api.wallets.update, { id, name: ' ' }),
+ ).rejects.toThrow('Name is required')
+ })
+ })
+
+ describe('updateOrder', () => {
+ it('reorders wallets', async () => {
+ const t = convexTest(schema, modules)
+ const id1 = await t.mutation(api.wallets.create, { ...validWallet, order: 0 })
+ const id2 = await t.mutation(api.wallets.create, { ...validWallet, name: 'Card', order: 1 })
+
+ await t.mutation(api.wallets.updateOrder, {
+ orders: [{ id: id1, order: 1 }, { id: id2, order: 0 }],
+ })
+
+ const wallets = await t.query(api.wallets.list)
+ const w1 = wallets!.find(w => w._id === id1)!
+ const w2 = wallets!.find(w => w._id === id2)!
+ expect(w1.order).toBe(1)
+ expect(w2.order).toBe(0)
+ })
+
+ it('throws for wallet owned by another user', async () => {
+ const t = convexTest(schema, modules)
+ const id = await t.mutation(api.wallets.create, validWallet)
+
+ setMockUser('other-user')
+ await expect(
+ t.mutation(api.wallets.updateOrder, { orders: [{ id, order: 5 }] }),
+ ).rejects.toThrow('Not found')
+ })
+ })
+
+ describe('list', () => {
+ it('returns null for unauthenticated user', async () => {
+ const t = convexTest(schema, modules)
+ setMockUser(null)
+ expect(await t.query(api.wallets.list)).toBeNull()
+ })
+
+ it('returns only own wallets', async () => {
+ const t = convexTest(schema, modules)
+ await t.mutation(api.wallets.create, validWallet)
+
+ setMockUser('other-user')
+ await t.mutation(api.wallets.create, { ...validWallet, name: 'Other' })
+
+ const otherWallets = await t.query(api.wallets.list)
+ expect(otherWallets).toHaveLength(1)
+ expect(otherWallets![0].name).toBe('Other')
+ })
+ })
+
+ describe('remove (cascade)', () => {
+ it('deletes wallet and its transactions', async () => {
+ const t = convexTest(schema, modules)
+ const walletId = await t.mutation(api.wallets.create, validWallet)
+
+ // Create a category for the trn
+ const catId = await t.mutation(api.categories.create, {
+ color: '#000', icon: 'food', name: 'Food',
+ showInLastUsed: true, showInQuickSelector: true,
+ })
+
+ // Create trn referencing this wallet
+ await t.mutation(api.trns.create, {
+ amount: 100, categoryId: catId, date: Date.now(),
+ type: 0, walletId,
+ })
+
+ // Verify trn exists
+ const trnsBefore = await t.query(api.trns.list, { paginationOpts: { cursor: null, numItems: 10 } })
+ expect(trnsBefore!.page).toHaveLength(1)
+
+ // Remove wallet (cascade action)
+ await t.action(api.wallets.remove, { id: walletId })
+
+ // Wallet gone
+ const wallets = await t.query(api.wallets.list)
+ expect(wallets).toHaveLength(0)
+
+ // Trn gone too
+ const trnsAfter = await t.query(api.trns.list, { paginationOpts: { cursor: null, numItems: 10 } })
+ expect(trnsAfter!.page).toHaveLength(0)
+ })
+ })
+})
diff --git a/package.json b/package.json
index 46b7ee6f..3c1a23f1 100644
--- a/package.json
+++ b/package.json
@@ -49,6 +49,7 @@
},
"devDependencies": {
"@antfu/eslint-config": "^6.4.1",
+ "@edge-runtime/vm": "^5.0.0",
"@iconify-json/lucide": "^1.2.78",
"@nuxt/eslint": "^1.11.0",
"@nuxt/ui": "^4.2.1",
@@ -59,6 +60,7 @@
"@vueuse/core": "^13.9.0",
"@vueuse/nuxt": "^13.9.0",
"clsx": "^2.1.1",
+ "convex-test": "^0.0.41",
"date-fns": "^4.1.0",
"defu": "^6.1.4",
"dotenv": "^17.2.3",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d66903b9..eae44cb2 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -10,7 +10,7 @@ importers:
dependencies:
'@convex-dev/better-auth':
specifier: ^0.10.10
'@formkit/drag-and-drop':
specifier: ^0.5.3
version: 0.5.3
@@ -25,7 +25,7 @@ importers:
better-auth:
specifier: ^1.4.9
convex:
specifier: ^1.22.0
version: 1.31.7([email protected])
@@ -62,7 +62,10 @@ importers:
devDependencies:
'@antfu/eslint-config':
specifier: ^6.4.1
+ '@edge-runtime/vm':
+ specifier: ^5.0.0
+ version: 5.0.0
'@iconify-json/lucide':
specifier: ^1.2.78
version: 1.2.78
@@ -83,7 +86,7 @@ importers:
'@vitest/coverage-v8':
specifier: ^4.0.15
'@vueuse/core':
specifier: ^13.9.0
@@ -93,6 +96,9 @@ importers:
clsx:
specifier: ^2.1.1
version: 2.1.1
+ convex-test:
+ specifier: ^0.0.41
date-fns:
specifier: ^4.1.0
version: 4.1.0
@@ -143,7 +149,7 @@ importers:
version: 5.9.3
vitest:
specifier: ^4.0.15
workbox-core:
specifier: ^7.4.0
version: 7.4.0
@@ -924,6 +930,14 @@ packages:
resolution: {integrity: sha512-/B8YJGPzaYq1NbsQmwgP8EZqg40NpTw4ZB3suuI0TplbxKHeK94jeaawLmVhCv+YwUnOpiWEz9U6SeThku/8JQ==}
+ '@edge-runtime/[email protected]':
+ resolution: {integrity: sha512-FqoxaBT+prPBHBwE1WXS1ocnu/VLTQyZ6NMUBAdbP7N2hsFTTxMC/jMu2D/8GAlMQfxeuppcPuCUk/HO3fpIvA==}
+ engines: {node: '>=18'}
+
+ '@edge-runtime/[email protected]':
+ resolution: {integrity: sha512-NKBGBSIKUG584qrS1tyxVpX/AKJKQw5HgjYEnPLC0QsTw79JrGn+qUr8CXFb955Iy7GUdiiUv1rJ6JBGvaKb6w==}
+ engines: {node: '>=18'}
+
'@emnapi/[email protected]':
resolution: {integrity: sha512-sbP8GzB1WDzacS8fgNPpHlp6C9VZe+SJP3F90W9rLemaQj2PzIuTEl1qDOYQf58YIpyjViI24y9aPWCjEzY2cg==}
@@ -4145,6 +4159,11 @@ packages:
zod:
optional: true
+ resolution: {integrity: sha512-GPHeYFOi70n7UtW0eCEQFVhzl/+m8PvbWkDCbKpHLybI1MrScf4sVpGeM0cC2qmtxiduxa2nLPbehPalhh9oyQ==}
+ peerDependencies:
+ convex: ^1.16.4
+
resolution: {integrity: sha512-PtNMe1mAIOvA8Yz100QTOaIdgt2rIuWqencVXrb4McdhxBHZ8IJ1eXTnrgCC9HydyilGT1pOn+KNqT14mqn9fQ==}
engines: {node: '>=18.0.0', npm: '>=7.0.0'}
@@ -8127,7 +8146,7 @@ snapshots:
'@jridgewell/gen-mapping': 0.3.12
'@jridgewell/trace-mapping': 0.3.29
dependencies:
'@antfu/install-pkg': 1.1.0
'@clack/prompts': 0.11.0
@@ -8136,7 +8155,7 @@ snapshots:
'@stylistic/eslint-plugin': 5.6.1([email protected]([email protected]))
'@typescript-eslint/parser': 8.48.1([email protected]([email protected]))([email protected])
ansis: 4.2.0
cac: 6.7.14
eslint: 9.39.1([email protected])
@@ -9035,14 +9054,14 @@ snapshots:
nanostores: 1.1.0
zod: 4.3.6
dependencies:
'@better-auth/utils': 0.3.0
'@better-fetch/fetch': 1.1.21
'@simplewebauthn/browser': 13.2.2
'@simplewebauthn/server': 13.2.2
better-call: 1.1.8([email protected])
nanostores: 1.1.0
zod: 4.3.6
@@ -9076,11 +9095,11 @@ snapshots:
dependencies:
mime: 3.0.0
dependencies:
'@better-fetch/fetch': 1.1.21
common-tags: 1.8.2
convex: 1.31.7([email protected])
@@ -9118,6 +9137,12 @@ snapshots:
'@dxup/[email protected]': {}
+ '@edge-runtime/[email protected]': {}
+
+ '@edge-runtime/[email protected]':
+ dependencies:
+ '@edge-runtime/primitives': 6.0.0
+
'@emnapi/[email protected]':
dependencies:
'@emnapi/wasi-threads': 1.1.0
@@ -11599,7 +11624,7 @@ snapshots:
vue: 3.5.25([email protected])
dependencies:
'@bcoe/v8-coverage': 1.0.2
'@vitest/utils': 4.0.15
@@ -11612,18 +11637,18 @@ snapshots:
obug: 2.1.1
std-env: 3.10.0
tinyrainbow: 3.0.3
transitivePeerDependencies:
- supports-color
dependencies:
'@typescript-eslint/scope-manager': 8.47.0
'@typescript-eslint/utils': 8.47.0([email protected]([email protected]))([email protected])
eslint: 9.39.1([email protected])
optionalDependencies:
typescript: 5.9.3
transitivePeerDependencies:
- supports-color
@@ -12137,7 +12162,7 @@ snapshots:
dependencies:
@@ -12154,7 +12179,7 @@ snapshots:
optionalDependencies:
react: 19.2.4
react-dom: 19.2.4([email protected])
vue: 3.5.25([email protected])
@@ -12427,6 +12452,10 @@ snapshots:
typescript: 5.9.3
zod: 4.3.6
+ dependencies:
+ convex: 1.31.7([email protected])
+
dependencies:
esbuild: 0.27.0
@@ -16689,7 +16718,7 @@ snapshots:
terser: 5.43.1
yaml: 2.8.2
dependencies:
'@vitest/expect': 4.0.15
@@ -16712,6 +16741,7 @@ snapshots:
why-is-node-running: 2.3.0
optionalDependencies:
+ '@edge-runtime/vm': 5.0.0
'@opentelemetry/api': 1.9.0
'@types/node': 25.2.3
happy-dom: 20.8.3
diff --git a/vitest.config.ts b/vitest.config.ts
index ae275be3..b46084dd 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -30,6 +30,15 @@ export default defineConfig({
setupFiles: ['./app/test-utils/setup-store.ts'],
},
},
+ {
+ resolve: { alias },
+ test: {
+ environment: 'edge-runtime',
+ include: ['convex/**/*.test.ts'],
+ name: 'convex',
+ setupFiles: ['./convex/test-utils/setup.ts'],
+ },
+ },
],
},
})