Spaces:
Sleeping
Sleeping
File size: 7,612 Bytes
1f5ea39 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | /**
* Unit tests for categoryService β CAT-SVC-001 through CAT-SVC-015.
* Uses a real in-memory SQLite DB so SQL logic is exercised faithfully.
*/
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
// ββ DB setup ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const { testDb, dbMock } = vi.hoisted(() => {
const Database = require('better-sqlite3');
const db = new Database(':memory:');
db.exec('PRAGMA journal_mode = WAL');
db.exec('PRAGMA foreign_keys = ON');
db.exec('PRAGMA busy_timeout = 5000');
const mock = {
db,
closeDb: () => {},
reinitialize: () => {},
getPlaceWithTags: () => null,
canAccessTrip: () => null,
isOwner: () => false,
};
return { testDb: db, dbMock: mock };
});
vi.mock('../../../src/db/database', () => dbMock);
vi.mock('../../../src/config', () => ({
JWT_SECRET: 'test-secret',
ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
updateJwtSecret: () => {},
}));
import { createTables } from '../../../src/db/schema';
import { runMigrations } from '../../../src/db/migrations';
import { resetTestDb } from '../../helpers/test-db';
import { createUser } from '../../helpers/factories';
import {
listCategories,
createCategory,
getCategoryById,
updateCategory,
deleteCategory,
} from '../../../src/services/categoryService';
beforeAll(() => {
createTables(testDb);
runMigrations(testDb);
});
beforeEach(() => {
resetTestDb(testDb);
});
afterAll(() => {
testDb.close();
});
// ββ listCategories ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('listCategories', () => {
it('CAT-SVC-001 β returns an array (seeded defaults are present after migrations)', () => {
// Migrations seed default categories, so the list is never empty in a fully initialized DB
const cats = listCategories() as any[];
expect(Array.isArray(cats)).toBe(true);
expect(cats.length).toBeGreaterThan(0);
});
it('CAT-SVC-002 β results are ordered by name ascending (custom categories sort correctly)', () => {
const { user } = createUser(testDb);
createCategory(user.id, 'Zoo');
createCategory(user.id, 'Aquarium');
// Migrations seed default categories; verify ordering by checking our custom ones appear in sorted order
const names = (listCategories() as any[]).map((c: any) => c.name);
const aquariumIdx = names.indexOf('Aquarium');
const zooIdx = names.indexOf('Zoo');
expect(aquariumIdx).toBeGreaterThanOrEqual(0);
expect(zooIdx).toBeGreaterThanOrEqual(0);
expect(aquariumIdx).toBeLessThan(zooIdx);
});
it('CAT-SVC-003 β returns categories from all users (including seeded defaults)', () => {
const { user: a } = createUser(testDb);
const { user: b } = createUser(testDb);
const before = (listCategories() as any[]).length;
createCategory(a.id, 'Cat-A');
createCategory(b.id, 'Cat-B');
expect(listCategories()).toHaveLength(before + 2);
});
});
// ββ createCategory ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('createCategory', () => {
it('CAT-SVC-004 β creates a category with name, color, and icon', () => {
const { user } = createUser(testDb);
const cat = createCategory(user.id, 'Restaurant', '#ff5500', 'π½οΈ') as any;
expect(cat.name).toBe('Restaurant');
expect(cat.color).toBe('#ff5500');
expect(cat.icon).toBe('π½οΈ');
expect(cat.user_id).toBe(user.id);
});
it('CAT-SVC-005 β defaults color to #6366f1 when not provided', () => {
const { user } = createUser(testDb);
const cat = createCategory(user.id, 'Default Color') as any;
expect(cat.color).toBe('#6366f1');
});
it('CAT-SVC-006 β defaults icon to π when not provided', () => {
const { user } = createUser(testDb);
const cat = createCategory(user.id, 'Default Icon') as any;
expect(cat.icon).toBe('π');
});
it('CAT-SVC-007 β returns the inserted row with an id', () => {
const { user } = createUser(testDb);
const cat = createCategory(user.id, 'WithId') as any;
expect(typeof cat.id).toBe('number');
expect(cat.id).toBeGreaterThan(0);
});
});
// ββ getCategoryById βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('getCategoryById', () => {
it('CAT-SVC-008 β returns category for a valid id', () => {
const { user } = createUser(testDb);
const created = createCategory(user.id, 'Find Me') as any;
const found = getCategoryById(created.id) as any;
expect(found).toBeDefined();
expect(found.name).toBe('Find Me');
});
it('CAT-SVC-009 β returns undefined for non-existent id', () => {
expect(getCategoryById(99999)).toBeUndefined();
});
it('CAT-SVC-010 β accepts string id (coerced by SQLite)', () => {
const { user } = createUser(testDb);
const created = createCategory(user.id, 'StringId') as any;
const found = getCategoryById(String(created.id)) as any;
expect(found).toBeDefined();
expect(found.id).toBe(created.id);
});
});
// ββ updateCategory ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('updateCategory', () => {
it('CAT-SVC-011 β updates name, color, and icon', () => {
const { user } = createUser(testDb);
const cat = createCategory(user.id, 'Old', '#aaaaaa', 'β') as any;
const updated = updateCategory(cat.id, 'New', '#bbbbbb', 'β
') as any;
expect(updated.name).toBe('New');
expect(updated.color).toBe('#bbbbbb');
expect(updated.icon).toBe('β
');
});
it('CAT-SVC-012 β COALESCE: omitting name preserves existing name', () => {
const { user } = createUser(testDb);
const cat = createCategory(user.id, 'KeepName', '#aaaaaa', 'β') as any;
const updated = updateCategory(cat.id, undefined, '#cccccc', 'π₯') as any;
expect(updated.name).toBe('KeepName');
expect(updated.color).toBe('#cccccc');
});
it('CAT-SVC-013 β COALESCE: omitting color preserves existing color', () => {
const { user } = createUser(testDb);
const cat = createCategory(user.id, 'KeepColor', '#dddddd', 'β') as any;
const updated = updateCategory(cat.id, 'NewName', undefined, 'π') as any;
expect(updated.name).toBe('NewName');
expect(updated.color).toBe('#dddddd');
});
});
// ββ deleteCategory ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('deleteCategory', () => {
it('CAT-SVC-014 β deletes the category from the database', () => {
const { user } = createUser(testDb);
const cat = createCategory(user.id, 'ToDelete') as any;
deleteCategory(cat.id);
expect(getCategoryById(cat.id)).toBeUndefined();
});
it('CAT-SVC-015 β deleting a non-existent category does not throw', () => {
expect(() => deleteCategory(99999)).not.toThrow();
});
});
|