test("verifies the exact host-sealed Epiphany receipt",async()=>{const{a,anchor,receipt}=awaitfixture();assert.equal(verifySealedReceipt({receipt,admission:a,trustedIdentity:anchor,encode:msgpack.encode}),receipt);});
test("refuses wrong key, signature, digest, binding, predating completion, and schema",async()=>{const{a,anchor,receipt}=awaitfixture(),other=awaitfixture();constcases=[{...receipt,providerIdentityId:other.identity.identityId},{...receipt,executorSignature:newUint8Array(64)},{...receipt,resultPayloadSha256:"sha256-wrong"},{...receipt,packetSha256:"sha256-wrong"},{...receipt,completedAt:"2026-07-19T11:59:59Z",result:{...receipt.result,completedAt:"2026-07-19T11:59:59Z"}},{...receipt,schemaVersion:"epiphany.operator_command.sealed_result.v9"}];for(constvalueofcases)assert.throws(()=>verifySealedReceipt({receipt:value,admission:a,trustedIdentity:anchor,encode:msgpack.encode}));});
test("accepts a correctly signed post-expiry receipt for durable exact recovery",async()=>{const{a,anchor,identity,receipt}=awaitfixture(),completedAt="2026-07-19T12:02:00Z",result={...receipt.result,completedAt},recovered={...receipt,result,completedAt,resultPayloadSha256:`sha256-${createHash("sha256").update(msgpack.encode(result)).digest("hex")}`,executorSignature:newUint8Array()};recovered.executorSignature=newUint8Array(signPurpose(identity,EPIPHANY_RESULT_SIGNING_PURPOSE,msgpack.encode(resultReceiptTuple(recovered))));assert.equal(verifySealedReceipt({receipt:recovered,admission:a,trustedIdentity:anchor,encode:msgpack.encode}),recovered);});
test("RUDP adapter requires a correlated typed application result and times out otherwise",async()=>{const{a,anchor,receipt}=awaitfixture();classFakeConnectionextendsEventEmitter{connected=false;connect(){this.connected=true;}send(){if(this.reply)setImmediate(()=>this.emit("frame",this.reply));}close(){this.closed=true;}}constfakeSocket=()=>{consts=newEventEmitter();s.bind=()=>setImmediate(()=>s.emit("listening"));returns;};constmake=(reply,timeoutMs=100)=>{constconnection=newFakeConnection();connection.reply=reply;return{connection,transport:newEpiphanyOperatorCultNetRudpTransport({endpoint:"rudp://127.0.0.1:49100",trustedEpiphanyIdentity:anchor,timeoutMs,cultnetRuntime:cultnet,msgpackRuntime:msgpack,socketFactory:fakeSocket,connectionFactory:()=>connection})};};constmessage={schemaVersion:"cultnet.document_put_raw.v0",messageId:"operator-result-command-1",document:{schemaId:"epiphany.operator_command.sealed_result",recordKey:a.packet.commandId,storedAt:receipt.completedAt,payloadEncoding:"messagepack",payload:msgpack.encode(receipt),sourceRuntimeId:a.packet.targetRuntimeId,sourceAgentId:receipt.providerIdentityId,sourceRole:"epiphany-operator-command-executor",tags:["cultnet.transport.rudp.v0"]}};constok=make({channelId:"schema",payload:msgpack.encode(message)});assert.equal((awaitok.transport.execute(a)).result.resultId,"result-1");constabsent=make(undefined);awaitassert.rejects(()=>absent.transport.execute(a),/Timed out waiting for the correlated/);constwrong=make({channelId:"schema",payload:msgpack.encode({...message,document:{...message.document,recordKey:"alien"}})});awaitassert.rejects(()=>wrong.transport.execute(a),/substituted result authority/);});
test("real CultNet RUDP socket path waits for the sealed application document",async()=>{const{a,anchor,receipt}=awaitfixture(),socket=createSocket("udp4");awaitnewPromise((ok,fail)=>{socket.once("error",fail);socket.bind(0,"127.0.0.1",ok)});constserver=newcultnet.CultNetRudpSocketTransportConnection({runtimeId:a.packet.targetRuntimeId,socket,mode:"server",connectionId:0xe91f0001,resendDelayMs:25,resendPollMs:5});server.on("frame",frame=>{constrequest=msgpack.decode(frame.payload);assert.equal(request.document.schemaId,"bifrost.operator_command.delivery");constresponse={schemaVersion:"cultnet.document_put_raw.v0",messageId:"operator-result-command-1",document:{schemaId:"epiphany.operator_command.sealed_result",recordKey:a.packet.commandId,storedAt:receipt.completedAt,payloadEncoding:"messagepack",payload:msgpack.encode(receipt),sourceRuntimeId:a.packet.targetRuntimeId,sourceAgentId:receipt.providerIdentityId,sourceRole:"epiphany-operator-command-executor",tags:["cultnet.transport.rudp.v0"]}};server.send("schema",msgpack.encode(cultnet.encodeCultNetMessageForWire(response,"cultnet.schema.v0")));});try{constport=socket.address().port,transport=newEpiphanyOperatorCultNetRudpTransport({endpoint:`rudp://127.0.0.1:${port}`,trustedEpiphanyIdentity:anchor,timeoutMs:1000,cultnetRuntime:cultnet,msgpackRuntime:msgpack});constoutput=awaittransport.execute(a);assert.equal(output.receipt.providerIdentityId,anchor.identityId);assert.equal(output.result.commandId,a.packet.commandId);}finally{server.close();}});