Test edition release against a real database
Release / build (push) Successful in 1m2s

Covers queueing on publish, the breaking bypass, releasing the latest due
edition once with the most important stories, and sweeping queued
stories after switching back to Live.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
TMCandClaude Opus 5.5 committed 2026-11-15 11:00:24 +01:00
1 parent 1cc99df9bb
commit f68cd8f96b
1 file changed
+77
+77
View File
@@ -0,0 +1,77 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { loadConfig } from '../config.ts';
import { openDatabase } from '../db/database.ts';
import { migrate } from '../db/schema.ts';
import { SettingsStore } from '../settings.ts';
import { LlmClient } from '../llm/client.ts';
import { setTimezone, localToUtc, localDay } from '../lib/time.ts';
import type { Ctx } from '../context.ts';
import { decideFeedOnPublish, releaseDueEdition, sweepQueuedInLiveMode } from './feedcontrol.ts';
function setup(): { ctx: Ctx; dir: string } {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rssfeed-ed-'));
const config = loadConfig({ RSSFEED_DATA_DIR: dir, TZ: 'Europe/Zagreb' });
setTimezone(config.timezone);
const db = openDatabase(config.dbPath);
migrate(db);
const settings = new SettingsStore(db, config);
const ctx: Ctx = { config, db, settings, llm: new LlmClient(db, () => settings.get().llm), wake: () => {} };
return { ctx, dir };
}
function addStory(ctx: Ctx, sourceId: number, importance: number, title: string): number {
const now = new Date().toISOString();
const a = ctx.db
.prepare(`INSERT INTO article (source_id, url, url_hash, title_hash, title, fetched_at, state, state_changed_at) VALUES (?,?,?,?,?,?,?,?)`)
.run(sourceId, `https://x/${title}`, title, title, title, now, 'grouped', now).lastInsertRowid;
const s = ctx.db
.prepare(
`INSERT INTO story (guid, lead_article_id, current_version, title, description, category, model_importance, importance, first_reported_at, last_reported_at, first_published_at, state, created_at)
VALUES (?,?,1,?,?, 'Svijet', ?, ?, ?, ?, ?, 'published', ?)`,
)
.run(`g-${title}`, a, title, 'Opis.', importance, importance, now, now, now, now).lastInsertRowid;
ctx.db.prepare(`UPDATE article SET story_id = ? WHERE id = ?`).run(s, a);
return s;
}
test('editions: stories queue, the latest due edition releases the most important ones once', () => {
const { ctx, dir } = setup();
try {
const src = ctx.db.prepare(`INSERT INTO source (name, feed_url, created_at) VALUES ('S', 'https://s/feed', ?)`).run(new Date().toISOString()).lastInsertRowid;
ctx.settings.update({ feed: { mode: 'editions', editionTimes: ['07:00', '12:00'], editionSize: 2 } });
const ids = [addStory(ctx, src, 2, 'low'), addStory(ctx, src, 4, 'high'), addStory(ctx, src, 3, 'mid'), addStory(ctx, src, 5, 'breaking')];
for (const id of ids) decideFeedOnPublish(ctx, id);
const status = (id: number) => ctx.db.prepare(`SELECT feed_status, feed_reason, edition_id FROM story WHERE id = ?`).get<{ feed_status: string; feed_reason: string | null; edition_id: number | null }>(id)!;
assert.equal(status(ids[3]!).feed_status, 'in_feed');
assert.equal(status(ids[3]!).feed_reason, 'breaking');
assert.equal(status(ids[0]!).feed_status, 'queued');
const day = localDay();
const at = localToUtc(day, '12:30');
const r = releaseDueEdition(ctx, at);
assert.ok(r);
assert.equal(r.released, 2);
assert.equal(status(ids[1]!).feed_status, 'in_feed');
assert.equal(status(ids[2]!).feed_status, 'in_feed');
assert.equal(status(ids[1]!).edition_id, r.editionId);
assert.deepEqual([status(ids[0]!).feed_status, status(ids[0]!).feed_reason], ['left_out', 'edition_full']);
assert.equal(releaseDueEdition(ctx, at), null, 'the same edition is released only once');
const ed = ctx.db.prepare(`SELECT label FROM edition WHERE id = ?`).get<{ label: string }>(r.editionId)!;
assert.equal(ed.label, '12:00');
// Back to live: anything still queued is decided immediately.
const late = addStory(ctx, src, 3, 'late');
decideFeedOnPublish(ctx, late);
assert.equal(status(late).feed_status, 'queued');
ctx.settings.update({ feed: { mode: 'live' } });
sweepQueuedInLiveMode(ctx);
assert.equal(status(late).feed_status, 'in_feed');
} finally {
ctx.db.close();
fs.rmSync(dir, { recursive: true, force: true });
}
});