qr.test.js 1.6 KB

123456789101112131415161718192021222324252627282930313233
  1. const { eq, ok } = require('../../helpers/assert');
  2. const QRCode = require('../../../src/server/node_modules/qrcode');
  3. describe('profile: QR code generation', (t) => {
  4. t('QRCode.toDataURL produces a base64 PNG data URL for an SSB feedId', async () => {
  5. const feedId = '@AbCdEfGhIjKlMnOpQrStUvWxYz1234567890+/=.ed25519';
  6. const dataUrl = await QRCode.toDataURL(feedId, { type: 'image/png', width: 240, margin: 1 });
  7. ok(typeof dataUrl === 'string');
  8. ok(dataUrl.startsWith('data:image/png;base64,'), 'PNG data URL prefix');
  9. const b64 = dataUrl.slice('data:image/png;base64,'.length);
  10. ok(b64.length > 100, 'non-trivial payload');
  11. });
  12. t('QR data URLs are deterministic for the same input', async () => {
  13. const feedId = '@deterministicFeedId.ed25519';
  14. const a = await QRCode.toDataURL(feedId, { type: 'image/png', width: 240, margin: 1 });
  15. const b = await QRCode.toDataURL(feedId, { type: 'image/png', width: 240, margin: 1 });
  16. eq(a, b);
  17. });
  18. t('QR data URLs differ for different inputs', async () => {
  19. const a = await QRCode.toDataURL('@feed-A.ed25519', { type: 'image/png', width: 240, margin: 1 });
  20. const b = await QRCode.toDataURL('@feed-B.ed25519', { type: 'image/png', width: 240, margin: 1 });
  21. ok(a !== b);
  22. });
  23. t('toDataURL with empty string rejects (handled by authorView try/catch)', async () => {
  24. let threw = false;
  25. try { await QRCode.toDataURL('', { type: 'image/png', width: 240, margin: 1 }); } catch (_) { threw = true; }
  26. ok(threw, 'empty input rejects — authorView wraps the call in try/catch and falls back to no QR');
  27. });
  28. });