contracts 1.24.0: devotionals two years ahead; Apps fetch 2 weeks

CROSSCODE_DEVOTIONALS_SCHEDULE_MONTHS (24) for how far ahead the Hub
schedules Cross Code's devotionals, CROSSCODE_DEVOTIONALS_FETCH_DAYS (14)
for what GET /api/v2/devotionals answers. The CSV takes 800 rows and
8 MB, so two years fit in one file. CROSSCODE_DEVOTIONALS_AHEAD_MONTHS
kept, deprecated. Docs: Hub 2.26.1, hub-protocol.md §3c, the App plan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-30 23:23:18 +02:00
co-authored by Claude Opus 5.5
parent 986d69d51d
commit 9cf2956aff
9 changed files with 77 additions and 29 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@crosscode/contracts",
"version": "1.23.0",
"version": "1.24.0",
"description": "The shared JSON formats used by every Cross Code product: zod schemas, JSON Schema, TypeScript types and sample payloads",
"license": "UNLICENSED",
"type": "commonjs",
+27 -10
View File
@@ -12,7 +12,8 @@ import {
* wrote it in the App, so both show and send alike.
*
* Cross Code's reach an App set to "From Cross Code" from the Hub: `GET /api/v2/devotionals`
* (`hub.DEVOTIONALS_PATH`), once a night, the next 3 months (`devotionalsReplySchema`).
* (`hub.DEVOTIONALS_PATH`), once a night, the next 2 weeks at most (1.24.0, `CROSSCODE_DEVOTIONALS_FETCH_DAYS`;
* `devotionalsReplySchema`).
*
* Both write them by hand or import a CSV (`DEVOTIONAL_CSV_COLUMNS`, `checkDevotionalCsv`), with
* the same checks in the Hub and the App.
@@ -25,9 +26,21 @@ export const DEVOTIONAL_SOURCES = ["crosscode", "church"] as const;
export const devotionalSourceSchema = z.enum(DEVOTIONAL_SOURCES);
export type DevotionalSource = z.infer<typeof devotionalSourceSchema>;
/** How far ahead the Hub schedules Cross Code's devotionals, and how far ahead an App fetches them. */
/**
* 1.24.0: the most days one `GET /api/v2/devotionals` answer covers, `from` included (two weeks):
* an App fetches them each night, so it always has the days ahead even if the Hub is down for a while.
*/
export const CROSSCODE_DEVOTIONALS_FETCH_DAYS = 14;
/**
* @deprecated 1.24.0: the Hub schedules `CROSSCODE_DEVOTIONALS_SCHEDULE_MONTHS` ahead and an App
* fetches `CROSSCODE_DEVOTIONALS_FETCH_DAYS`. Kept so nothing that read it breaks.
*/
export const CROSSCODE_DEVOTIONALS_AHEAD_MONTHS = 3;
/** 1.24.0: how far ahead the Hub schedules Cross Code's devotionals (two years; was 3 months). */
export const CROSSCODE_DEVOTIONALS_SCHEDULE_MONTHS = 24;
export const DEVOTIONAL_LIMITS = { title: 200, reference: 100, passageText: 10_000, body: 20_000, prayer: 5_000, author: 200 } as const;
/**
@@ -66,14 +79,15 @@ export type Devotional = z.infer<typeof devotionalSchema>;
/**
* The Hub's answer to `GET /api/v2/devotionals?from=YYYY-MM-DD`: Cross Code's devotionals from
* `from` to `to` (3 months on), one per day at most, oldest first. The App replaces its copy of
* `from` to `to` (1.24.0: at most `CROSSCODE_DEVOTIONALS_FETCH_DAYS` days, `from` included; 3 months
* before), one per day at most, oldest first. The App replaces its copy of
* that range with them (a day missing from the list was removed in the Hub).
*/
export const devotionalsReplySchema = z.object({
schema: z.literal(DEVOTIONAL_SCHEMA),
from: dateSchema,
to: dateSchema,
devotionals: z.array(devotionalSchema.extend({ source: z.literal("crosscode") })).max(100),
devotionals: z.array(devotionalSchema.extend({ source: z.literal("crosscode") })).max(100), // 14 from 1.24.0; 100 kept for older Hubs
generatedAt: dateTimeSchema,
});
export type DevotionalsReply = z.infer<typeof devotionalsReplySchema>;
@@ -114,9 +128,12 @@ export const DEVOTIONAL_CSV_COLUMNS = ["date", "title", "passage", "translation"
export type DevotionalCsvColumn = (typeof DEVOTIONAL_CSV_COLUMNS)[number];
const REQUIRED_COLUMNS: readonly DevotionalCsvColumn[] = ["date", "title", "passage", "translation", "passage_text", "body"];
/** Rows per import (a year and a bit), and the file size the Hub and App accept. */
export const DEVOTIONAL_CSV_MAX_ROWS = 400;
export const DEVOTIONAL_CSV_MAX_BYTES = 2 * 1024 * 1024;
/**
* Rows per import and the file size the Hub and App accept. 1.24.0: two years and a bit (800 rows,
* 8 MB; was 400 and 2 MB), so the Hub's two years ahead fit in one file.
*/
export const DEVOTIONAL_CSV_MAX_ROWS = 800;
export const DEVOTIONAL_CSV_MAX_BYTES = 8 * 1024 * 1024;
/**
* Reads CSV (RFC 4180: quoted fields, `""` for a quote, line breaks inside quotes), with or
@@ -180,7 +197,7 @@ export interface DevotionalCsvResult {
export interface DevotionalCsvOptions {
/** Dates that already have a devotional. */
taken?: Iterable<string>;
/** The last date allowed (the Hub: 3 months from today). Later dates are errors. */
/** The last date allowed (the Hub: two years from today). Later dates are errors. */
latestDate?: string;
/** The translations allowed (the Hub and App 1.0: `FREE_BIBLE_TRANSLATION_IDS`). Any by default. */
translations?: readonly BibleTranslationId[];
@@ -192,7 +209,7 @@ export interface DevotionalCsvOptions {
* twice in the file, a date already filled). Never throws.
*/
export function checkDevotionalCsv(text: string, options: DevotionalCsvOptions = {}): DevotionalCsvResult {
if (text.length > DEVOTIONAL_CSV_MAX_BYTES) return { fileError: "The file is larger than 2 MB", rows: [] };
if (text.length > DEVOTIONAL_CSV_MAX_BYTES) return { fileError: `The file is larger than ${DEVOTIONAL_CSV_MAX_BYTES / 1024 / 1024} MB`, rows: [] };
const all = parseCsv(text);
const header = all[0];
if (!header) return { fileError: "The file is empty", rows: [] };
@@ -228,7 +245,7 @@ export function checkDevotionalCsv(text: string, options: DevotionalCsvOptions =
const date = required("date", "The date");
if (date && !dateSchema.safeParse(date).success) errors.push({ column: "date", message: `"${date}" isn't a date (use YYYY-MM-DD, e.g. 2027-01-05)` });
else if (date && options.latestDate && date > options.latestDate) errors.push({ column: "date", message: `${date} is more than ${CROSSCODE_DEVOTIONALS_AHEAD_MONTHS} months ahead (the last day is ${options.latestDate})` });
else if (date && options.latestDate && date > options.latestDate) errors.push({ column: "date", message: `${date} is too far ahead (the last day is ${options.latestDate})` });
else if (date && seen.has(date)) errors.push({ column: "date", message: `${date} is also on line ${seen.get(date)}` });
if (date && !seen.has(date)) seen.set(date, line);
+2 -2
View File
@@ -327,8 +327,8 @@ export type StorageRequestResponse = z.infer<typeof storageRequestResponseSchema
// ---------------------------------------------------------------------------------------------
// Cross Code's devotionals (contracts 1.20.0, Hub 2.26.0, APP-1.0-PLAN §6): an App set to "From
// Cross Code" fetches the next 3 months once a night (and on "Sync now"), and keeps its copy, so a
// Hub outage changes nothing. `GET /api/v2/devotionals?from=YYYY-MM-DD` with
// Cross Code" fetches the next 2 weeks once a night (1.24.0, `CROSSCODE_DEVOTIONALS_FETCH_DAYS`; 3 months
// before) and on "Sync now", and keeps its copy, so a Hub outage of under 2 weeks changes nothing. `GET /api/v2/devotionals?from=YYYY-MM-DD` with
// `Authorization: Bearer <the install's licence token>`; App licences only (`403 NOT_AN_APP`),
// active or in grace (`403 LICENCE_INACTIVE`). The answer is a `devotionalsReplySchema`.
// ---------------------------------------------------------------------------------------------
+12 -1
View File
@@ -2,6 +2,7 @@ import {
BIBLE_BOOKS, BIBLE_TRANSLATIONS, DEVOTIONAL_CSV_COLUMNS, FREE_BIBLE_TRANSLATION_IDS, FREE_QUOTE_VERSES, DEVOTIONAL_CSV_TEMPLATE, checkDevotionalCsv, devotionalCsv,
devotionalDisclaimer, devotionalsReplySchema, findBibleBook, quotedVerses, formatBibleReference, parseBibleReference, parseCsv, passageVerseCount, youVersionUrl,
type PassageLocation,
CROSSCODE_DEVOTIONALS_FETCH_DAYS, CROSSCODE_DEVOTIONALS_SCHEDULE_MONTHS, DEVOTIONAL_CSV_MAX_ROWS,
} from "../src";
import { sample } from "./helpers";
@@ -202,7 +203,17 @@ describe("checkDevotionalCsv", () => {
expect(r.rows[0]).toMatchObject({ taken: true, errors: [] });
expect(r.rows[1]!.warnings[0]!.message).toContain("40 verses");
expect(r.rows[1]!.devotional).not.toBeNull();
expect(r.rows[2]!.errors[0]!.message).toContain("more than 3 months ahead");
expect(r.rows[2]!.errors[0]!.message).toContain("too far ahead (the last day is 2027-03-31)");
});
it("1.24.0: two years ahead in the Hub, in one file; Apps fetch 2 weeks", () => {
expect([CROSSCODE_DEVOTIONALS_SCHEDULE_MONTHS, CROSSCODE_DEVOTIONALS_FETCH_DAYS]).toEqual([24, 14]);
const start = Date.UTC(2027, 0, 1);
const rows = Array.from({ length: 731 }, (_, i) => row({ date: new Date(start + i * 864e5).toISOString().slice(0, 10), translation: "KJV" }));
const r = checkDevotionalCsv(devotionalCsv(rows), { latestDate: "2028-12-31" });
expect(DEVOTIONAL_CSV_MAX_ROWS).toBeGreaterThanOrEqual(731);
expect(r.fileError).toBeNull();
expect(r.rows.filter((x) => x.devotional)).toHaveLength(731);
});
it("refuses a file without the required columns, or without rows", () => {