Files
events-hub/backend/tests/storage.test.js
T
joshuaandClaude Opus 5.5 43eb634199 Hub 2.22.0: Get more storage for the Church Website
POST /api/installs/storage-request (contracts 1.18.0): recorded on the
install, Joshua emailed once, closed when the licence gets more extra
storage steps. The church is emailed once when its website's storage
passes 80% and once when it's full (from the heartbeat). Vendors
contracts 1.18.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 15:41:31 +02:00

76 lines
4.0 KiB
JavaScript

// A Church Website's storage (Hub 2.22.0): "Get more storage" requests and the 80% / full emails.
jest.mock('../src/config/db', () => require('./fakeDb').createFakeDb());
const { createFakeDb } = require('./fakeDb');
const storage = require('../src/utils/storage');
const GB = 1024 ** 3;
function setup() {
const db = createFakeDb();
const mails = [];
const sendMail = async m => { mails.push(m); };
const install = {
id: 'inst-1', name: 'Hope Website', product: 'website', url: 'https://www.hope.test',
adminContact: { name: 'Sam', email: 'sam@hope.test' }, customer: { name: 'Hope Family Church', email: 'office@hope.test' },
};
return { db, mails, sendMail, install };
}
describe('Get more storage', () => {
test('records the request and emails Joshua once; asking again only changes it', async () => {
const { db, mails, sendMail, install } = setup();
const body = { protocolVersion: 2, steps: 1, usedBytes: 4.8 * GB, limitBytes: 5 * GB, requestedBy: 'Sam' };
const first = await storage.storageRequested(db, install, body, { alertEmail: 'joshua@hub.test', sendMail });
expect(first.alreadyRequested).toBe(false);
expect(mails).toHaveLength(1);
expect(mails[0].to).toBe('joshua@hub.test');
expect(mails[0].subject).toMatch(/Hope Family Church would like more website storage/);
const again = await storage.storageRequested(db, install, { ...body, steps: 2 }, { alertEmail: 'joshua@hub.test', sendMail });
expect(again).toMatchObject({ alreadyRequested: true, steps: 2 });
expect(mails).toHaveLength(1);
const events = await db.installEvent.findMany({ where: { installId: 'inst-1', type: 'storage_requested' } });
expect(events).toHaveLength(1);
expect(events[0].message).toMatch(/10 GB more/);
});
test('is done when the licence gets more steps', async () => {
const { db, sendMail, install } = setup();
await storage.storageRequested(db, install, { protocolVersion: 2, steps: 1, usedBytes: 1, limitBytes: 5 * GB, requestedBy: null }, { alertEmail: null, sendMail });
expect(await storage.storageAdded(db, 'inst-1')).toBe(1);
expect(await db.installEvent.count({ where: { installId: 'inst-1', type: 'storage_requested', resolvedAt: null } })).toBe(0);
});
});
describe('80% and full', () => {
const at = share => ({ storage: { usedBytes: Math.round(share * 5 * GB), includedBytes: 5 * GB } });
test('emails the church once at 80% and once when full, to its two addresses', async () => {
const { db, mails, sendMail, install } = setup();
expect(await storage.storageLevels(db, install, at(0.5), { sendMail })).toEqual([]);
expect(await storage.storageLevels(db, install, at(0.85), { sendMail })).toEqual(['storage_80']);
expect(mails.map(m => m.to).sort()).toEqual(['office@hope.test', 'sam@hope.test']);
expect(mails[0].subject).toBe("Your website's storage is nearly full");
expect(await storage.storageLevels(db, install, at(0.9), { sendMail })).toEqual([]);
expect(await storage.storageLevels(db, install, at(1), { sendMail })).toEqual(['storage_full']);
expect(mails).toHaveLength(4);
expect(mails[3].subject).toBe("Your website's storage is full");
expect(mails[3].text).toMatch(/admin\/media/);
});
test('again only after it dropped back below', async () => {
const { db, mails, sendMail, install } = setup();
await storage.storageLevels(db, install, at(0.85), { sendMail });
await storage.storageLevels(db, install, at(0.4), { sendMail });
expect(await db.installEvent.count({ where: { installId: 'inst-1', type: 'storage_80', resolvedAt: null } })).toBe(0);
await storage.storageLevels(db, install, at(0.85), { sendMail });
expect(mails).toHaveLength(4);
});
test('leaves other products and odd stats alone', async () => {
const { db, sendMail, install } = setup();
expect(await storage.storageLevels(db, { ...install, product: 'events' }, at(1), { sendMail })).toBeNull();
expect(await storage.storageLevels(db, install, { storage: { usedBytes: 5, includedBytes: 0 } }, { sendMail })).toBeNull();
});
});