import test from 'node:test'; import assert from 'node:assert/strict'; import { readFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { dirname, resolve } from 'node:path'; import { avatarR2Url } from '../js/services/public-file-service.js'; import { buildAvatarReference, parseAvatarReference } from '../js/services/avatar-reference.js'; import { encodePublicPiecesRoot } from '../js/services/public-pieces-root.js'; import { preparePublicFile } from '../js/services/file-preparation-service.js'; const ui = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const source = (path) => readFileSync(resolve(ui, path), 'utf8'); const pr = encodePublicPiecesRoot(new Uint8Array(32).fill(255)); test('avatar v2: PR Base58 exactly as public attachment; Arweave TX ID unchanged', () => { assert.equal(avatarR2Url(pr), `https://files.shineup.me/${pr}`); const value = buildAvatarReference({ pr, size: 1000 }); assert.equal(value, ``); assert.equal(parseAvatarReference(value).pr, pr); assert.equal(parseAvatarReference(value).source, 'r2'); const ar = 'A'.repeat(43); const withAr = buildAvatarReference({ pr, size: 1000, ar }); assert.equal(parseAvatarReference(withAr).ar, ar); assert.equal(parseAvatarReference(withAr).source, 'arweave'); for (const legacy of [``, 'AR:' + ar]) { assert.equal(parseAvatarReference(legacy).ok, false); } assert.throws(() => buildAvatarReference({ pr: pr.toLowerCase(), size: 1000 })); assert.throws(() => avatarR2Url('not-a-root')); }); test('Avatar PR computed on the same exact bytes as public attachments', async () => { const file = new File([Uint8Array.of(1, 2, 3, 4, 5)], 'avatar.webp', { type: 'image/webp' }); const prepared = await preparePublicFile(file); assert.equal(prepared.pr, (await preparePublicFile(file)).pr); assert.equal(avatarR2Url(prepared.pr), `https://files.shineup.me/${prepared.pr}`); }); test('avatar R2 upload reuses signed public PR prepare; no avatar-specific endpoint/path', () => { const service = source('js/services/public-file-service.js'); assert.match(service, /return uploadR2PublicFile\(\{ file, login, sessionId, wsUrl, onStatus \}\)/); assert.match(service, /\/api\/files\/prepare/); assert.doesNotMatch(service, /\/api\/avatars\/prepare/); assert.doesNotMatch(service, /avatars\/\$\{/); assert.match(source('js/components/avatar-image.js'), /resolvePublicFileUrl\(\{ pr: expectedPr, authorLogin \}\)/); assert.match(source('js/pages/network/graph-canvas.js'), /resolvePublicFileUrl\(\{ pr, authorLogin \}\)/); assert.match(source('js/pages/network-legacy/force-graph.js'), /resolvePublicFileUrl\(\{ pr, authorLogin \}\)/); });