The App installs from New install on app.<address>, with Accounts 1.9.0 first, and is in every product list and filter (installs, Updates). Every product has demo data; a demo client loads a whole demo scenario in one seed-scenario job (agent 1.7.0), and installs show whether they are out of step with it. The Website's and App's .env from the Hub. Devotionals: written or imported from CSV up to 3 months ahead in the free translations (KJV, BSB), and GET /api/v2/devotionals for App licences. An alert when an App's active-member band is above its tier. Contracts 1.23.0 vendored. Migration 20261130120000_app_scenarios_devotionals. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
152 lines
10 KiB
JavaScript
152 lines
10 KiB
JavaScript
// Hub 2.26.0 (crosscode-platform docs/APP-1.0-PLAN.md §6): Cross Code's devotionals. Written up to 3
|
||
// months ahead in the free translations only, imported from CSV with a preview, and fetched by App
|
||
// installs (GET /api/v2/devotionals with the licence token; App licences only). Also the App's
|
||
// tier alert (its active-member band above its tier).
|
||
const crypto = require('crypto');
|
||
|
||
jest.mock('../src/config/db', () => require('./fakeDb').createFakeDb());
|
||
jest.mock('express-rate-limit', () => () => (req, res, next) => next());
|
||
|
||
process.env.HUB_ENCRYPTION_KEY = 'test-encryption-key-that-is-long-enough-123';
|
||
const hubKey = crypto.generateKeyPairSync('ed25519');
|
||
process.env.HUB_SIGNING_KEY = hubKey.privateKey.export({ type: 'pkcs8', format: 'pem' }).toString();
|
||
|
||
const express = require('express');
|
||
const { DEVOTIONAL_CSV_TEMPLATE, devotionalCsv, devotionalsReplySchema } = require('@crosscode/contracts');
|
||
const db = require('../src/config/db');
|
||
const devotionals = require('../src/utils/devotionals');
|
||
const licenceToken = require('../src/utils/licenceToken');
|
||
const { problems } = require('../src/utils/alerts');
|
||
|
||
// 10:00 in South Africa on 30 November 2026.
|
||
const NOW = new Date('2026-11-30T08:00:00.000Z');
|
||
const admin = { id: 'admin-1' };
|
||
const form = (over = {}) => ({
|
||
date: '2026-12-05', title: 'Love that never gives up', reference: '1 Cor 13:4-7', translation: 'BSB',
|
||
passageText: 'Love is patient, love is kind.', body: 'Paul wrote these words…', prayer: 'Lord, teach me to love.', author: 'Cross Code', ...over,
|
||
});
|
||
|
||
beforeEach(() => { for (const k of Object.keys(db.tables)) db.tables[k].length = 0; });
|
||
|
||
describe('writing devotionals', () => {
|
||
test('a devotional: the passage written out, in a free translation, within 3 months', async () => {
|
||
const { devotional, warnings } = await devotionals.create(form(), admin, NOW);
|
||
expect(devotional).toMatchObject({ date: '2026-12-05', passage: { reference: '1 Corinthians 13:4–7', translation: 'BSB', text: 'Love is patient, love is kind.' }, prayer: 'Lord, teach me to love.' });
|
||
expect(warnings).toEqual([]);
|
||
await expect(devotionals.create(form({ title: 'Another' }), admin, NOW)).rejects.toMatchObject({ code: 'DATE_TAKEN' });
|
||
});
|
||
|
||
test('refused: a translation that needs permission, more than 3 months ahead, a reference that can\'t be read, an empty field', async () => {
|
||
expect(devotionals.latestDate(NOW)).toBe('2027-02-28');
|
||
await expect(devotionals.create(form({ translation: 'NIV' }), admin, NOW)).rejects.toMatchObject({ code: 'TRANSLATION_NOT_FREE', message: expect.stringContaining('KJV or BSB') });
|
||
await expect(devotionals.create(form({ date: '2027-03-01' }), admin, NOW)).rejects.toMatchObject({ code: 'TOO_FAR_AHEAD', message: expect.stringContaining('2027-02-28') });
|
||
await expect(devotionals.create(form({ reference: 'Hezekiah 1:1' }), admin, NOW)).rejects.toMatchObject({ code: 'BAD_REFERENCE' });
|
||
await expect(devotionals.create(form({ body: ' ' }), admin, NOW)).rejects.toMatchObject({ status: 400, message: expect.stringContaining('The devotional') });
|
||
expect(await db.devotional.findMany({})).toEqual([]);
|
||
});
|
||
|
||
test('a long passage is saved, with a warning', async () => {
|
||
const { warnings } = await devotionals.create(form({ reference: 'Psalm 119:1-40', translation: 'KJV' }), admin, NOW);
|
||
expect(warnings).toEqual([expect.stringContaining('40 verses')]);
|
||
});
|
||
|
||
test('editing, moving to a free day, deleting; the overview lists the empty days', async () => {
|
||
const { devotional } = await devotionals.create(form(), admin, NOW);
|
||
await devotionals.create(form({ date: '2026-12-06' }), admin, NOW);
|
||
await expect(devotionals.update(devotional.id, form({ date: '2026-12-06' }), NOW)).rejects.toMatchObject({ code: 'DATE_TAKEN' });
|
||
expect((await devotionals.update(devotional.id, form({ date: '2026-12-07', title: 'Moved' }), NOW)).devotional).toMatchObject({ date: '2026-12-07', title: 'Moved' });
|
||
const o = await devotionals.overview(NOW);
|
||
expect(o).toMatchObject({ today: '2026-11-30', latest: '2027-02-28', aheadMonths: 3 });
|
||
expect(o.devotionals.map(d => d.date)).toEqual(['2026-12-06', '2026-12-07']);
|
||
expect(o.empty).toContain('2026-12-05');
|
||
expect(o.empty).not.toContain('2026-12-06');
|
||
expect(o.translations.map(t => t.id)).toEqual(['KJV', 'BSB']);
|
||
expect(o.csv.template).toBe(DEVOTIONAL_CSV_TEMPLATE);
|
||
await devotionals.remove(devotional.id);
|
||
expect((await devotionals.list()).map(d => d.date)).toEqual(['2026-12-06']);
|
||
});
|
||
});
|
||
|
||
describe('Import CSV', () => {
|
||
const row = (over = {}) => ({ date: '2026-12-10', title: 'A title', passage: 'John 3:16', translation: 'KJV', passage_text: 'For God so loved the world…', body: 'Body', prayer: '', author: '', ...over });
|
||
|
||
test('the preview checks every row: a translation needing permission, too far ahead, a day taken', async () => {
|
||
await devotionals.create(form({ date: '2026-12-11' }), admin, NOW);
|
||
const csv = devotionalCsv([row(), row({ date: '2026-12-11' }), row({ date: '2026-12-12', translation: 'ESV' }), row({ date: '2027-06-01' })]);
|
||
const preview = await devotionals.previewCsv(csv, NOW);
|
||
expect(preview.fileError).toBeNull();
|
||
expect(preview.rows.map(r => [!!r.devotional, r.taken])).toEqual([[true, false], [true, true], [false, false], [false, false]]);
|
||
expect(preview.rows[2].errors[0].message).toContain('Crossway');
|
||
expect(preview.rows[3].errors[0].message).toContain('3 months');
|
||
});
|
||
|
||
test('importing saves the valid rows; days already filled are skipped or replaced', async () => {
|
||
await devotionals.create(form({ date: '2026-12-11', title: 'Old' }), admin, NOW);
|
||
const csv = devotionalCsv([row(), row({ date: '2026-12-11', title: 'New' }), row({ date: '2026-12-12', translation: 'NLT' })]);
|
||
expect(await devotionals.importCsv(csv, { replace: false }, admin, NOW)).toEqual({ created: 1, replaced: 0, skipped: 1, invalid: 1 });
|
||
expect((await db.devotional.findUnique({ where: { date: '2026-12-11' } })).title).toBe('Old');
|
||
expect(await devotionals.importCsv(csv, { replace: true }, admin, NOW)).toEqual({ created: 0, replaced: 2, skipped: 0, invalid: 1 });
|
||
expect((await db.devotional.findUnique({ where: { date: '2026-12-11' } })).title).toBe('New');
|
||
await expect(devotionals.importCsv('nothing,here\n', {}, admin, NOW)).rejects.toMatchObject({ code: 'BAD_CSV' });
|
||
});
|
||
});
|
||
|
||
describe('GET /api/v2/devotionals', () => {
|
||
let server;
|
||
let base;
|
||
beforeAll(async () => {
|
||
const app = express();
|
||
app.use(express.json());
|
||
app.use('/api', require('../src/routes/installApi'));
|
||
await new Promise(resolve => { server = app.listen(0, '127.0.0.1', resolve); });
|
||
base = `http://127.0.0.1:${server.address().port}/api`;
|
||
});
|
||
afterAll(() => new Promise(resolve => server.close(resolve)));
|
||
|
||
async function licence(product = 'app', data = {}) {
|
||
const l = await db.licence.create({ data: { id: crypto.randomUUID(), customerId: 'cust-1', product, key: `KEY-${product}`, status: 'active', plan: 'Mustard Seed', graceDays: 14, expiresAt: null, ...data } });
|
||
return licenceToken.sign(licenceToken.buildPayload(l, { id: 'inst-1' }, 'Hope'));
|
||
}
|
||
const get = (token, query = '') => fetch(`${base}/v2/devotionals${query}`, { headers: token ? { Authorization: `Bearer ${token}` } : {} });
|
||
|
||
test("an App's licence token gets the next 3 months from `from`, as crosscode.devotional/1", async () => {
|
||
const today = devotionals.today();
|
||
const later = devotionals.addMonths(today, 1);
|
||
await db.devotional.create({ data: { id: crypto.randomUUID(), date: later, title: 'Later', passage: { reference: 'John 3:16', location: { book: 'JHN', chapter: 3, verse: 16, endChapter: 3, endVerse: 16 }, translation: 'KJV', text: 'For God so loved the world' }, body: 'Body', prayer: null, author: null, updatedAt: new Date() } });
|
||
await db.devotional.create({ data: { id: crypto.randomUUID(), date: '2020-01-01', title: 'Old', passage: { reference: 'John 3:16', location: { book: 'JHN', chapter: 3, verse: 16, endChapter: 3, endVerse: 16 }, translation: 'KJV', text: 'x' }, body: 'Body', prayer: null, author: null, updatedAt: new Date() } });
|
||
const res = await get(await licence());
|
||
expect(res.status).toBe(200);
|
||
const body = await res.json();
|
||
expect(devotionalsReplySchema.safeParse(body).success).toBe(true);
|
||
expect(body).toMatchObject({ from: today, to: devotionals.addMonths(today, 3) });
|
||
expect(body.devotionals.map(d => [d.date, d.source])).toEqual([[later, 'crosscode']]);
|
||
});
|
||
|
||
test('refused: no token or a forged one, another product, an inactive licence, a bad date', async () => {
|
||
expect((await get(null)).status).toBe(401);
|
||
expect(await (await get('hel1.forged.token')).json()).toMatchObject({ code: 'BAD_LICENCE' });
|
||
const events = await get(await licence('events'));
|
||
expect([events.status, (await events.json()).code]).toEqual([403, 'NOT_AN_APP']);
|
||
const lapsed = await get(await licence('app', { expiresAt: new Date('2026-01-01'), graceDays: 14 }));
|
||
expect([lapsed.status, (await lapsed.json()).code]).toEqual([403, 'LICENCE_INACTIVE']);
|
||
for (const k of Object.keys(db.tables)) db.tables[k].length = 0;
|
||
const suspended = await get(await licence('app', { status: 'suspended' }));
|
||
expect((await suspended.json()).code).toBe('LICENCE_INACTIVE');
|
||
for (const k of Object.keys(db.tables)) db.tables[k].length = 0;
|
||
expect((await get(await licence(), '?from=tomorrow')).status).toBe(400);
|
||
});
|
||
});
|
||
|
||
describe("the App's tier (2.26.0)", () => {
|
||
const app = (plan, band) => ({ product: 'app', registeredAt: NOW, lastHeartbeatAt: NOW, licence: { status: 'active', plan, expiresAt: null }, stats: { activeMembers90d: band } });
|
||
|
||
test('an active-member band above its tier is flagged for the annual review; at or below it, or no band, nothing', () => {
|
||
const above = problems(app('Mustard Seed', '301–750'), {}, NOW).find(p => p.type === 'tier_above');
|
||
expect(above).toMatchObject({ level: 'info', message: expect.stringContaining('moves it to Congregation') });
|
||
expect(problems(app('Congregation', '301–750'), {}, NOW).find(p => p.type === 'tier_above')).toBeUndefined();
|
||
expect(problems(app('Gathering', '101–300'), {}, NOW).find(p => p.type === 'tier_above')).toBeUndefined();
|
||
expect(problems(app('Mustard Seed', null), {}, NOW).find(p => p.type === 'tier_above')).toBeUndefined();
|
||
expect(problems({ ...app('Mustard Seed', '301–750'), product: 'events' }, {}, NOW).find(p => p.type === 'tier_above')).toBeUndefined();
|
||
});
|
||
});
|