Files
loki/scripts/test/project-capabilities.ts
CatoandClaude Fable 5.1 14e8667896 feat(projects): builders can dispatch; client is a role; hand a project over (#958)
* feat(widget): Ask Loki for a second opinion; hiding is visible and reversible

Tested the embed in a real browser and found three install/hide defects:
- a `type="module"` tag rendered nothing (document.currentScript is null);
- two copies of the tag rendered two launchers;
- hiding lived only behind a long-press/right-click and was one-way: after a
  reload nothing, not even the host's own Loki.report(), brought it back.

Fixes: find the tag by its attribute when currentScript is null; a boot-in-
flight guard keeps one widget per page; "Hide this button on this site" sits
at the foot of the panel with an Undo toast, and #loki, Loki.show(),
report() and ask() all restore it.

New Ask Loki mode (on by default next to "Request a change"): the person the
site was built for asks whether an element, the page or the whole site should
change and why it is the way it is. The widget builds a text outline in the
visitor's browser (a few same-origin pages for "Whole site") and POSTs it to
/api/widget/advise — same token, origin allowlist, rate limits and owner AI
budget as widget chat. The answer may say "leave it"; each recommended change
has "Request this", which prefills Report so advice becomes ordinary
Implement work. data-fc-modes="report" opts a site out.

Tests: scripts/test/widget-embed-browser.ts (real Chromium; fails on the old
bundle) and scripts/test/widget-advise.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EsT2PtLuBKwgaaBU5pheG8

* refactor(widget): move panel pieces out of main.ts to meet the function-length ratchet

CI lint failed: the main IIFE grew to 597 lines (max-lines-per-function 510).
The success views, scope chips, mode tabs and hide toast move to
widget/panel-views.ts; the counter/Send sync is one helper. No behaviour
change — widget-embed-browser (31) and widget-host-avoid-browser (17) pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EsT2PtLuBKwgaaBU5pheG8

* feat(projects): builders can dispatch; client is a role; hand a project over

- A collaborator with the builder role ("can run agents") was refused with
  "Project not found" the moment they dispatched, because dispatchStep looked
  the project up BY OWNER. It now asks getProjectAccess who may edit and
  refuses a non-editor with 403 and a sentence; the work still runs in the
  owner's tenant (project-capabilities.ts), so a builder receives the
  capability and never the owner's runner credentials.
- `client` joins PROJECT_ROLE_VALUES: the person the project is FOR. Same
  capabilities as a viewer, a different name, because a roster that cannot
  say who is paying is not a roster. ROLE_LABELS in project-capabilities.ts
  is the one table of role label / help / invitation sentence; the members
  panel and the invite email both read it (editors are shown as "Builder";
  the stored value stays `editor`).
- transferProjectOwnership: the owner hands the project to any member in one
  transaction — the user_projects row moves (the tenant the work runs in),
  the new owner's membership row goes, the old owner keeps a builder's seat.
  POST /api/projects/:id/transfer, and a "Hand over" button per member with
  a confirm that names what changes. The studio's hand-over to a client is
  this button.
- scripts/test/project-capabilities.ts covers the client role and that every
  role has a label and a help line.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EsT2PtLuBKwgaaBU5pheG8

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-09-28 06:17:12 -07:00

109 lines
3.8 KiB
TypeScript

/**
* The project permission boundary, pinned.
*
* Priority 5 of the 2026-09-17 review asks for the journey to be proved with an
* editor and a reporter account. The live two-account walk still needs two real
* sign-ins; this is the half that does NOT — the rule itself, which until now
* lived inside a DATABASE_URL-bound query and so had no test at all. The only
* way to discover that a viewer could edit was to sign in as one and look.
*
* These are the claims a permission bug would have to break.
*/
import assert from "node:assert/strict";
import {
capabilitiesForRole,
OWNER_CAPABILITIES,
ROLE_LABELS,
type ProjectCapabilities,
} from "@/lib/project-capabilities";
import { PROJECT_ROLE_VALUES } from "@/db/schema/project-memberships";
let passed = 0;
const check = (label: string, fn: () => void) => {
fn();
passed += 1;
console.log(` ✓ ${label}`);
};
check("the owner may do everything, including manage members", () => {
const owner = capabilitiesForRole("owner");
assert.equal(owner.canEdit, true);
assert.equal(owner.canManageMembers, true);
assert.deepEqual(owner, OWNER_CAPABILITIES);
});
check("an editor works the project but never changes who can reach it", () => {
const editor = capabilitiesForRole("editor");
assert.equal(editor.canEdit, true, "an editor is there to do the work");
assert.equal(
editor.canManageMembers,
false,
"membership is the owner's decision about their own tenant",
);
});
// A reporter is given `viewer` on the project they filed against: they can watch
// the fix, they cannot spend the owner's runner on another one.
check("a viewer reads and nothing more", () => {
const viewer = capabilitiesForRole("viewer");
assert.equal(viewer.canEdit, false, "a reporter must not be able to dispatch work");
assert.equal(viewer.canManageMembers, false);
});
// The client is who the project is for. They see everything a viewer sees and
// are named as the client on the roster; they do not spend the owner's runner.
check("a client follows and is named, but does not dispatch", () => {
const client = capabilitiesForRole("client");
assert.equal(client.canEdit, false, "the client is not the builder");
assert.equal(client.canManageMembers, false);
assert.equal(ROLE_LABELS.client.label, "Client");
});
check("every role has a label and a help line the roster can show", () => {
for (const role of ["owner", ...PROJECT_ROLE_VALUES] as const) {
assert.ok(ROLE_LABELS[role].label.length > 1, `${role} needs a label`);
assert.ok(ROLE_LABELS[role].help.startsWith(ROLE_LABELS[role].label), `${role} help names it`);
}
assert.equal(
ROLE_LABELS.editor.label,
"Builder",
"the stored value stays editor; the word is Builder",
);
});
check("NOBODY but the owner can manage members", () => {
for (const role of PROJECT_ROLE_VALUES) {
assert.equal(
capabilitiesForRole(role).canManageMembers,
false,
`${role} must not be able to change the member list`,
);
}
});
check("every declared role has a decided answer — none falls through", () => {
for (const role of PROJECT_ROLE_VALUES) {
const caps: ProjectCapabilities | undefined = capabilitiesForRole(role);
assert.ok(caps, `${role} has no capabilities entry`);
assert.equal(typeof caps.canEdit, "boolean", `${role}.canEdit must be decided`);
assert.equal(
typeof caps.canManageMembers,
"boolean",
`${role}.canManageMembers must be decided`,
);
}
});
check("no collaborator role is silently as powerful as the owner", () => {
for (const role of PROJECT_ROLE_VALUES) {
assert.notDeepEqual(
capabilitiesForRole(role),
OWNER_CAPABILITIES,
`${role} was granted the full owner capability set`,
);
}
});
console.log(`\nproject-capabilities: ${passed} checks passed`);