Files
hyprcrd/public/js/test/api.test.js
T
2026-04-09 22:18:45 +02:00

129 lines
5.3 KiB
JavaScript

import { describe, it, expect, csrf } from './runner.js';
describe('API: Cards CRUD', () => {
it('GET /api/cards returns array', async () => {
const data = await fetch('api/cards').then(r => r.json());
expect(Array.isArray(data)).toBe(true);
});
it('GET /api/cards/meta returns array', async () => {
const data = await fetch('api/cards/meta').then(r => r.json());
expect(Array.isArray(data)).toBe(true);
});
it('POST creates, PATCH updates, DELETE removes', async () => {
// Create
const card = await fetch('api/cards', {
method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': csrf() },
body: JSON.stringify({ title: '__api_crud__', body: 'test', color: '#38bdf8' }),
}).then(r => r.json());
expect(card.id).toBeGreaterThan(0);
// PATCH returns the updated card object
const updated = await fetch(`api/cards/${card.id}`, {
method: 'PATCH', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': csrf() },
body: JSON.stringify({ title: '__api_updated__' }),
}).then(r => r.json());
expect(updated.title).toBe('__api_updated__');
// DELETE returns {ok: true}
const deleted = await fetch(`api/cards/${card.id}`, {
method: 'DELETE', headers: { 'X-CSRF-Token': csrf() },
}).then(r => r.json());
expect(deleted.ok).toBe(true);
});
it('tags extracted from body', async () => {
const card = await fetch('api/cards', {
method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': csrf() },
body: JSON.stringify({ title: '__api_tags__', body: '#alpha text #beta/child', color: '#38bdf8' }),
}).then(r => r.json());
const all = await fetch(`api/cards?ids=${card.id}`).then(r => r.json());
const c = all.find(x => x.id == card.id);
expect(c.tags).toContain('alpha');
expect(c.tags).toContain('beta/child');
});
it('due_date persists via PATCH', async () => {
const card = await fetch('api/cards', {
method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': csrf() },
body: JSON.stringify({ title: '__api_due__', body: '', color: '#38bdf8' }),
}).then(r => r.json());
await fetch(`api/cards/${card.id}`, {
method: 'PATCH', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': csrf() },
body: JSON.stringify({ due_date: '2099-12-31' }),
});
const all = await fetch(`api/cards?ids=${card.id}`).then(r => r.json());
expect(all.find(x => x.id == card.id).due_date).toBe('2099-12-31');
});
it('bucket_id assignment', async () => {
const bucket = await fetch('api/buckets', {
method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': csrf() },
body: JSON.stringify({ name: '__api_bkt__' }),
}).then(r => r.json());
const card = await fetch('api/cards', {
method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': csrf() },
body: JSON.stringify({ title: '__api_inbucket__', body: '', color: '#38bdf8', bucket_id: bucket.id }),
}).then(r => r.json());
const all = await fetch(`api/cards?ids=${card.id}`).then(r => r.json());
expect(all.find(x => x.id == card.id).bucket_id).toBe(bucket.id);
});
});
describe('API: Buckets CRUD', () => {
it('GET /api/buckets returns array', async () => {
const data = await fetch('api/buckets').then(r => r.json());
expect(Array.isArray(data)).toBe(true);
});
it('POST creates, PATCH renames, DELETE removes', async () => {
// Create
const bucket = await fetch('api/buckets', {
method: 'POST', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': csrf() },
body: JSON.stringify({ name: '__api_bkt_crud__' }),
}).then(r => r.json());
expect(bucket.id).toBeGreaterThan(0);
// PATCH returns updated bucket object
const renamed = await fetch(`api/buckets/${bucket.id}`, {
method: 'PATCH', headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': csrf() },
body: JSON.stringify({ name: '__api_bkt_renamed__' }),
}).then(r => r.json());
expect(renamed.name).toBe('__api_bkt_renamed__');
// DELETE returns {ok: true}
const deleted = await fetch(`api/buckets/${bucket.id}`, {
method: 'DELETE', headers: { 'X-CSRF-Token': csrf() },
}).then(r => r.json());
expect(deleted.ok).toBe(true);
});
});
describe('API: Auth & Misc', () => {
it('auth status ok', async () => {
const data = await fetch('api/auth/status').then(r => r.json());
expect(data.logged_in).toBe(true);
});
it('relay settings reachable', async () => {
const data = await fetch('api/relay/settings').then(r => r.json());
expect(data).toExist();
});
it('caldav settings reachable', async () => {
const data = await fetch('api/caldav/settings').then(r => r.json());
expect(data).toExist();
});
it('unknown endpoint returns 404', async () => {
const res = await fetch('api/nonexistent');
expect(res.status).toBe(404);
});
});