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.
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { runInNewContext } from 'node:vm';
import ts from 'typescript';
const source = await readFile(
new URL('../backend/handler.ts', import.meta.url),
'utf8',
);
const compiled = ts.transpileModule(source, {
compilerOptions: {
module: ts.ModuleKind.CommonJS,
target: ts.ScriptTarget.ES2022,
},
}).outputText;
const exports = {};
runInNewContext(compiled, { exports });
const handle = exports.handle;
function fixture() {
const records = new Map();
let access;
const ctx = {
operation: 'createWorld',
account: 'owner',
owner: 'nostr:owner',
principal: 'nostr:owner',
state: {
async get(c, k) {
return structuredClone(records.get(`${c}/${k}`));
},
async set(c, k, value) {
records.set(`${c}/${k}`, structuredClone(value));
},
async setAccess(value) {
access = value;
},
},
};
return { ctx, world: () => records.get("worlds/island"), access: () => access };
}
test('creative island is bounded, persistent, and guest-buildable by explicit policy', async () => {
const f = fixture();
await handle(f.ctx, { name: 'A shared island' });
assert.equal(f.world().blocks.length, 3888);
assert.equal(f.access().guestsMayBuild, true);
f.ctx.operation = 'readWorld';
assert.equal((await handle(f.ctx, {})).name, 'A shared island');
});
test('top-block edits enforce stale reads, foundation, height and support rules', async () => {
const f = fixture();
await handle(f.ctx, { name: 'Rules' });
f.ctx.operation = 'editBlock';
const p = { x: 9, y: 3, z: 10, expected: 0, block: 4 };
await handle(f.ctx, p);
assert.equal(f.world().edits, 1);
await assert.rejects(handle(f.ctx, p), /CONFLICT/);
await assert.rejects(handle(f.ctx, { ...p, y: 1 }), /INVALID/);
await assert.rejects(handle(f.ctx, { ...p, y: 12 }), /INVALID/);
await assert.rejects(handle(f.ctx, { ...p, y: 5 }), /existing block/);
await assert.rejects(
handle(f.ctx, { ...p, y: 2, expected: 1, block: 0 }),
/top block/,
);
await handle(f.ctx, { ...p, expected: 4, block: 0 });
assert.equal(f.world().edits, 2);
});
test('only verified owner can pause and resume building, lock rejects edits', async () => {
const f = fixture();
await handle(f.ctx, { name: 'Owner' });
f.ctx.operation = 'setLocked';
f.ctx.account = 'intruder';
f.ctx.principal = 'nostr:intruder';
await assert.rejects(handle(f.ctx, { locked: true }), /FORBIDDEN/);
f.ctx.account = 'owner';
f.ctx.principal = 'nostr:owner';
await handle(f.ctx, { locked: true });
f.ctx.operation = 'editBlock';
await assert.rejects(
handle(f.ctx, { x: 9, y: 3, z: 10, expected: 0, block: 4 }),
/paused/,
);
f.ctx.operation = 'setLocked';
await handle(f.ctx, { locked: false });
assert.equal(f.world().locked, false);
});
test('batches are atomic, sequential and return a compact ordered delta', async () => {
const f = fixture();
await handle(f.ctx,{name:'Batch'});
f.ctx.operation = 'editBlocks';
const edits = [
{x:9,y:3,z:10,expected:0,block:4},
{x:9,y:4,z:10,expected:0,block:5},
{x:9,y:4,z:10,expected:5,block:0},
];
const result = await handle(f.ctx,{after:0,edits});
assert.equal(result.edits,3);
assert.equal(result.changes.length,3);
assert.equal(result.blocks,undefined);
assert.ok(JSON.stringify(result).length < 400);
await assert.rejects(handle(f.ctx,{after:3,edits:[{x:9,y:4,z:10,expected:0,block:6},{x:9,y:3,z:10,expected:0,block:2}]}),/CONFLICT/);
assert.equal(f.world().edits,3);
assert.equal(f.world().blocks[9+10*18+4*324],0,'failed batch saves none');
f.ctx.operation = 'readUpdates';
const updates = await handle(f.ctx,{after:1});
assert.deepEqual(Array.from(updates.changes,c=>c.n),[2,3]);
const unchanged = await handle(f.ctx,{after:3});
assert.equal(unchanged.changes.length,0);
});
test('bounded journal asks an old cursor to resync; lock also rejects batches', async () => {
const f = fixture();
await handle(f.ctx,{name:'Journal'});
f.ctx.operation = 'editBlocks';
for (let i=0;i<130;i++) await handle(f.ctx,{after:i,edits:[{x:9,y:3,z:10,expected:i%2?4:0,block:i%2?0:4}]});
f.ctx.operation = 'readUpdates';
assert.equal((await handle(f.ctx,{after:0})).reset,true);
assert.equal((await handle(f.ctx,{after:129})).changes.length,1);
f.ctx.operation = 'setLocked'; await handle(f.ctx,{locked:true});
f.ctx.operation = 'editBlocks';
await assert.rejects(handle(f.ctx,{after:130,edits:[{x:9,y:3,z:10,expected:0,block:4}]}),/paused/);
});
