@capybara-hq/testing 0.1.0-rc.18 → 0.1.0-rc.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,7 +10,7 @@ export default defineExtension({
10
10
  version: "0.1.0",
11
11
  sdk: "^0.1.0-rc.0",
12
12
  provides: ["counter@1"],
13
- permissions: { http: [{ method: "GET", path: "/ping" }] },
13
+ permissions: { http: [{ method: "GET", path: "/ping", access: "public" }] },
14
14
  setup(ctx) {
15
15
  const pong = "pong from fake-dep-provider";
16
16
  ctx.provide("counter@1", { ping: () => pong });
@@ -11,7 +11,7 @@ export default defineExtension({
11
11
  sdk: "^0.1.0-rc.0",
12
12
  provides: [],
13
13
  requires: ["counter@1"],
14
- permissions: { http: [{ method: "GET", path: "/ping" }] },
14
+ permissions: { http: [{ method: "GET", path: "/ping", access: "public" }] },
15
15
  setup(ctx) {
16
16
  ctx.http.route("GET", "/ping", async () => {
17
17
  const impl = ctx.extensionPoints.list<{ ping(): string }>("counter@1")[0]?.impl;
@@ -1,27 +1,27 @@
1
- import { defineExtension, EMBED_PROTOCOL, type OrgEmbedClaims } from "@capybara-hq/plugin-sdk";
1
+ import { defineExtension, EMBED_PROTOCOL, type RouteCaller } from "@capybara-hq/plugin-sdk";
2
2
  import { z } from "zod";
3
3
 
4
4
  /**
5
5
  * Test fake of an embed extension (`ui.embeds`): the kernel side of embedded apps (token issue and
6
6
  * verification, the `/plugin-ui` proxy, `ctx.bus` across replicas) without the official board.
7
7
  * - embeds `panel` (org page, `/ui/panel`) and `panel-conv` (conversation tab, `/ui/panel-conv`);
8
- * both pages are static HTML loading the static script `/ui/panel.js`; none of them needs a
9
- * token. The script speaks the host's message bridge (`capybara-embed/1`): on `init` it fetches
8
+ * both pages are static HTML loading the static script `/ui/panel.js`; all three are `public`
9
+ * GET routes (an iframe navigation carries no token). The script speaks the host's message bridge (`capybara-embed/1`): on `init` it fetches
10
10
  * `claims` with the token it was given, shows `<contribution> <orgId>` in `#status` and posts a
11
11
  * `toast` "fake-embed ready" to the host (a failed fetch shows `error <status>`), then reads the
12
12
  * `/ui/events` stream with the token: `ready` sets `data-live="1"` on the body, each `event`
13
13
  * shows its text in `#event`;
14
- * - every data route checks `Authorization: Bearer <embed token>` with `ctx.ui.verifyEmbedToken`
15
- * (401 otherwise); identity comes only from the claims;
16
- * - `GET /ui/claims` returns the verified claims;
14
+ * - every data route is an `embed` route: the kernel checks the embed token (`Authorization: Bearer`
15
+ * or `?token=`) and hands the handler the caller; identity comes only from `caller` (an org-scope
16
+ * caller, 401 otherwise);
17
+ * - `GET /ui/claims` returns the caller;
17
18
  * - `POST /ui/events` `{ text }` publishes `{ conversationId, text }` on the bus topic
18
19
  * `events.<orgId>` (202);
19
20
  * - `GET /ui/events` is an SSE stream: `ready` first, then an `event` for each message in the
20
21
  * token's scope (its conversation, or org-wide messages for a page token); it ends when the
21
- * client disconnects or the token expires.
22
+ * client disconnects or the token expires (`caller.exp`).
22
23
  */
23
24
 
24
- const BEARER_RE = /^Bearer ([A-Za-z0-9._-]{1,4096})$/;
25
25
  const EventBody = z.object({ text: z.string().min(1).max(200) });
26
26
 
27
27
  const PAGE = `<!doctype html><html><head><meta charset="utf-8"><title>Fake embed</title><script src="./panel.js"></script></head><body><p>fake-embed</p><p id="status">waiting</p><p id="event"></p></body></html>`;
@@ -110,22 +110,19 @@ export default defineExtension({
110
110
  ],
111
111
  permissions: {
112
112
  http: [
113
- { method: "GET", path: "/ui/panel" },
114
- { method: "GET", path: "/ui/panel-conv" },
115
- { method: "GET", path: "/ui/panel.js" },
116
- { method: "GET", path: "/ui/claims" },
117
- { method: "POST", path: "/ui/events" },
118
- { method: "GET", path: "/ui/events" },
113
+ { method: "GET", path: "/ui/panel", access: "public" },
114
+ { method: "GET", path: "/ui/panel-conv", access: "public" },
115
+ { method: "GET", path: "/ui/panel.js", access: "public" },
116
+ { method: "GET", path: "/ui/claims", access: "embed" },
117
+ { method: "POST", path: "/ui/events", access: "embed" },
118
+ { method: "GET", path: "/ui/events", access: "embed" },
119
119
  ],
120
120
  ui: { embeds: ["panel", "panel-conv"] },
121
121
  },
122
122
  setup(ctx) {
123
- /** An org-scope token's claims (every contribution here is in an org slot), or null. */
124
- const auth = async (req: Request): Promise<OrgEmbedClaims | null> => {
125
- const m = BEARER_RE.exec(req.headers.get("authorization") ?? "");
126
- const claims = m?.[1] === undefined ? null : await ctx.ui.verifyEmbedToken(m[1]);
127
- return claims?.scope === "org" ? claims : null;
128
- };
123
+ /** The org-scope embed caller (every contribution here is in an org slot), or null. */
124
+ const auth = (caller: RouteCaller) =>
125
+ caller.access === "embed" && caller.scope === "org" ? caller : null;
129
126
  const page = async () =>
130
127
  new Response(PAGE, { headers: { "content-type": "text/html; charset=utf-8" } });
131
128
  ctx.http.route("GET", "/ui/panel", page);
@@ -137,13 +134,13 @@ export default defineExtension({
137
134
  new Response(SCRIPT, { headers: { "content-type": "text/javascript; charset=utf-8" } }),
138
135
  );
139
136
 
140
- ctx.http.route("GET", "/ui/claims", async (req) => {
141
- const claims = await auth(req);
137
+ ctx.http.route("GET", "/ui/claims", async (_req, caller) => {
138
+ const claims = auth(caller);
142
139
  return claims ? json(200, claims) : unauthorized();
143
140
  });
144
141
 
145
- ctx.http.route("POST", "/ui/events", async (req) => {
146
- const claims = await auth(req);
142
+ ctx.http.route("POST", "/ui/events", async (req, caller) => {
143
+ const claims = auth(caller);
147
144
  if (!claims) return unauthorized();
148
145
  const body = EventBody.safeParse(await req.json().catch(() => undefined));
149
146
  if (!body.success) {
@@ -156,9 +153,10 @@ export default defineExtension({
156
153
  return json(202, { published: true });
157
154
  });
158
155
 
159
- ctx.http.route("GET", "/ui/events", async (req) => {
160
- const claims = await auth(req);
156
+ ctx.http.route("GET", "/ui/events", async (req, caller) => {
157
+ const claims = auth(caller);
161
158
  if (!claims) return unauthorized();
159
+ // ends when the client disconnects or the token expires (`caller.exp`)
162
160
  const done = AbortSignal.any([
163
161
  req.signal,
164
162
  AbortSignal.timeout(Math.max(1, claims.exp * 1000 - Date.now())),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/testing",
3
- "version": "0.1.0-rc.18",
3
+ "version": "0.1.0-rc.19",
4
4
  "description": "Test helpers for Capybara: test containers, dual-replica harness and fake third-party servers",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -47,18 +47,18 @@
47
47
  "tsx": "^4.23.15",
48
48
  "ws": "^8.22.0",
49
49
  "zod": "^4.6.7",
50
- "@capybara-hq/kernel": "^0.1.0-rc.28"
50
+ "@capybara-hq/kernel": "^0.1.0-rc.32"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/pg": "^8.23.1",
54
54
  "@types/ws": "^8.18.2",
55
- "@capybara-hq/plugin-sdk": "^0.1.0-rc.21",
55
+ "@capybara-hq/plugin-sdk": "^0.1.0-rc.23",
56
56
  "@capybara-hq/plugin-client": "^0.1.0-rc.5"
57
57
  },
58
58
  "peerDependencies": {
59
59
  "vitest": "^5.0.5",
60
- "@capybara-hq/plugin-sdk": "^0.1.0-rc.21",
61
- "@capybara-hq/plugin-tools": "^0.1.0-rc.13"
60
+ "@capybara-hq/plugin-sdk": "^0.1.0-rc.23",
61
+ "@capybara-hq/plugin-tools": "^0.1.0-rc.15"
62
62
  },
63
63
  "peerDependenciesMeta": {
64
64
  "@capybara-hq/plugin-tools": {