Files
loki/scripts/test/crew-delegation.ts
CatoandClaude Fable 5.1 95c40a9cd8 refactor: rename the product from FleetCrown to Loki (#695)
The product, its assistant, the repo, the host and every unit on the box now
share one name. 638 files, ~3,000 mentions: UI copy, config, env keys
(FLEETCROWN_* -> LOKI_*), systemd units (fleetcrown-* -> loki-*), docs,
articles, the deploy workflow other repos reuse, and nine file names.

- Three article slugs carried the old name and had been shared; permanent
  redirects keep those links alive (next.config.ts).
- test-no-retired-name.sh now also forbids the old product name across the
  whole repo, so the rename cannot creep back through templates and examples.
- ASSISTANT_STACK collapses the product and the assistant into one entry,
  because they are now the same name.

Infra was cut over ahead of this merge: /opt/loki, loki-* units, the `loki`
database and role, loki.orangecat.ch (old host 308s to it; /api, /widget,
/embed keep proxying), GitHub repos bitbaum/loki and bitbaum/loki-releases.
The Resend sending domain stays fleetcrown.orangecat.ch until the new domain
is verified in DNS.


Claude-Session: https://claude.ai/code/session_016YJVAanfj8keVSXyZnTemh

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-13 16:13:10 -07:00

232 lines
10 KiB
TypeScript

/**
* Inline tests for the crew delegation rules (config/crew.ts + config/actors.ts).
*
* Why this is worth testing: the whole feature rests on an asymmetry that is
* invisible at a glance — the operator hands work out and accepts it back, but
* only the person asked can say yes, say no, or claim it is done. If an
* operator-side move ever gains `accepted`, Loki starts recording consent
* nobody gave, and it does so silently, on a board that looks correct. The same
* goes the other way: an assignee move that could reach `done` would let the
* person asked sign off their own work.
*
* The second half guards the actor kernel: a crew flag on a robot, or a listing
* flag on a person, are both "a person became inventory" bugs. They are cheap
* to prevent here and expensive to notice in production.
*
* Pure — no DB, no network. Run: npx tsx scripts/test/crew-delegation.ts
*/
import {
ASSIGNEE_ACTION,
ASSIGNEE_ACTION_STATUS,
CLOSED_HUMAN_TASK_STATUSES,
HUMAN_TASK_STATUS,
HUMAN_TASK_STATUS_LABEL,
HUMAN_TASK_STATUS_ORDER,
OPEN_HUMAN_TASK_STATUSES,
assigneeActionsFor,
canAssigneeMove,
canOperatorMove,
BTC,
TASK_CURRENCIES,
canShare,
formatFee,
formatSats,
orangeCatProfileUrl,
isWaitingOnAssignee,
isWaitingOnOperator,
taskSharePath,
type HumanTaskStatus,
} from "@/config/crew";
import {
ACTOR_CAPABILITY,
CREW_ATTR,
MARKET_ATTR,
assertAttrAllowed,
canDelegate,
isActorCapabilityError,
} from "@/config/actors";
import { ENTITY_TYPE } from "@/lib/constants/statuses";
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(message);
}
let passed = 0;
const ok = (condition: boolean, message: string) => {
assert(condition, message);
passed += 1;
};
const ALL_STATUSES = Object.values(HUMAN_TASK_STATUS) as HumanTaskStatus[];
// ─── The asymmetry: who may move what ────────────────────────────────────────
// The operator asks. They never answer on the other person's behalf.
for (const from of ALL_STATUSES) {
ok(
!canOperatorMove(from, HUMAN_TASK_STATUS.ACCEPTED) || from === HUMAN_TASK_STATUS.DELIVERED,
`operator must not record consent: ${from} → accepted`,
);
ok(
!canOperatorMove(from, HUMAN_TASK_STATUS.DECLINED),
`operator must not decline on someone's behalf: ${from} → declined`,
);
}
// The one exception above is deliberate: delivered → accepted is "not good
// enough yet, you are still on it", which is the operator revising THEIR
// judgement of the work, not asserting the person agreed to something.
ok(
canOperatorMove(HUMAN_TASK_STATUS.DELIVERED, HUMAN_TASK_STATUS.ACCEPTED),
"sending delivered work back must stay possible",
);
// The assignee answers. They never close the work off as accepted.
for (const from of ALL_STATUSES) {
ok(
!canAssigneeMove(from, HUMAN_TASK_STATUS.DONE),
`assignee must not sign off their own work: ${from} → done`,
);
ok(
!canAssigneeMove(from, HUMAN_TASK_STATUS.CANCELLED),
`assignee must not cancel the ask: ${from} → cancelled`,
);
ok(
!canAssigneeMove(from, HUMAN_TASK_STATUS.ASSIGNED),
`assignee must not hand work to themselves: ${from} → assigned`,
);
}
// A draft has told nobody anything, so nobody can answer it.
ok(
assigneeActionsFor(HUMAN_TASK_STATUS.DRAFT).length === 0,
"a draft must offer the assignee nothing — it was never sent",
);
// Once asked, all three honest answers are on the table. Declining is not
// hidden behind acceptance: an ask you cannot refuse is an order.
const asked = assigneeActionsFor(HUMAN_TASK_STATUS.ASSIGNED);
ok(asked.includes(ASSIGNEE_ACTION.ACCEPT), "an asked person can say yes");
ok(asked.includes(ASSIGNEE_ACTION.DECLINE), "an asked person can say no");
ok(asked.includes(ASSIGNEE_ACTION.DELIVER), "an asked person can just do it");
// Having accepted, they can still back out — circumstances change, and a
// system that only allows yes-then-deliver produces silence instead of news.
ok(
assigneeActionsFor(HUMAN_TASK_STATUS.ACCEPTED).includes(ASSIGNEE_ACTION.DECLINE),
"someone who accepted can still tell you they cannot after all",
);
// Nothing to answer once it is closed or already delivered.
for (const status of [...CLOSED_HUMAN_TASK_STATUSES, HUMAN_TASK_STATUS.DELIVERED]) {
ok(assigneeActionsFor(status).length === 0, `no actions once ${status}`);
}
// Every action maps to a status the assignee is actually allowed to reach.
for (const action of Object.values(ASSIGNEE_ACTION)) {
const target = ASSIGNEE_ACTION_STATUS[action];
ok(
ALL_STATUSES.some((from) => canAssigneeMove(from, target)),
`action ${action} maps to ${target}, which no assignee move reaches`,
);
}
// ─── Board bookkeeping ───────────────────────────────────────────────────────
ok(
OPEN_HUMAN_TASK_STATUSES.every((s) => !CLOSED_HUMAN_TASK_STATUSES.includes(s)),
"a status cannot be both open and closed",
);
ok(
OPEN_HUMAN_TASK_STATUSES.length + CLOSED_HUMAN_TASK_STATUSES.length === ALL_STATUSES.length,
"every status must be classified open or closed — an unclassified one vanishes from the counts",
);
for (const status of ALL_STATUSES) {
ok(HUMAN_TASK_STATUS_ORDER.includes(status), `${status} missing from the board order`);
ok(Boolean(HUMAN_TASK_STATUS_LABEL[status]), `${status} has no label`);
// Waiting-on is a partition of the open set: a row that is nobody's move is
// a row that quietly rots.
const open = OPEN_HUMAN_TASK_STATUSES.includes(status);
ok(
!open || isWaitingOnOperator(status) !== isWaitingOnAssignee(status),
`${status} is open but is either nobody's move or both`,
);
}
// ─── Handing over ────────────────────────────────────────────────────────────
ok(
!canShare({ status: HUMAN_TASK_STATUS.DRAFT, assigneeId: null }),
"an unassigned ask has nobody to hand it to",
);
ok(
canShare({ status: HUMAN_TASK_STATUS.DRAFT, assigneeId: "id" }),
"an assigned draft is exactly what handing over is for",
);
for (const status of CLOSED_HUMAN_TASK_STATUSES) {
ok(!canShare({ status, assigneeId: "id" }), `a ${status} assignment has nothing left to ask`);
}
ok(taskSharePath("abc") === "/share/task/abc", "share path is the public route");
// Fees are displayed, never computed. No currency, no invented one.
ok(formatFee(null, "CHF") === "", "no fee renders as nothing at all");
ok(formatFee(400, "CHF") === "CHF 400", "whole amounts stay whole");
ok(formatFee(12.5, "EUR") === "EUR 12.50", "part amounts get two places");
ok(formatFee(400, null) === "400", "an amount without a currency is not dressed up as one");
// ─── Bitcoin ─────────────────────────────────────────────────────────────────
// The failure this guards is silent and expensive: two-decimal formatting turns
// every sub-cent bitcoin fee into "0.00", so the person owed 50,000 sats is
// shown a fee of nothing on the page where they decide whether to say yes.
ok(formatFee(0.0005, BTC) === "BTC 0.0005", "a BTC fee keeps its decimals");
ok(formatFee(0.00000001, BTC) === "BTC 0.00000001", "one satoshi survives formatting");
ok(formatFee(0.5, BTC) === "BTC 0.5", "no trailing-zero noise on round amounts");
ok(formatFee(1, BTC) === "BTC 1", "a whole coin does not render as 1.00000000");
ok(formatSats(0.0005, BTC) === "50,000 sats", "sats are the unit people quote");
ok(formatSats(0.0005, "CHF") === "", "francs are not satoshis");
ok(formatSats(null, BTC) === "", "no fee, no sats line");
// The list must stay exactly what OrangeCat's services API accepts: a currency
// Loki offers but OrangeCat rejects is a fee that cannot be mirrored.
ok(TASK_CURRENCIES.includes(BTC), "BTC is assignable — paying a person is the point");
for (const c of ["USD", "EUR", "CHF", "GBP", "BTC"]) {
ok((TASK_CURRENCIES as readonly string[]).includes(c), `${c} matches OrangeCat's enum`);
}
// The payment destination is normalised, so one wallet cannot be recorded two
// ways — and anything that is not a profile is refused rather than half-saved.
const canonical = "https://orangecat.ch/profiles/jana";
ok(orangeCatProfileUrl("jana") === canonical, "a bare handle resolves");
ok(orangeCatProfileUrl("@jana") === canonical, "an @handle resolves");
ok(orangeCatProfileUrl(canonical) === canonical, "a canonical URL is unchanged");
ok(orangeCatProfileUrl(`${canonical}/`) === canonical, "a trailing slash is not a second wallet");
ok(orangeCatProfileUrl("https://orangecat.ch/services/abc") === null, "a service is not a person");
ok(orangeCatProfileUrl("not a url at all") === null, "prose is not a payment destination");
ok(orangeCatProfileUrl("") === null, "empty is not a payment destination");
// ─── The actor kernel: people are not inventory ──────────────────────────────
ok(canDelegate(ENTITY_TYPE.PERSON), "people can be asked to do work");
ok(!canDelegate(ENTITY_TYPE.ROBOT), "machines are dispatched, not asked");
function refuses(type: string, key: string, why: string) {
let threw = false;
try {
assertAttrAllowed(type, key);
} catch (e) {
threw = isActorCapabilityError(e);
}
ok(threw, why);
}
refuses(ENTITY_TYPE.ROBOT, CREW_ATTR.MEMBER, "a robot must never join the crew roster");
refuses(ENTITY_TYPE.ROBOT, CREW_ATTR.RATE, "a robot must never carry a crew rate");
refuses(ENTITY_TYPE.PERSON, MARKET_ATTR.rent, "a person must never carry a listing flag");
refuses(ENTITY_TYPE.PERSON, MARKET_ATTR.sell, "a person must never be sellable");
// The allowed direction still works, or the guard has eaten the feature.
assertAttrAllowed(ENTITY_TYPE.PERSON, CREW_ATTR.ROLE);
assertAttrAllowed(ENTITY_TYPE.ROBOT, MARKET_ATTR.rent);
passed += 2;
ok(
ACTOR_CAPABILITY.DELEGATE === "delegate",
"capability name is persisted in nothing, but renaming it silently would break the guards above",
);
console.log(`✓ crew delegation rules — ${passed} assertions passed`);