import { BIBLE_BOOKS, BIBLE_TRANSLATIONS, DEVOTIONAL_CSV_COLUMNS, DEVOTIONAL_CSV_TEMPLATE, checkDevotionalCsv, devotionalCsv, devotionalsReplySchema, findBibleBook, formatBibleReference, parseBibleReference, parseCsv, passageVerseCount, youVersionUrl, type PassageLocation, } 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 seven translations, with unique ids and YouVersion numbers", () => { expect(BIBLE_TRANSLATIONS.map((t) => t.id)).toEqual(["NLT", "ESV", "NIV", "NKJV", "KJV", "AFR53", "AFR83"]); expect(new Set(BIBLE_TRANSLATIONS.map((t) => t.youVersionId)).size).toBe(7); for (const t of BIBLE_TRANSLATIONS) expect(t.copyright.length).toBeGreaterThan(10); }); }); 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> = {}) => ({ 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: "NLT" }); }); 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("more than 3 months ahead"); }); 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); }); });