Extracted from the private caelum0x/scribase monorepo's packages/scribase-js with a fresh, single-commit history (no core history leaks). The npx scribase CLI and the typed /v1 API client, unchanged except: prepublishOnly simplified (the monorepo-only tarball gitleaks gate dropped), repository/homepage/bugs fields added, and one test that cross-checked embedded template strings against the monorepo's templates/ directory removed (nothing it tested changed; that directory isn't shipped here).
80 lines
3.5 KiB
TypeScript
80 lines
3.5 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { exportFirestoreDocuments, exportInstantApp, InstantExportError } from '../src/importers/index.js';
|
|
|
|
function jsonResponse(body: unknown, status = 200): Response {
|
|
return new Response(JSON.stringify(body), { status, headers: { 'content-type': 'application/json' } });
|
|
}
|
|
|
|
test('exportInstantApp queries each namespace and flattens links to ids', async () => {
|
|
const calls: { url: string; body: unknown; headers: Record<string, string> }[] = [];
|
|
const fakeFetch = (async (url: string, init: RequestInit) => {
|
|
const body = JSON.parse(String(init.body)) as { query: Record<string, unknown> };
|
|
calls.push({ url, body, headers: init.headers as Record<string, string> });
|
|
if ('todos' in body.query) {
|
|
return jsonResponse({ todos: [{ id: 't1', title: 'ship', owner: [{ id: 'u1', email: 'a@b.co' }] }] });
|
|
}
|
|
if ('$users' in body.query) {
|
|
return jsonResponse({ $users: [{ id: 'u1', email: 'a@b.co' }] });
|
|
}
|
|
return jsonResponse({ $files: [{ id: 'f1', path: 'a.png', url: 'https://x/a.png' }, { id: 'f2' }] });
|
|
}) as typeof fetch;
|
|
|
|
const document = await exportInstantApp({
|
|
appId: 'app-1',
|
|
adminToken: 'secret',
|
|
namespaces: ['todos'],
|
|
links: { todos: ['owner'] },
|
|
fetch: fakeFetch,
|
|
});
|
|
assert.deepEqual(document.namespaces.todos, [{ id: 't1', title: 'ship', owner: ['u1'] }]);
|
|
assert.equal(document.users.length, 1);
|
|
assert.deepEqual(document.files, [{ id: 'f1', path: 'a.png', url: 'https://x/a.png' }]);
|
|
assert.equal(calls[0]?.headers['app-id'], 'app-1');
|
|
assert.equal(calls[0]?.headers.authorization, 'Bearer secret');
|
|
assert.match(calls[0]?.url ?? '', /\/admin\/query\?app_id=app-1$/);
|
|
});
|
|
|
|
test('exportInstantApp refuses $-namespaces and reports auth failures without the token', async () => {
|
|
await assert.rejects(
|
|
exportInstantApp({ appId: 'a', adminToken: 't', namespaces: ['$users'], fetch: fetch }),
|
|
InstantExportError,
|
|
);
|
|
const denied = (async () => jsonResponse({}, 401)) as unknown as typeof fetch;
|
|
await assert.rejects(
|
|
exportInstantApp({ appId: 'a', adminToken: 'super-secret', namespaces: ['todos'], fetch: denied }),
|
|
(error: unknown) => error instanceof InstantExportError && !error.message.includes('super-secret'),
|
|
);
|
|
});
|
|
|
|
test('exportFirestoreDocuments walks collections and subcollections, skipping missing parents', async () => {
|
|
const root = 'projects/p/databases/(default)/documents';
|
|
const fakeFetch = (async (url: string) => {
|
|
const path = decodeURIComponent(new URL(url).pathname.replace('/v1/', ''));
|
|
if (path === `${root}:listCollectionIds`) {
|
|
return jsonResponse({ collectionIds: ['users'] });
|
|
}
|
|
if (path === `${root}/users`) {
|
|
return jsonResponse({
|
|
documents: [
|
|
{ name: `${root}/users/u1`, fields: { a: { stringValue: 'x' } }, createTime: 't', updateTime: 't' },
|
|
{ name: `${root}/users/ghost` },
|
|
],
|
|
});
|
|
}
|
|
if (path === `${root}/users/ghost:listCollectionIds`) {
|
|
return jsonResponse({ collectionIds: ['orders'] });
|
|
}
|
|
if (path === `${root}/users/ghost/orders`) {
|
|
return jsonResponse({ documents: [{ name: `${root}/users/ghost/orders/o1`, createTime: 't' }] });
|
|
}
|
|
return jsonResponse({});
|
|
}) as typeof fetch;
|
|
|
|
const names: string[] = [];
|
|
for await (const document of exportFirestoreDocuments({ projectId: 'p', accessToken: 'tok', fetch: fakeFetch })) {
|
|
names.push(document.name);
|
|
}
|
|
assert.deepEqual(names.sort(), [`${root}/users/ghost/orders/o1`, `${root}/users/u1`]);
|
|
});
|