Back to Black Site
SOURCE / PINNED RELEASE

Made of little things.

Black Site

Release
666bc7c74ebd…
Author-recorded commit
05e95af79676…
License
LICENSE
Author’s source reference
nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-d2225ddc391

Archive hash verified: 1544e072e245c4a8…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

tests/current-user.test.mjs
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { runInNewContext } from 'node:vm';
import test from 'node:test';
import ts from 'typescript';

const compiled = ts.transpileModule(readFileSync(new URL('../src/current-user.ts', import.meta.url), 'utf8'), {
  compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
}).outputText;
const alice = 'a'.repeat(64), bob = 'b'.repeat(64);
const tick = () => new Promise(resolve => setImmediate(resolve));
function harness({ domains = ['identity', 'common'], read = async () => alice, profile = async key => ({ ok: true, pubkey: key, profile: { display_name: 'Alice', picture: 'https://example.com/alice.png' } }), snapshot = async () => ({ name: 'Snapshot' }), subscribeFails = false } = {}) {
  let handler, detached = false;
  const states = [];
  const module = { exports: {} };
  const identity = {
    getPublicKey: read,
    getProfile: snapshot,
    onChanged(callback) {
      if (subscribeFails) throw new Error('unsupported');
      handler = callback;
      return { close() { detached = true; } };
    },
  };
  runInNewContext(compiled, {
    module, exports: module.exports, setTimeout, clearTimeout,
    require(name) {
      return name === '@napplet/sdk' ? { identity, common: { getProfile: profile } }
        : { runtimeHasDomain: domain => domains.includes(domain) };
    },
  });
  const controller = module.exports.watchCurrentUser(state => states.push(state));
  return { controller, states, change: key => handler(key), detached: () => detached };
}

test('renders the connected profile; logout clears it and closes subscription', async () => {
  const h = harness();
  await tick();
  assert.equal(h.states.at(-1).name, 'Alice');
  assert.equal(h.states.at(-1).pubkey, alice);
  assert.equal(h.states.at(-1).picture, 'https://example.com/alice.png');
  h.change('');
  assert.match(h.states.at(-1).status, /Playing as guest/);
  assert.equal(h.states.at(-1).name, '');
  assert.equal(h.states.at(-1).picture, '');
  h.controller.close();
  assert.equal(h.detached(), true);
});

test('old profile and initial identity reads cannot overwrite an account change', async () => {
  let resolveRead, resolveProfile;
  const h = harness({ read: () => new Promise(resolve => { resolveRead = resolve; }), profile: key => key === alice
    ? new Promise(resolve => { resolveProfile = resolve; })
    : Promise.resolve({ ok: true, pubkey: bob, profile: { name: 'Bob' } }) });
  h.change(alice);
  await tick();
  h.change(bob);
  resolveRead(alice);
  resolveProfile({ ok: true, pubkey: alice, profile: { name: 'Alice' } });
  await tick();
  assert.equal(h.states.at(-1).name, 'Bob');
  h.change(alice);
  await tick();
  h.controller.close();
  const count = h.states.length;
  resolveProfile({ ok: true, pubkey: alice, profile: { name: 'Late' } });
  await tick();
  assert.equal(h.states.length, count);
});

test('missing optional domains, signed out, synchronous failures and retry remain usable', async () => {
  for (const options of [
    { domains: [] },
    { read: async () => '' },
    { read: () => { throw new Error('read denied'); } },
    { profile: () => { throw new Error('profile denied'); } },
    { profile: async () => ({ ok: false, pubkey: alice, error: 'denied' }) },
    { profile: async () => ({ ok: true, pubkey: bob, profile: { name: 'Wrong account' } }) },
  ]) {
    const h = harness(options);
    await tick();
    assert.match(h.states.at(-1).status, /unavailable|Playing as guest|Could not load/);
    assert.notEqual(h.states.at(-1).name, 'Wrong account');
    await h.controller.refresh();
    await tick();
    h.controller.close();
  }
  const h = harness({ domains: ['identity'], subscribeFails: true });
  await tick();
  assert.equal(h.states.at(-1).name, 'Snapshot');
  h.controller.close();
});