diff --git a/server/src/pipeline/editions.test.ts b/server/src/pipeline/editions.test.ts new file mode 100644 index 0000000..7a594c5 --- /dev/null +++ b/server/src/pipeline/editions.test.ts @@ -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 }); + } +});