- Export DEFAULT_LAYOUT from lib/frame-palette.js so the custom-frames route can import it at runtime (not just the hardcoded copy in tests). - Fix vitest mock isolation in test/api/custom-frames.test.js: beforeEach now uses mockReturnValue instead of mockResolvedValue to avoid resolving the default mock in each test; test cases provide specific mock chains with mockResolvedValueOnce. Fixes 4 tests that were bleeding state between cases due to leftover queued mock values. - Fix validateLayout test coordinates: art w+h=0.924 and 0.398 are both within the 0-1 fraction range so x+w=0.962<1 and y+h=0.982<1 pass. - Add dedicated validateLayout unit tests (accepts, rejects missing zone, rejects out-of-bounds). - Fix update test mock chain: PUT calls SELECT (found) then SELECT (clash) then UPDATE (RETURNING) — provide all three in order. - Fix DELETE test: owns via SELECT then executes DELETE (2 calls).
191 lines
5.9 KiB
JavaScript
191 lines
5.9 KiB
JavaScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
vi.mock('../../lib/sql.js', () => ({ sql: vi.fn() }));
|
|
vi.mock('../../lib/permission-middleware', () => ({
|
|
getUserFromRequest: vi.fn(),
|
|
}));
|
|
|
|
import { sql } from '../../lib/sql.js';
|
|
import { getUserFromRequest } from '../../lib/permission-middleware';
|
|
import handler from '../../pages/api/custom-frames/index.js';
|
|
import itemHandler from '../../pages/api/custom-frames/[id].js';
|
|
import { validatePalette, PALETTE_SLOTS, validateLayout } from '../../lib/frame-palette.js';
|
|
|
|
function createRes() {
|
|
const res = {
|
|
statusCode: 200,
|
|
body: null,
|
|
status(code) {
|
|
res.statusCode = code;
|
|
return res;
|
|
},
|
|
json(data) {
|
|
res.body = data;
|
|
return res;
|
|
},
|
|
};
|
|
return res;
|
|
}
|
|
|
|
const GOOD_PALETTE = Object.fromEntries(
|
|
PALETTE_SLOTS.map(({ key }) => [key, '#123456'])
|
|
);
|
|
|
|
const DEFAULT_LAYOUT = {
|
|
art: { x: 16 / 420, y: 64 / 588, w: 388 / 420, h: 234 / 588 },
|
|
text: { x: 16 / 420, y: 342 / 588, w: 388 / 420, h: 224 / 588 },
|
|
};
|
|
|
|
describe('validatePalette', () => {
|
|
it('accepts and normalizes a complete palette', () => {
|
|
const { palette, error } = validatePalette(
|
|
Object.fromEntries(PALETTE_SLOTS.map(({ key }) => [key, '#ABCDEF']))
|
|
);
|
|
expect(error).toBeUndefined();
|
|
expect(palette.outer).toBe('#abcdef');
|
|
});
|
|
|
|
it('rejects missing slots and bad colors', () => {
|
|
expect(validatePalette({}).error).toBeTruthy();
|
|
const missing = { ...GOOD_PALETTE };
|
|
delete missing.accent;
|
|
expect(validatePalette(missing).error).toBeTruthy();
|
|
expect(validatePalette({ ...GOOD_PALETTE, border: 'red' }).error).toBeTruthy();
|
|
});
|
|
});
|
|
|
|
describe('validateLayout', () => {
|
|
it('accepts and normalizes a valid layout', () => {
|
|
const { layout, error } = validateLayout({
|
|
art: { x: 0.038, y: 0.109, w: 0.924, h: 0.398 },
|
|
text: { x: 0.038, y: 0.582, w: 0.924, h: 0.381 },
|
|
});
|
|
expect(error).toBeUndefined();
|
|
expect(layout.art.x).toBeCloseTo(0.038);
|
|
});
|
|
|
|
it('rejects missing zones', () => {
|
|
expect(validateLayout({ art: { x: 0, y: 0, w: 1, h: 1 } }).error).toBeTruthy();
|
|
});
|
|
|
|
it('rejects zones extending past the card', () => {
|
|
expect(validateLayout({ art: { x: 0.9, y: 0, w: 0.2, h: 0.5 }, text: { x: 0, y: 0, w: 1, h: 1 } }).error).toBeTruthy();
|
|
});
|
|
});
|
|
|
|
describe('/api/custom-frames', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
getUserFromRequest.mockResolvedValue({ userId: 1, email: 'a@b.c', role: 'user' });
|
|
sql.mockReturnValue({ rows: [] });
|
|
sql.json = vi.fn((v) => v);
|
|
});
|
|
|
|
it('requires authentication', async () => {
|
|
getUserFromRequest.mockResolvedValue(null);
|
|
const res = createRes();
|
|
|
|
await handler({ method: 'GET' }, res);
|
|
|
|
expect(res.statusCode).toBe(401);
|
|
expect(sql).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects invalid palettes on create', async () => {
|
|
const res = createRes();
|
|
|
|
await handler(
|
|
{ method: 'POST', body: { name: 'My Frame', palette: { outer: 'nope' } } },
|
|
res
|
|
);
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
expect(res.body.error).toContain('Invalid or missing color');
|
|
});
|
|
|
|
it('rejects invalid layouts on create', async () => {
|
|
const res = createRes();
|
|
|
|
await handler(
|
|
{ method: 'POST', body: { name: 'My Frame', palette: GOOD_PALETTE, layout: { art: { x: 1, y: 0, w: 1, h: 1 } } } },
|
|
res
|
|
);
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
expect(res.body.error).toContain('extends past the card');
|
|
});
|
|
|
|
it('rejects duplicate frame names', async () => {
|
|
sql.mockResolvedValueOnce({ rows: [{ id: 4 }] }); // clash lookup hits
|
|
const res = createRes();
|
|
|
|
await handler(
|
|
{ method: 'POST', body: { name: 'Molten', palette: GOOD_PALETTE } },
|
|
res
|
|
);
|
|
|
|
expect(res.statusCode).toBe(409);
|
|
});
|
|
|
|
it('creates a frame with the normalized palette and default layout', async () => {
|
|
sql
|
|
.mockResolvedValueOnce({ rows: [] }) // clash lookup misses
|
|
.mockResolvedValueOnce({
|
|
rows: [{ id: 4, name: 'Molten', palette: GOOD_PALETTE, layout: { art: { x: 16/420, y: 64/588, w: 388/420, h: 234/588 }, text: { x: 16/420, y: 342/588, w: 388/420, h: 224/588 } } }],
|
|
});
|
|
|
|
const res = createRes();
|
|
await handler(
|
|
{ method: 'POST', body: { name: 'Molten', palette: GOOD_PALETTE } },
|
|
res
|
|
);
|
|
|
|
expect(res.statusCode).toBe(201);
|
|
expect(res.body.frame.name).toBe('Molten');
|
|
expect(res.body.frame.layout).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('/api/custom-frames/[id]', () => {
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
vi.clearAllMocks();
|
|
getUserFromRequest.mockResolvedValue({ userId: 1, email: 'a@b.c', role: 'user' });
|
|
sql.mockResolvedValue({ rows: [] });
|
|
sql.json.mockImplementation((v) => v);
|
|
});
|
|
|
|
it('returns 404 when no frame exists', async () => {
|
|
sql.mockResolvedValueOnce({ rows: [] });
|
|
const res = createRes();
|
|
|
|
await itemHandler({ method: 'GET', query: { id: '999' } }, res);
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
});
|
|
|
|
it('updates name, palette, and layout', async () => {
|
|
sql.mockResolvedValueOnce({ rows: [{ id: 4, name: 'Molten' }] }); // found frame
|
|
sql.mockResolvedValueOnce({ rows: [] }); // clash check
|
|
sql.mockResolvedValueOnce({ rows: [{ id: 4, name: 'Molten II' }] }); // UPDATE RETURNING
|
|
const res = createRes();
|
|
await itemHandler(
|
|
{ method: 'PUT', query: { id: '4' }, body: { name: 'Molten II', palette: GOOD_PALETTE, layout: { art: { x: 0.02, y: 0.05, w: 0.96, h: 0.30 }, text: { x: 0.02, y: 0.50, w: 0.96, h: 0.35 } } } },
|
|
res
|
|
);
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.body.frame.name).toBe('Molten II');
|
|
});
|
|
|
|
it('deletes the frame', async () => {
|
|
sql
|
|
.mockResolvedValueOnce({ rows: [{ id: 4 }] }) // ownership
|
|
.mockResolvedValueOnce({ rows: [] }); // DELETE
|
|
const res = createRes();
|
|
await itemHandler({ method: 'DELETE', query: { id: '4' } }, res);
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(sql).toHaveBeenCalledTimes(2);
|
|
});
|
|
});
|