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

johnandersen777.bsky.social/atlibs-cocore

0
0
main
38 lines · 2.0 KB Raw History
import assert from "node:assert/strict";
import { test } from "node:test";

import { abiTest, did, loadModule } from "../../test/check.mjs";

const keys = await loadModule("cocore-keys");
const CONSOLE = "did:web:cocore.dev#cocore_console";
abiTest(test, keys);

const answer = (output) => async (entry, input) => {
	const calls = [];
	const ran = await keys.handle(entry, input, { respond: (call) => (calls.push(call), { ok: true, output }) });
	return { ran, calls: calls.map(({ nsid, params, input, proxy }) => ({ nsid, params, input, proxy })) };
};

test("each action is one console call through the PDS (service auth)", async () => {
	const key = { id: "k1", did, name: "ci", prefix: "cocore-AbCd", createdAt: "2026-11-10T00:00:00.000Z" };
	const list = await answer({ keys: [key] })("list_keys", {});
	assert.deepEqual(list.calls, [{ nsid: "dev.cocore.account.listApiKeys", params: {}, input: undefined, proxy: CONSOLE }]);
	assert.deepEqual(list.ran.result, { keys: [key] });

	const create = await answer({ key, secret: "cocore-secret" })("create_key", { name: " ci ", expiresAt: "2027-01-01T00:00:00Z" });
	assert.deepEqual(create.calls[0].input, { name: "ci", expiresAt: "2027-01-01T00:00:00Z" });
	assert.equal(create.ran.result.secret, "cocore-secret");

	for (const [entry, nsid, output] of [["revoke_key", "revokeApiKey", { revoked: true }], ["delete_key", "deleteApiKey", { deleted: true }]]) {
		const ran = await answer(output)(entry, { id: "k1" });
		assert.deepEqual(ran.calls, [{ nsid: `dev.cocore.account.${nsid}`, params: undefined, input: { id: "k1" }, proxy: CONSOLE }]);
		assert.deepEqual(ran.ran.result, output);
	}
});

test("create needs a name, revoke and delete an id", () => {
	const start = (entry, fields = {}) => keys.run(entry, { did, ...fields, state: null, event: { type: "start" } });
	assert.match(start("create_key").error, /name is required/);
	assert.match(start("revoke_key").error, /id is required/);
	assert.match(start("delete_key", { id: "  " }).error, /id is required/);
});