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>
239 lines
12 KiB
TypeScript
239 lines
12 KiB
TypeScript
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";
|
||
|
||
const ok = (input: string): PassageLocation => {
|
||
const r = parseBibleReference(input);
|
||
if (!r.ok) throw new Error(`${input}: ${r.error}`);
|
||
return r.passage;
|
||
};
|
||
|
||
describe("Bible books and translations", () => {
|
||
it("has the 66 books with unique codes", () => {
|
||
expect(BIBLE_BOOKS).toHaveLength(66);
|
||
expect(new Set(BIBLE_BOOKS.map((b) => b.code)).size).toBe(66);
|
||
});
|
||
|
||
it("finds every book by its English and Afrikaans name and its code", () => {
|
||
for (const b of BIBLE_BOOKS) {
|
||
expect(findBibleBook(b.en)?.code).toBe(b.code);
|
||
expect(findBibleBook(b.af)?.code).toBe(b.code);
|
||
expect(findBibleBook(b.code)?.code).toBe(b.code);
|
||
}
|
||
});
|
||
|
||
it("ignores case, accents, dots and spaces", () => {
|
||
expect(findBibleBook("1 korintiers")?.code).toBe("1CO");
|
||
expect(findBibleBook("1Kor.")?.code).toBe("1CO");
|
||
expect(findBibleBook("ESEGIËL")?.code).toBe("EZK");
|
||
expect(findBibleBook("Ps")?.code).toBe("PSA");
|
||
expect(findBibleBook("Nowhere")).toBeNull();
|
||
});
|
||
|
||
it("has the eight translations, with unique ids and YouVersion numbers", () => {
|
||
expect(BIBLE_TRANSLATIONS.map((t) => t.id)).toEqual(["NLT", "ESV", "NIV", "NKJV", "KJV", "AFR53", "AFR83", "BSB"]);
|
||
expect(new Set(BIBLE_TRANSLATIONS.map((t) => t.youVersionId)).size).toBe(8);
|
||
for (const t of BIBLE_TRANSLATIONS) expect(t.copyright.length).toBeGreaterThan(10);
|
||
});
|
||
|
||
it("carries each publisher's notices, with a publisher to ask for all but the public-domain ones", () => {
|
||
expect(FREE_BIBLE_TRANSLATION_IDS).toEqual(["KJV", "BSB"]);
|
||
for (const t of BIBLE_TRANSLATIONS) {
|
||
const free = FREE_BIBLE_TRANSLATION_IDS.includes(t.id);
|
||
expect(t.licence).toBe(free ? "public-domain" : "permission");
|
||
expect(t.acknowledgement.length).toBeGreaterThan(t.id === "KJV" ? 10 : 50);
|
||
expect(t.publisher === null).toBe(free);
|
||
expect(t.permissionsUrl === null).toBe(free);
|
||
}
|
||
expect(youVersionUrl(ok("John 3:16"), "BSB")).toBe("https://www.bible.com/bible/3034/JHN.3.16.BSB");
|
||
expect(BIBLE_TRANSLATIONS.find((t) => t.id === "NLT")!.copyright).toContain("Tyndale House Ministries");
|
||
expect(BIBLE_TRANSLATIONS.find((t) => t.id === "ESV")!.copyright).toContain("ESV Text Edition: 2025");
|
||
expect(FREE_QUOTE_VERSES).toBe(500);
|
||
});
|
||
|
||
it("gives the Bible Society's translations their disclaimer and official names", () => {
|
||
for (const t of BIBLE_TRANSLATIONS) {
|
||
expect(t.disclaimer !== null).toBe(t.language === "af");
|
||
expect(t.acknowledgement).not.toMatch(/Ou Vertaling|Nuwe Vertaling/);
|
||
}
|
||
expect(devotionalDisclaimer({ translation: "AFR83" })).toContain("Bybelgenootskap van Suid-Afrika verklaar");
|
||
expect(devotionalDisclaimer({ translation: "NIV" })).toBeNull();
|
||
expect(BIBLE_TRANSLATIONS.find((t) => t.id === "AFR83")!.copyright).toContain("1983-vertaling met herformulerings tot en met 1992");
|
||
});
|
||
|
||
it("counts the verses quoted per translation, passages across chapters apart", () => {
|
||
const r = quotedVerses([
|
||
{ location: ok("John 3:16-18"), translation: "NIV" },
|
||
{ location: ok("Ps 23:1-6"), translation: "NIV" },
|
||
{ location: ok("Rom 8:38-9:2"), translation: "NIV" },
|
||
{ location: ok("Gen 1:1"), translation: "KJV" },
|
||
]);
|
||
expect(r.verses).toEqual({ NIV: 9, KJV: 1 });
|
||
expect(r.uncounted).toEqual({ NIV: 1 });
|
||
});
|
||
});
|
||
|
||
describe("parseBibleReference", () => {
|
||
it.each([
|
||
["John 3:16", { book: "JHN", chapter: 3, verse: 16, endChapter: 3, endVerse: 16 }],
|
||
["John 3:16-18", { book: "JHN", chapter: 3, verse: 16, endChapter: 3, endVerse: 18 }],
|
||
["Johannes 3:16–18", { book: "JHN", chapter: 3, verse: 16, endChapter: 3, endVerse: 18 }],
|
||
["1 Kor. 13:4 - 7", { book: "1CO", chapter: 13, verse: 4, endChapter: 13, endVerse: 7 }],
|
||
["Rom 8:38-9:1", { book: "ROM", chapter: 8, verse: 38, endChapter: 9, endVerse: 1 }],
|
||
["Ps 23 1-3", { book: "PSA", chapter: 23, verse: 1, endChapter: 23, endVerse: 3 }],
|
||
["Psalm 23.1", { book: "PSA", chapter: 23, verse: 1, endChapter: 23, endVerse: 1 }],
|
||
["Jude 3", { book: "JUD", chapter: 1, verse: 3, endChapter: 1, endVerse: 3 }],
|
||
["Judas 3-5", { book: "JUD", chapter: 1, verse: 3, endChapter: 1, endVerse: 5 }],
|
||
["Jude 1:3", { book: "JUD", chapter: 1, verse: 3, endChapter: 1, endVerse: 3 }],
|
||
["Song of Songs 2:4", { book: "SNG", chapter: 2, verse: 4, endChapter: 2, endVerse: 4 }],
|
||
])("reads %s", (input, expected) => {
|
||
expect(ok(input)).toEqual(expected);
|
||
});
|
||
|
||
it.each([
|
||
["", "isn't a Bible reference"],
|
||
["John", "isn't a Bible reference"],
|
||
["John 3", "Give the verses"],
|
||
["John 316", "Give the verses"],
|
||
["Hezekiah 3:1", "isn't a book"],
|
||
["John 22:1", "John has 21 chapters"],
|
||
["John 3:18-16", "ends before it starts"],
|
||
["Rom 9:1-8:38", "ends before it starts"],
|
||
])("refuses %p", (input, message) => {
|
||
const r = parseBibleReference(input);
|
||
expect(r.ok).toBe(false);
|
||
if (!r.ok) expect(r.error).toContain(message);
|
||
});
|
||
|
||
it("formats in the translation's language", () => {
|
||
expect(formatBibleReference(ok("john 3:16-18"))).toBe("John 3:16–18");
|
||
expect(formatBibleReference(ok("John 3:16-18"), "af")).toBe("Johannes 3:16–18");
|
||
expect(formatBibleReference(ok("Rom 8:38-9:1"))).toBe("Romans 8:38–9:1");
|
||
expect(formatBibleReference(ok("Jude 3"))).toBe("Jude 1:3");
|
||
});
|
||
|
||
it("counts verses within a chapter only", () => {
|
||
expect(passageVerseCount(ok("1 Cor 13:4-7"))).toBe(4);
|
||
expect(passageVerseCount(ok("Rom 8:38-9:1"))).toBeNull();
|
||
});
|
||
|
||
it("builds the YouVersion link", () => {
|
||
expect(youVersionUrl(ok("John 3:16-18"), "NLT")).toBe("https://www.bible.com/bible/116/JHN.3.16-18.NLT");
|
||
expect(youVersionUrl(ok("Johannes 3:16"), "AFR83")).toBe("https://www.bible.com/bible/6/JHN.3.16.AFR83");
|
||
expect(youVersionUrl(ok("Rom 8:38-9:1"), "ESV")).toBe("https://www.bible.com/bible/59/ROM.8.38.ESV");
|
||
});
|
||
});
|
||
|
||
describe("parseCsv", () => {
|
||
it("reads quotes, doubled quotes, line breaks in quotes, CRLF and a BOM", () => {
|
||
const rows = parseCsv(String.fromCharCode(0xfeff) + 'a,b\r\n"x, y","say ""hi"""\r\n"two\nlines",z\r\n\r\nlast,row');
|
||
expect(rows).toEqual([
|
||
{ line: 1, cells: ["a", "b"] },
|
||
{ line: 2, cells: ["x, y", 'say "hi"'] },
|
||
{ line: 3, cells: ["two\nlines", "z"] },
|
||
{ line: 6, cells: ["last", "row"] },
|
||
]);
|
||
});
|
||
|
||
it("takes semicolons when the header has them and no commas", () => {
|
||
expect(parseCsv("a;b\n1,5;2\n")).toEqual([{ line: 1, cells: ["a", "b"] }, { line: 2, cells: ["1,5", "2"] }]);
|
||
});
|
||
});
|
||
|
||
describe("checkDevotionalCsv", () => {
|
||
const row = (over: Partial<Record<(typeof DEVOTIONAL_CSV_COLUMNS)[number], string>> = {}) => ({
|
||
date: "2027-01-05", title: "Love", passage: "1 Cor 13:4-7", translation: "NLT", passage_text: "Love is patient and kind.",
|
||
body: "Text.", prayer: "", author: "", ...over,
|
||
});
|
||
|
||
it("accepts the template", () => {
|
||
const r = checkDevotionalCsv(DEVOTIONAL_CSV_TEMPLATE);
|
||
expect(r.fileError).toBeNull();
|
||
expect(r.rows).toHaveLength(1);
|
||
expect(r.rows[0]!.errors).toEqual([]);
|
||
expect(r.rows[0]!.devotional?.passage).toMatchObject({ reference: "1 Corinthians 13:4–7", translation: "BSB" });
|
||
expect(checkDevotionalCsv(DEVOTIONAL_CSV_TEMPLATE, { translations: FREE_BIBLE_TRANSLATION_IDS }).rows[0]!.errors).toEqual([]);
|
||
});
|
||
|
||
it("refuses a translation that isn't allowed, naming the publisher to ask", () => {
|
||
const r = checkDevotionalCsv(devotionalCsv([row({ translation: "NIV" }), row({ date: "2027-01-06", translation: "kjv" })]), { translations: FREE_BIBLE_TRANSLATION_IDS });
|
||
expect(r.rows[0]!.devotional).toBeNull();
|
||
expect(r.rows[0]!.errors).toEqual([{ column: "translation", message: "NIV needs Biblica, Inc.'s permission; use KJV or BSB" }]);
|
||
expect(r.rows[1]!.devotional?.passage.translation).toBe("KJV");
|
||
});
|
||
|
||
it("turns a good row into a devotional, in the translation's language, empty optional fields null", () => {
|
||
const r = checkDevotionalCsv(devotionalCsv([row({ passage: "Joh 3:16", translation: "afr83" })]));
|
||
expect(r.rows[0]!.devotional).toEqual({
|
||
date: "2027-01-05", title: "Love",
|
||
passage: { reference: "Johannes 3:16", location: { book: "JHN", chapter: 3, verse: 16, endChapter: 3, endVerse: 16 }, translation: "AFR83", text: "Love is patient and kind." },
|
||
body: "Text.", prayer: null, author: null,
|
||
});
|
||
});
|
||
|
||
it("reports each bad field with its column and line", () => {
|
||
const r = checkDevotionalCsv(devotionalCsv([
|
||
row({ date: "5 Jan 2027" }),
|
||
row({ date: "2027-01-06", title: "" }),
|
||
row({ date: "2027-01-07", passage: "Hezekiah 1:1" }),
|
||
row({ date: "2027-01-08", translation: "MSG" }),
|
||
row({ date: "2027-01-09", body: "x".repeat(20_001) }),
|
||
row({ date: "2027-01-06" }),
|
||
]));
|
||
const errors = r.rows.map((x) => ({ line: x.line, columns: x.errors.map((e) => e.column), ok: x.devotional !== null }));
|
||
expect(errors).toEqual([
|
||
{ line: 2, columns: ["date"], ok: false },
|
||
{ line: 3, columns: ["title"], ok: false },
|
||
{ line: 4, columns: ["passage"], ok: false },
|
||
{ line: 5, columns: ["translation"], ok: false },
|
||
{ line: 6, columns: ["body"], ok: false },
|
||
{ line: 7, columns: ["date"], ok: false },
|
||
]);
|
||
expect(r.rows[5]!.errors[0]!.message).toContain("also on line 3");
|
||
});
|
||
|
||
it("marks taken days, refuses days past the limit and warns on long passages", () => {
|
||
const r = checkDevotionalCsv(devotionalCsv([row(), row({ date: "2027-01-06", passage: "Ps 119:1-40" }), row({ date: "2027-05-01" })]), {
|
||
taken: ["2027-01-05"], latestDate: "2027-03-31",
|
||
});
|
||
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("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", () => {
|
||
expect(checkDevotionalCsv("").fileError).toBe("The file is empty");
|
||
expect(checkDevotionalCsv("date,title\n2027-01-05,Love\n").fileError).toContain("passage, translation, passage_text, body");
|
||
expect(checkDevotionalCsv(`${DEVOTIONAL_CSV_COLUMNS.join(",")}\n`).fileError).toContain("no devotionals");
|
||
});
|
||
|
||
it("takes columns in any order, and without the optional ones", () => {
|
||
const r = checkDevotionalCsv("body,passage_text,translation,passage,title,date\nText.,Words.,KJV,Gen 1:1,In the beginning,2027-02-01\n");
|
||
expect(r.rows[0]!.devotional).toMatchObject({ date: "2027-02-01", prayer: null, passage: { reference: "Genesis 1:1", translation: "KJV" } });
|
||
});
|
||
});
|
||
|
||
describe("devotionals reply", () => {
|
||
it("only carries Cross Code's devotionals", () => {
|
||
const reply = sample<{ devotionals: { source: string }[] }>("devotionals-reply.json");
|
||
expect(devotionalsReplySchema.safeParse(reply).success).toBe(true);
|
||
reply.devotionals[0]!.source = "church";
|
||
expect(devotionalsReplySchema.safeParse(reply).success).toBe(false);
|
||
});
|
||
});
|