- Hub client: register and heartbeat on protocol v2 with the site's Ed25519 key (sent on the heartbeat until the Hub has it); falls back to v1 for a day if the Hub refuses v2, so the licence always refreshes. - crosscode.link/1 Connect / Disconnect, both directions, trusted through the Hub's cached linkedProducts (ProductLink, LinkNonce). - GET /api/feed/v1/events from FeedEntry (go-live, closing and selling out are changes; deactivated events go in deleted), event.changed to connected products, and showing their events (ImportedEvent). - /api/v2/linked-products, /api/v2/linked-events; /events/:id/full gains crosscodeEvent and priceRangeCents for the JSON-LD. - Contracts: FEED_PATHS, typesVersions for @crosscode/contracts/signing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
116 lines
5.2 KiB
JavaScript
116 lines
5.2 KiB
JavaScript
// An in-memory stand-in for the Prisma client, with just what the feed and Linked products use
|
|
// (src/feed, src/links, src/v2/resources/linkedProducts.ts). Where clauses are matched generically
|
|
// (AND, equality, in / notIn, gt / gte / lt / lte, not, nested relation filters on `link`).
|
|
|
|
const t = { appSetting: new Map(), productLink: new Map(), linkNonce: new Map(), importedEvent: new Map(), feedEntry: new Map(), event: new Map(), campus: [], user: [], audit: [] };
|
|
|
|
// Rows keep their Dates; JSON columns (payload) keep their strings, as Prisma gives them back.
|
|
const clone = (v) => (v === undefined ? v : structuredClone(v));
|
|
const val = (v) => (v instanceof Date ? v.getTime() : v);
|
|
|
|
function matchValue(actual, cond) {
|
|
if (cond !== null && typeof cond === 'object' && !(cond instanceof Date) && !Array.isArray(cond)) {
|
|
return Object.entries(cond).every(([op, x]) => {
|
|
switch (op) {
|
|
case 'in': return x.includes(actual);
|
|
case 'notIn': return !x.includes(actual);
|
|
case 'gt': return actual != null && val(actual) > val(x);
|
|
case 'gte': return actual != null && val(actual) >= val(x);
|
|
case 'lt': return actual != null && val(actual) < val(x);
|
|
case 'lte': return actual != null && val(actual) <= val(x);
|
|
case 'not': return x === null ? actual != null : !matchValue(actual, x);
|
|
default: throw new Error(`fakeFeedDb: no operator ${op}`);
|
|
}
|
|
});
|
|
}
|
|
return val(actual) === val(cond);
|
|
}
|
|
|
|
function matches(row, where = {}) {
|
|
return Object.entries(where || {}).every(([k, cond]) => {
|
|
if (k === 'AND') return cond.every((w) => matches(row, w));
|
|
if (k === 'link') return matches(t.productLink.get(row.linkId) || {}, cond.is || cond);
|
|
return matchValue(row[k], cond);
|
|
});
|
|
}
|
|
|
|
const pick = (row, select) => (select ? Object.fromEntries(Object.keys(select).filter((k) => select[k]).map((k) => [k, row[k]])) : row);
|
|
|
|
function sortBy(rows, orderBy) {
|
|
const list = Array.isArray(orderBy) ? orderBy : orderBy ? [orderBy] : [];
|
|
return [...rows].sort((a, b) => {
|
|
for (const o of list) {
|
|
const [k, dir] = Object.entries(o)[0];
|
|
const d = val(a[k]) < val(b[k]) ? -1 : val(a[k]) > val(b[k]) ? 1 : 0;
|
|
if (d) return dir === 'desc' ? -d : d;
|
|
}
|
|
return 0;
|
|
});
|
|
}
|
|
|
|
function table(name, idKey, { include } = {}) {
|
|
const m = t[name];
|
|
const withIncludes = (row, inc) => {
|
|
if (!inc) return row;
|
|
const out = { ...row };
|
|
if (inc._count) out._count = { importedEvents: [...t.importedEvent.values()].filter((e) => e.linkId === row.installId).length };
|
|
if (inc.link) out.link = { ...t.productLink.get(row.linkId) };
|
|
return out;
|
|
};
|
|
return {
|
|
findUnique: jest.fn(async ({ where, select }) => { const r = m.get(where[idKey]); return r ? pick(clone(r), select) : null; }),
|
|
findFirst: jest.fn(async ({ where, include: inc } = {}) => { const r = [...m.values()].find((x) => matches(x, where)); return r ? withIncludes(clone(r), inc) : null; }),
|
|
findMany: jest.fn(async ({ where, select, include: inc, orderBy, take } = {}) => sortBy([...m.values()].filter((x) => matches(x, where)), orderBy).slice(0, take ?? Infinity).map((r) => withIncludes(pick(clone(r), select), inc))),
|
|
create: jest.fn(async ({ data }) => {
|
|
if (m.has(data[idKey])) throw Object.assign(new Error('Unique constraint failed'), { code: 'P2002' });
|
|
m.set(data[idKey], clone(data));
|
|
return clone(data);
|
|
}),
|
|
upsert: jest.fn(async ({ where, create, update }) => {
|
|
const prev = m.get(where[idKey]);
|
|
const row = prev ? { ...prev, ...update } : { ...create };
|
|
m.set(where[idKey], clone(row));
|
|
return clone(row);
|
|
}),
|
|
update: jest.fn(async ({ where, data }) => {
|
|
const prev = m.get(where[idKey]);
|
|
if (!prev) throw Object.assign(new Error('Record to update not found'), { code: 'P2025' });
|
|
const row = { ...prev, ...data };
|
|
m.set(where[idKey], clone(row));
|
|
return clone(row);
|
|
}),
|
|
delete: jest.fn(async ({ where }) => {
|
|
const prev = m.get(where[idKey]);
|
|
if (!prev) throw Object.assign(new Error('Record to delete does not exist'), { code: 'P2025' });
|
|
m.delete(where[idKey]);
|
|
if (name === 'productLink') for (const [k, e] of t.importedEvent) if (e.linkId === where[idKey]) t.importedEvent.delete(k);
|
|
return prev;
|
|
}),
|
|
deleteMany: jest.fn(async ({ where } = {}) => {
|
|
let count = 0;
|
|
for (const [k, r] of m) if (matches(r, where)) { m.delete(k); count++; }
|
|
return { count };
|
|
}),
|
|
...include,
|
|
};
|
|
}
|
|
|
|
const db = {
|
|
t,
|
|
appSetting: table('appSetting', 'key'),
|
|
productLink: table('productLink', 'installId'),
|
|
linkNonce: table('linkNonce', 'nonce'),
|
|
importedEvent: table('importedEvent', 'key'),
|
|
feedEntry: table('feedEntry', 'eventId'),
|
|
event: table('event', 'id'),
|
|
campus: { findMany: jest.fn(async () => t.campus.map((c) => ({ ...c }))) },
|
|
user: { findMany: jest.fn(async ({ where }) => t.user.filter((u) => matches(u, where))) },
|
|
adminAuditLog: { create: jest.fn(async ({ data }) => { t.audit.push(data); return data; }) },
|
|
reset() {
|
|
for (const k of ['appSetting', 'productLink', 'linkNonce', 'importedEvent', 'feedEntry', 'event']) t[k].clear();
|
|
t.campus.length = 0; t.user.length = 0; t.audit.length = 0;
|
|
},
|
|
};
|
|
|
|
module.exports = db;
|