~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/atlibs-socialweb-computer-requester

socialweb.computer market requester and policy modules for atlibs, with its web pages and lexicons

0
0
46 lines · 2.7 KB Raw History
// kcp parity for bid-payload: internal/policyengine/spec/engine_test.go TestWorkPolicies
// (matrix/bid-payload/full/requester/{free-bid,paid-bid-not-allowed}) and bid-payload.ts.
import assert from "node:assert/strict";
import { test } from "node:test";

import { abiSuite, Bidder, loadPolicy, NSID, Requester, World } from "../../crates/policy/vectors.mjs";

const mod = await loadPolicy("policy-bid-payload");
abiSuite(mod);
const allow = { allow: true, violations: [] };
const deny = (nsid) => ({ allow: false, violations: [{ msg: `bid payload type ${nsid} not allowed`, policyId: "bid-payload" }] });
const base = { selfDid: Requester, subjectDid: Bidder, counterpartyDid: Bidder, rootRequesterDid: Requester, mode: "full" };

test("free bid allowed, paid bid denied (engine_test.go matrix/bid-payload)", async () => {
	for (const [free, want] of [[true, allow], [false, deny(NSID.x402)]]) {
		const world = new World();
		const bid = world.bid(Bidder, `bid-${free}`, free, 3);
		const { verdict, reads } = await mod.evaluate({ ...base, bid }, world);
		assert.deepEqual(verdict, want);
		assert.deepEqual(reads, [bid.uri], "the bid ref is hydrated with one read");
	}
});

test("args.allowedPayloadNsids replaces the free-only default; a non-array keeps the default", async () => {
	const world = new World();
	const paid = world.bid(Bidder, "paid", false, 3);
	const free = world.bid(Bidder, "free", true);
	assert.deepEqual((await mod.evaluate({ ...base, bid: paid, args: { allowedPayloadNsids: [NSID.x402] } }, world)).verdict, allow);
	assert.deepEqual((await mod.evaluate({ ...base, bid: free, args: { allowedPayloadNsids: [NSID.x402] } }, world)).verdict, deny(NSID.free));
	assert.deepEqual((await mod.evaluate({ ...base, bid: paid, args: { allowedPayloadNsids: NSID.x402 } }, world)).verdict, deny(NSID.x402));
});

test("no offer allows: no bid, scope mode, an unreadable bid, a payload without cid", async () => {
	assert.deepEqual((await mod.evaluate(base)).verdict, allow);
	assert.deepEqual((await mod.evaluate({ ...base, mode: "scope" })).verdict, allow);
	assert.deepEqual((await mod.evaluate({ ...base, bid: { uri: `at://${Bidder}/${NSID.bid}/gone`, cid: "bafyx" } })).verdict, allow);
	const inline = { uri: `at://${Bidder}/${NSID.bid}/b`, cid: "bafyx", value: { payload: { uri: `at://${Bidder}/${NSID.x402}/p` } } };
	assert.deepEqual((await mod.evaluate({ ...base, bid: inline })).verdict, allow);
});

test("a bid with its value inline needs no read", async () => {
	const bid = { uri: `at://${Bidder}/${NSID.bid}/b`, cid: "bafyx", value: { payload: { uri: `at://${Bidder}/${NSID.x402}/p`, cid: "bafyp" } } };
	const { verdict, reads } = await mod.evaluate({ ...base, bid });
	assert.deepEqual(verdict, deny(NSID.x402));
	assert.deepEqual(reads, []);
});