Back to Napcraft
SOURCE / PINNED RELEASE

Made of little things.

Napcraft

Release
ee47ac6a25df…
Author-recorded commit
410dea87e109…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-52f9e22f5ce

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

tests/world-directory.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import {readFileSync} from 'node:fs';
import {webcrypto} from 'node:crypto';
import path from 'node:path';
import {createContext,runInContext} from 'node:vm';
import ts from 'typescript';
const cache=new Map();
const context=createContext({atob,btoa,TextEncoder,crypto:webcrypto,structuredClone,setTimeout,clearTimeout,Date});
function load(file){
  file=path.resolve(file);if(cache.has(file))return cache.get(file);
  const exports={};cache.set(file,exports);
  const code=ts.transpileModule(readFileSync(file,'utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText;
  runInContext(`(function(exports,require){${code}\n})`,context)(exports,name=>name==='@napplet/sdk'?{}:load(path.resolve(path.dirname(file),name.replace(/\.js$/,'')+'.ts')));return exports;
}
const {validateListing,listingId,distinctWorlds}=load('src/world-directory.ts');
const {appDataCollection}=load('docs/examples/app-data.ts');
const {dataTags}=load('docs/examples/app-data-contract.ts');
const scope='d'.repeat(64),author='a'.repeat(64),release='b'.repeat(64);
const code=(r=release,id='world-one')=>'NC1-'+btoa(JSON.stringify([r,id])).replaceAll('+','-').replaceAll('/','_').replace(/=+$/,'');
const event=(id,{time=1,deleted=false,data={code:code()},revision='e'.repeat(64)}={})=>({kind:30078,id:revision,pubkey:author,sig:'f'.repeat(128),created_at:time,tags:dataTags(scope,'worlds',id,'napcraft.world-directory',1),content:JSON.stringify({schema:'napcraft.world-directory',version:1,title:'A shared island',previous:deleted?'1'.repeat(64):null,deleted,data:deleted?null:data})});
function host(events=[]){return {shell:{onReady(fn){fn({capabilities:{appData:{profile:'soy.app-data/1',scope,maxContentBytes:16384,relays:['wss://test.invalid'],defaultRelays:['wss://test.invalid']}}});return {close(){}}}},identity:{async getPublicKey(){return author}},outbox:{async query(){return {events:events.map(event=>({event}))}},async publish(template){return {ok:true,event:{...template,id:'e'.repeat(64),pubkey:author,sig:'f'.repeat(128)}}}}};}
const collection=h=>appDataCollection({collection:'worlds',schema:'napcraft.world-directory',version:1,validate:validateListing,host:h});
test('listing IDs are stable across retries and distinguish pinned worlds',async()=>{
  assert.equal(validateListing({code:code()}).code,code());
  assert.equal(await listingId(code()),await listingId(code()));
  assert.notEqual(await listingId(code()),await listingId(code('c'.repeat(64))));
});
test('directory rejects external URLs, malformed codes and unexpected data',()=>{
  for(const value of [{code:'https://example.com'}, {code:code(),secret:'extra'}, {code:code('invalid')},{code:code(release,'bad/path')},{code:'NC1-'+btoa('[1,2,3]')},null])assert.throws(()=>validateListing(value));
});
test('duplicate invitations collapse while retaining the viewer own listing',()=>{
  const a={data:{code:code()},author,createdAt:1,revision:'a'},b={...a,author:'b',createdAt:2,revision:'b'};
  assert.equal(distinctWorlds([a,b]).length,1);assert.equal(distinctWorlds([a,b])[0].author,'b');assert.equal(distinctWorlds([a,b],author)[0].author,author);
});
test('anonymous discovery never resurrects older invalid or removed listings',async()=>{
  const h=host([event('bad'),event('bad',{time:2,data:{code:'invalid'}}),event('removed'),event('removed',{time:2,deleted:true}),event('good')]);
  h.identity.getPublicKey=()=>{throw Error('Reading must not require sign-in')};
  const result=await(await collection(h)).list();assert.equal(result.records.length,1);assert.equal(result.records[0].id,'good');assert.equal(result.invalid,1);
});
test('uncertain listing retry keeps its template and identity changes block replay',async()=>{
  const h=host(),calls=[];let fail=true;
  h.outbox.publish=async template=>{calls.push(JSON.stringify(template));if(fail){fail=false;return {ok:false,error:'Reply lost'}}return {ok:true,event:{...template,id:'e'.repeat(64),pubkey:author,sig:'f'.repeat(128)}}};
  const change=await(await collection(h)).prepare({id:await listingId(code()),title:'Island',data:{code:code()},base:null});
  await assert.rejects(change.publish(),/Reply lost/);await change.publish();assert.equal(calls[0],calls[1]);
  h.identity.getPublicKey=async()=>'b'.repeat(64);await assert.rejects(change.publish(),/identity-changed/);assert.equal(calls.length,2);
});