epaper-display/display/middleware/tests/api/display-api.test.ts
Christian Mueller e760853781 refactor: migrate from node-sqlite3-wasm to better-sqlite3
Replace all db.run(sql, [params]) calls with db.prepare(sql).run(...params)
across all source modules and test files. Also remove the node-sqlite3-wasm
import from tests/db.test.ts and update the type annotation accordingly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-06 21:17:44 +02:00

83 lines
2.6 KiB
TypeScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import request from 'supertest';
import { initDb, closeDb } from '../../src/db/index';
import { createApp } from '../../src/api/index';
const app = createApp();
beforeAll(() => {
const db = initDb(':memory:');
// Create a layout
db.prepare(
`INSERT INTO layouts (id, name, type, width, height)
VALUES (1, 'Test Layout', 'free', 800, 480)`
).run();
// Display without token
db.prepare(
`INSERT INTO displays (id, name, layout_id, refresh_sec)
VALUES (1, 'Open Display', 1, 60)`
).run();
// Display with token auth
db.prepare(
`INSERT INTO displays (id, name, layout_id, api_token, refresh_sec)
VALUES (2, 'Secure Display', 1, 'secret-token-123', 300)`
).run();
});
afterAll(() => {
closeDb();
});
describe('GET /api/display/:id', () => {
it('returns display JSON for open display', async () => {
const res = await request(app).get('/api/display/1');
expect(res.status).toBe(200);
expect(res.body).toHaveProperty('display');
expect(res.body.display.id).toBe(1);
expect(res.body).toHaveProperty('layout');
expect(res.body).toHaveProperty('elements');
});
it('returns 404 for non-existent display', async () => {
const res = await request(app).get('/api/display/999');
expect(res.status).toBe(404);
});
it('returns 401 for token-protected display without token', async () => {
const res = await request(app).get('/api/display/2');
expect(res.status).toBe(401);
});
it('returns 401 for token-protected display with wrong token', async () => {
const res = await request(app)
.get('/api/display/2')
.set('Authorization', 'Bearer wrong-token');
expect(res.status).toBe(401);
});
it('returns display JSON with valid Bearer token', async () => {
const res = await request(app)
.get('/api/display/2')
.set('Authorization', 'Bearer secret-token-123');
expect(res.status).toBe(200);
expect(res.body.display.id).toBe(2);
});
it('returns display JSON with valid query token', async () => {
const res = await request(app).get('/api/display/2?token=secret-token-123');
expect(res.status).toBe(200);
expect(res.body.display.id).toBe(2);
});
it('updates last_seen on successful access', async () => {
await request(app).get('/api/display/1');
// last_seen is updated — verify by checking display 1 via another endpoint
const res = await request(app).get('/api/display/1');
expect(res.status).toBe(200);
// Just verify the request succeeds — last_seen updated in DB
});
});