refactor(memories, photos, packing, share): async DB-touching methods; transactions through UnitOfWork

This commit is contained in:
jubnl
2026-09-22 02:21:06 +02:00
parent 6b755314d8
commit 37992de7ab
48 changed files with 1250 additions and 1219 deletions
@@ -19,17 +19,17 @@ const writeAudit = vi.fn();
function controller(over: Partial<PackingService> = {}) {
const packing = {
listPackingTemplates: vi.fn(() => [{ id: 1 }]),
getPackingTemplate: vi.fn(() => ({ template: { id: 1 } })),
createPackingTemplate: vi.fn(() => ({ template: { id: 9 } })),
updatePackingTemplate: vi.fn(() => ({ template: { id: 1 } })),
deletePackingTemplate: vi.fn(() => ({ name: 'Beach' })),
createTemplateCategory: vi.fn(() => ({ category: { id: 2 } })),
updateTemplateCategory: vi.fn(() => ({ category: { id: 2 } })),
deleteTemplateCategory: vi.fn(() => ({ ok: true })),
createTemplateItem: vi.fn(() => ({ item: { id: 3 } })),
updateTemplateItem: vi.fn(() => ({ item: { id: 3 } })),
deleteTemplateItem: vi.fn(() => ({ ok: true })),
listPackingTemplates: vi.fn(async () => [{ id: 1 }]),
getPackingTemplate: vi.fn(async () => ({ template: { id: 1 } })),
createPackingTemplate: vi.fn(async () => ({ template: { id: 9 } })),
updatePackingTemplate: vi.fn(async () => ({ template: { id: 1 } })),
deletePackingTemplate: vi.fn(async () => ({ name: 'Beach' })),
createTemplateCategory: vi.fn(async () => ({ category: { id: 2 } })),
updateTemplateCategory: vi.fn(async () => ({ category: { id: 2 } })),
deleteTemplateCategory: vi.fn(async () => ({ ok: true })),
createTemplateItem: vi.fn(async () => ({ item: { id: 3 } })),
updateTemplateItem: vi.fn(async () => ({ item: { id: 3 } })),
deleteTemplateItem: vi.fn(async () => ({ ok: true })),
...over,
} as unknown as PackingService;
return { c: new AdminPackingTemplatesController(packing, { writeAudit } as unknown as AuditService), packing };
@@ -45,9 +45,9 @@ function offContract<K extends keyof PackingService>(name: K, impl: () => unknow
return { [name]: vi.fn(impl) } as unknown as Partial<PackingService>;
}
const thrown = (run: () => unknown) => {
const thrown = async (run: () => unknown) => {
try {
run();
await run();
return null;
} catch (e) {
return e instanceof HttpException ? { status: e.getStatus(), body: e.getResponse() } : e;
@@ -59,18 +59,18 @@ describe('AdminPackingTemplatesController', () => {
vi.clearAllMocks();
});
it('PACKTPL-001 list wraps the service result in the { templates } envelope', () => {
expect(controller().c.list()).toEqual({ templates: [{ id: 1 }] });
it('PACKTPL-001 list wraps the service result in the { templates } envelope', async () => {
expect(await controller().c.list()).toEqual({ templates: [{ id: 1 }] });
});
it('PACKTPL-002 a service {error,status} becomes that HTTP status, not a 200 body', () => {
const { c } = controller({ getPackingTemplate: vi.fn(() => ({ error: 'not found', status: 404 })) });
expect(thrown(() => c.get('9'))).toEqual({ status: 404, body: { error: 'not found' } });
it('PACKTPL-002 a service {error,status} becomes that HTTP status, not a 200 body', async () => {
const { c } = controller({ getPackingTemplate: vi.fn(async () => ({ error: 'not found', status: 404 })) });
expect(await thrown(() => c.get('9'))).toEqual({ status: 404, body: { error: 'not found' } });
});
it('PACKTPL-003 an error without a status defaults to 400', () => {
const { c } = controller(offContract('createTemplateCategory', () => ({ error: 'name required' })));
expect(thrown(() => c.createCategory('1', { name: '' }))).toEqual({ status: 400, body: { error: 'name required' } });
it('PACKTPL-003 an error without a status defaults to 400', async () => {
const { c } = controller(offContract('createTemplateCategory', async () => ({ error: 'name required' })));
expect(await thrown(() => c.createCategory('1', { name: '' }))).toEqual({ status: 400, body: { error: 'name required' } });
});
it('PACKTPL-004 create audits with the new template id', async () => {
@@ -90,26 +90,26 @@ describe('AdminPackingTemplatesController', () => {
);
});
it('PACKTPL-006 the nested category and item routes forward their ids in order', () => {
it('PACKTPL-006 the nested category and item routes forward their ids in order', async () => {
const { c, packing } = controller();
c.createCategory('1', { name: 'Clothes' });
await c.createCategory('1', { name: 'Clothes' });
expect(packing.createTemplateCategory).toHaveBeenCalledWith('1', 'Clothes');
c.updateCategory('1', '2', { name: 'Warm' });
await c.updateCategory('1', '2', { name: 'Warm' });
expect(packing.updateTemplateCategory).toHaveBeenCalledWith('1', '2', { name: 'Warm' });
expect(c.deleteCategory('1', '2')).toEqual({ success: true });
c.createItem('1', '2', { name: 'Socks' });
expect(await c.deleteCategory('1', '2')).toEqual({ success: true });
await c.createItem('1', '2', { name: 'Socks' });
expect(packing.createTemplateItem).toHaveBeenCalledWith('1', '2', 'Socks');
c.updateItem('1', '3', { name: 'Wool socks' });
await c.updateItem('1', '3', { name: 'Wool socks' });
expect(packing.updateTemplateItem).toHaveBeenCalledWith('1', '3', { name: 'Wool socks' });
expect(c.deleteItem('1', '3')).toEqual({ success: true });
expect(await c.deleteItem('1', '3')).toEqual({ success: true });
});
it('PACKTPL-007 the delete routes answer { success: true } rather than the service payload', () => {
it('PACKTPL-007 the delete routes answer { success: true } rather than the service payload', async () => {
// The client branches on `success`; leaking the raw service result here would be a
// silent shape change for every caller.
const { c } = controller();
expect(c.deleteCategory('1', '2')).toEqual({ success: true });
expect(c.deleteItem('1', '3')).toEqual({ success: true });
expect(await c.deleteCategory('1', '2')).toEqual({ success: true });
expect(await c.deleteItem('1', '3')).toEqual({ success: true });
});
it('PACKTPL-008 the class is listed in its module controllers', () => {