mirror of
https://github.com/mileswolfallen2/miles-web.git
synced 2026-12-05 00:00:41 +00:00
Add comprehensive test suite for application functionality
- Implement tests for HTTP API endpoints, ensuring correct responses and security headers. - Create configuration tests to validate loading of defaults and environment overrides. - Add tests for FEDL service to verify data handling and snapshot functionality. - Introduce GitHub service tests to check API data normalization and caching behavior. - Develop monitor service tests for network checks and uptime calculations. - Implement persistence tests to ensure database migrations are idempotent and data persists across restarts. - Add vault service tests for session management and brute-force protection mechanisms. - Include helper functions for test context creation and JSON response handling.
This commit is contained in:
1 parent
84b7a7e302
commit
c250653961
55 files changed
+8174
-226
No files matched your search
@@ -0,0 +1,13 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import Database from 'better-sqlite3';
|
||||
|
||||
export function openDatabase(dbPath, { readonly = false } = {}) {
|
||||
if (!readonly) fs.mkdirSync(path.dirname(dbPath), { recursive: true });
|
||||
const db = new Database(dbPath, { readonly, fileMustExist: readonly });
|
||||
db.pragma('journal_mode = WAL');
|
||||
db.pragma('synchronous = NORMAL');
|
||||
db.pragma('foreign_keys = ON');
|
||||
db.pragma('busy_timeout = 5000');
|
||||
return db;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
const HERE = path.dirname(fileURLToPath(import.meta.url));
|
||||
const MIGRATIONS_DIR = path.join(HERE, 'migrations');
|
||||
|
||||
export function migrate(db, { migrationsDir = MIGRATIONS_DIR } = {}) {
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
applied_at TEXT NOT NULL
|
||||
);
|
||||
`);
|
||||
|
||||
const applied = new Set(
|
||||
db.prepare('SELECT version FROM schema_migrations').all().map((row) => row.version)
|
||||
);
|
||||
|
||||
const files = fs
|
||||
.readdirSync(migrationsDir)
|
||||
.filter((name) => /^\d+_.+\.sql$/.test(name))
|
||||
.sort((a, b) => a.localeCompare(b));
|
||||
|
||||
const insert = db.prepare(
|
||||
'INSERT INTO schema_migrations (version, name, applied_at) VALUES (?, ?, ?)'
|
||||
);
|
||||
|
||||
let count = 0;
|
||||
for (const file of files) {
|
||||
const version = Number.parseInt(file.split('_')[0], 10);
|
||||
if (applied.has(version)) continue;
|
||||
const sql = fs.readFileSync(path.join(migrationsDir, file), 'utf8');
|
||||
const run = db.transaction(() => {
|
||||
db.exec(sql);
|
||||
insert.run(version, file, new Date().toISOString());
|
||||
});
|
||||
run();
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
CREATE TABLE servers (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
ip TEXT NOT NULL,
|
||||
display_order INTEGER NOT NULL DEFAULT 0,
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE server_checks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
server_id TEXT NOT NULL REFERENCES servers(id) ON DELETE CASCADE,
|
||||
checked_at TEXT NOT NULL,
|
||||
online INTEGER NOT NULL,
|
||||
response_ms REAL,
|
||||
reason TEXT NOT NULL DEFAULT 'ok',
|
||||
on_home_network INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE INDEX idx_server_checks_lookup ON server_checks (server_id, checked_at DESC);
|
||||
CREATE INDEX idx_server_checks_time ON server_checks (checked_at);
|
||||
|
||||
CREATE TABLE services (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
url TEXT NOT NULL,
|
||||
display_order INTEGER NOT NULL DEFAULT 0,
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE service_checks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
service_id TEXT NOT NULL REFERENCES services(id) ON DELETE CASCADE,
|
||||
checked_at TEXT NOT NULL,
|
||||
status TEXT NOT NULL CHECK (status IN ('operational', 'degraded', 'offline', 'unknown')),
|
||||
response_ms REAL,
|
||||
http_status INTEGER,
|
||||
error TEXT
|
||||
);
|
||||
CREATE INDEX idx_service_checks_lookup ON service_checks (service_id, checked_at DESC);
|
||||
CREATE INDEX idx_service_checks_time ON service_checks (checked_at);
|
||||
|
||||
CREATE TABLE projects (
|
||||
id TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
github_owner TEXT,
|
||||
github_repo TEXT,
|
||||
website_url TEXT,
|
||||
api_type TEXT,
|
||||
api_base TEXT,
|
||||
display_order INTEGER NOT NULL DEFAULT 0,
|
||||
visible INTEGER NOT NULL DEFAULT 1,
|
||||
featured INTEGER NOT NULL DEFAULT 0,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE cache (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL,
|
||||
etag TEXT,
|
||||
fetched_at TEXT NOT NULL,
|
||||
expires_at TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE fedl_snapshots (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
fetched_at TEXT NOT NULL,
|
||||
list_json TEXT NOT NULL,
|
||||
runs_json TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX idx_fedl_snapshots_time ON fedl_snapshots (fetched_at DESC);
|
||||
|
||||
CREATE TABLE fedl_stats (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
stats_json TEXT NOT NULL,
|
||||
fetched_at TEXT NOT NULL,
|
||||
snapshot_id INTEGER REFERENCES fedl_snapshots(id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE TABLE pageviews (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
path TEXT NOT NULL,
|
||||
visitor_hash TEXT NOT NULL,
|
||||
referrer_domain TEXT,
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX idx_pageviews_time ON pageviews (created_at DESC);
|
||||
CREATE INDEX idx_pageviews_path_time ON pageviews (path, created_at DESC);
|
||||
CREATE INDEX idx_pageviews_visitor_time ON pageviews (visitor_hash, created_at DESC);
|
||||
CREATE INDEX idx_pageviews_dedupe ON pageviews (visitor_hash, path, created_at DESC);
|
||||
|
||||
CREATE TABLE vault_sessions (
|
||||
id TEXT PRIMARY KEY,
|
||||
token_hash TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
expires_at TEXT NOT NULL,
|
||||
last_seen_at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX idx_vault_sessions_expiry ON vault_sessions (expires_at);
|
||||
|
||||
CREATE TABLE vault_attempts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
ip_hash TEXT NOT NULL,
|
||||
attempted_at TEXT NOT NULL,
|
||||
success INTEGER NOT NULL
|
||||
);
|
||||
CREATE INDEX idx_vault_attempts_lookup ON vault_attempts (ip_hash, attempted_at DESC);
|
||||
CREATE INDEX idx_vault_attempts_time ON vault_attempts (attempted_at);
|
||||
|
||||
CREATE TABLE meta (
|
||||
key TEXT PRIMARY KEY,
|
||||
value TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
@@ -0,0 +1,89 @@
|
||||
export function seedConfig(db, config, { now = new Date() } = {}) {
|
||||
const ts = now.toISOString();
|
||||
|
||||
const upsertServer = db.prepare(`
|
||||
INSERT INTO servers (id, name, ip, display_order, enabled, created_at, updated_at)
|
||||
VALUES (@id, @name, @ip, @order, @enabled, @ts, @ts)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
ip = excluded.ip,
|
||||
display_order = excluded.display_order,
|
||||
enabled = excluded.enabled,
|
||||
updated_at = excluded.updated_at
|
||||
`);
|
||||
|
||||
const upsertService = db.prepare(`
|
||||
INSERT INTO services (id, name, url, display_order, enabled, created_at, updated_at)
|
||||
VALUES (@id, @name, @url, @order, @enabled, @ts, @ts)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
url = excluded.url,
|
||||
display_order = excluded.display_order,
|
||||
enabled = excluded.enabled,
|
||||
updated_at = excluded.updated_at
|
||||
`);
|
||||
|
||||
const upsertProject = db.prepare(`
|
||||
INSERT INTO projects (
|
||||
id, name, description, github_owner, github_repo, website_url,
|
||||
api_type, api_base, display_order, visible, featured, updated_at
|
||||
)
|
||||
VALUES (
|
||||
@id, @name, @description, @githubOwner, @githubRepo, @website,
|
||||
@apiType, @apiBase, @order, @visible, @featured, @ts
|
||||
)
|
||||
ON CONFLICT(id) DO UPDATE SET
|
||||
name = excluded.name,
|
||||
description = excluded.description,
|
||||
github_owner = excluded.github_owner,
|
||||
github_repo = excluded.github_repo,
|
||||
website_url = excluded.website_url,
|
||||
api_type = excluded.api_type,
|
||||
api_base = excluded.api_base,
|
||||
display_order = excluded.display_order,
|
||||
visible = excluded.visible,
|
||||
featured = excluded.featured,
|
||||
updated_at = excluded.updated_at
|
||||
`);
|
||||
|
||||
db.transaction(() => {
|
||||
for (const server of config.servers) {
|
||||
upsertServer.run({
|
||||
id: server.id,
|
||||
name: server.name,
|
||||
ip: server.ip,
|
||||
order: server.order,
|
||||
enabled: server.enabled ? 1 : 0,
|
||||
ts
|
||||
});
|
||||
}
|
||||
|
||||
for (const service of config.services) {
|
||||
upsertService.run({
|
||||
id: service.id,
|
||||
name: service.name,
|
||||
url: service.url,
|
||||
order: service.order,
|
||||
enabled: service.enabled ? 1 : 0,
|
||||
ts
|
||||
});
|
||||
}
|
||||
|
||||
for (const project of config.projects) {
|
||||
upsertProject.run({
|
||||
id: project.id,
|
||||
name: project.name,
|
||||
description: project.description,
|
||||
githubOwner: project.github?.owner || null,
|
||||
githubRepo: project.github?.repo || null,
|
||||
website: project.website || null,
|
||||
apiType: project.api?.type || null,
|
||||
apiBase: project.api?.base || null,
|
||||
order: project.order,
|
||||
visible: project.visible ? 1 : 0,
|
||||
featured: project.featured ? 1 : 0,
|
||||
ts
|
||||
});
|
||||
}
|
||||
})();
|
||||
}
|
||||
Reference in new issue
Block a user