Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159import { test } from 'node:test';import assert from 'node:assert/strict';import { createHash } from 'node:crypto';import { crc32c, crc32cHeader } from '../lib/cloud/crc32c.js';import { parseIndex, docIndex, rootIndex, sha256 } from '../lib/cloud/index-file.js';import { pageIds, pageIndexValue, notebookContent, newMetadata, updateMetadata, safeFileName, uploadKind } from '../lib/cloud/documents.js';import { decodeJwt, tokenExpired, describeToken, hosts } from '../lib/cloud/auth.js';
test('crc32c matches the reference vectors', () => { assert.equal(crc32c(Buffer.from('123456789')), 0xE3069283); assert.equal(crc32c(Buffer.alloc(0)), 0); assert.equal(crc32c(Buffer.alloc(32, 0)), 0x8A9136AA); assert.equal(crc32cHeader(Buffer.from('123456789')), 'crc32c=4waSgw==');});
const H = (s) => createHash('sha256').update(s).digest('hex');
test('parses schema 3 and schema 4 indexes', () => { const v3 = `3\n${H('a')}:80000000:doc-a:5:136848\n${H('b')}:80000000:doc-b:4:122252\n`; const p3 = parseIndex(v3); assert.equal(p3.schema, '3'); assert.deepEqual(p3.entries, [ { hash: H('a'), type: '80000000', name: 'doc-a', subfiles: 5, size: 136848 }, { hash: H('b'), type: '80000000', name: 'doc-b', subfiles: 4, size: 122252 }, ]);
const v4 = `4\n0:.:2:10\n${H('x')}:0:doc-x:2:4\n${H('y')}:0:doc-y:3:6\n`; const p4 = parseIndex(v4); assert.equal(p4.schema, '4'); assert.equal(p4.entries.length, 2); assert.equal(p4.entries[1].name, 'doc-y');
const withColon = `3\n${H('c')}:0:id.weird:name:0:12\n`; assert.equal(parseIndex(withColon).entries[0].name, 'id.weird:name');
assert.throws(() => parseIndex(''), /empty/); assert.throws(() => parseIndex('5\n'), /unsupported/); assert.throws(() => parseIndex('3\nnot-a-line\n'), /malformed/);});
test('document index: sorted by name, hash over binary file hashes', () => { const files = [ { name: 'id.metadata', hash: H('meta'), size: 3 }, { name: 'id.content', hash: H('content'), size: 5 }, { name: 'id/page.rm', hash: H('page'), size: 7 }, ]; const idx = docIndex(files); assert.equal(idx.text, [ '3', `${H('content')}:0:id.content:0:5`, `${H('meta')}:0:id.metadata:0:3`, `${H('page')}:0:id/page.rm:0:7`, '', ].join('\n')); const expected = createHash('sha256') .update(Buffer.from(H('content'), 'hex')) .update(Buffer.from(H('meta'), 'hex')) .update(Buffer.from(H('page'), 'hex')) .digest('hex'); assert.equal(idx.hash, expected); assert.equal(idx.size, 15); assert.equal(idx.subfiles, 3); // round-trips through the parser assert.equal(parseIndex(idx.text).entries.length, 3);});
test('root index: schema 4 with header, sorted by id, hashed as text', () => { const docs = [ { id: 'bbbb', hash: H('b'), subfiles: 2, size: 20 }, { id: 'aaaa', hash: H('a'), subfiles: 3, size: 10 }, ]; const root = rootIndex(docs); assert.equal(root.text, `4\n0:.:2:30\n${H('a')}:0:aaaa:3:10\n${H('b')}:0:bbbb:2:20\n`); assert.equal(root.hash, sha256(root.text)); assert.equal(rootIndex([]).text, '4\n0:.:0:0\n'); assert.deepEqual(parseIndex(root.text).entries.map(e => e.name), ['aaaa', 'bbbb']);});
test('page order follows cPages idx values and skips deleted pages', () => { const content = { cPages: { pages: [ { id: 'p3', idx: { value: 'bc' } }, { id: 'p1', idx: { value: 'ba' } }, { id: 'gone', idx: { value: 'bb' }, deleted: { value: 1 } }, { id: 'p2', idx: { value: 'bb' } }, ], }, }; assert.deepEqual(pageIds(content), ['p1', 'p2', 'p3']); assert.deepEqual(pageIds({ pages: ['x', 'y'] }), ['x', 'y']); assert.deepEqual(pageIds({}), []); assert.equal(pageIndexValue(0), 'ba'); assert.equal(pageIndexValue(25), 'bz'); assert.equal(pageIndexValue(26), 'ca'); const nb = notebookContent({ pageIds: ['a', 'b', 'c'], authorUuids: ['u1'], sizeInBytes: 42 }); assert.deepEqual(pageIds(nb), ['a', 'b', 'c']); assert.equal(nb.fileType, 'notebook'); assert.equal(nb.formatVersion, 2); assert.equal(nb.pageCount, 3); assert.equal(nb.sizeInBytes, '42'); assert.deepEqual(nb.cPages.uuids, [{ first: 'u1', second: 1 }]);});
test('metadata builders', () => { const meta = newMetadata({ name: 'Notes', parent: 'folder-1' }); assert.equal(meta.visibleName, 'Notes'); assert.equal(meta.type, 'DocumentType'); assert.equal(meta.parent, 'folder-1'); assert.match(meta.lastModified, /^\d{13}$/); const updated = updateMetadata({ ...meta, version: 3 }, { parent: 'trash' }); assert.equal(updated.parent, 'trash'); assert.equal(updated.version, 4); assert.equal(updated.metadatamodified, true); assert.equal(updated.visibleName, 'Notes');});
test('file-name helpers', () => { assert.equal(safeFileName('Meeting: notes / 2026?'), 'Meeting_ notes _ 2026_'); assert.equal(safeFileName(' '), 'untitled'); assert.equal(safeFileName('..hidden'), '_hidden'); assert.equal(uploadKind('a.PDF'), 'pdf'); assert.equal(uploadKind('a.epub'), 'epub'); assert.equal(uploadKind('x/y.rm'), 'rm'); assert.equal(uploadKind('notes.md'), 'text'); assert.equal(uploadKind('notes.docx'), null);});
test('jwt helpers and host selection', () => { const b64 = (o) => Buffer.from(JSON.stringify(o)).toString('base64url'); const future = Math.floor(Date.now() / 1000) + 3600; const past = Math.floor(Date.now() / 1000) - 10; const live = `h.${b64({ exp: future, 'auth0-profile': { Email: 'a@b.c' }, scopes: 'sync:fox docedit' })}.s`; const dead = `h.${b64({ exp: past })}.s`; assert.equal(decodeJwt(live).exp, future); assert.equal(decodeJwt('garbage'), null); assert.equal(tokenExpired(live), false); assert.equal(tokenExpired(dead), true); assert.equal(tokenExpired(undefined), true); assert.equal(tokenExpired('h.e30.s'), false); // no exp claim: assume valid const d = describeToken(live); assert.equal(d.email, 'a@b.c'); assert.deepEqual(d.scopes, ['sync:fox', 'docedit']);
const saved = { ...process.env }; try { delete process.env.NOTABLE_CLOUD_HOST; delete process.env.NOTABLE_AUTH_HOST; delete process.env.NOTABLE_SYNC_HOST; assert.equal(hosts({}).syncHost, 'https://internal.cloud.remarkable.com'); assert.equal(hosts({ host: 'http://localhost:3000/' }).authHost, 'http://localhost:3000'); process.env.NOTABLE_CLOUD_HOST = 'http://cloud.test'; assert.equal(hosts({}).syncHost, 'http://cloud.test'); assert.equal(hosts({ syncHost: 'http://other' }).syncHost, 'http://other'); } finally { process.env = saved; }});