@mastra/server-adapters-test-suite 0.2.0 → 0.2.1-alpha.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # @mastra/server-adapters-test-suite
2
2
 
3
+ ## 0.2.1-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - `server.auth.authorize(path, method, user, context)` can now call `context.get('requestContext')` and `context.get('mastra')` on the Express, Fastify, Koa and Elysia adapters, as it already could on Hono. Values an app sets on the request context in `authorize` now reach route handlers on every adapter. ([#29069](https://github.com/mastra-ai/mastra/pull/29069))
8
+
9
+ ```ts
10
+ authorize: async (path, method, user, context) => {
11
+ context.get('requestContext').set('tenantId', user.orgId); // read it in any route handler
12
+ return appAuthorize(path, method, user, context);
13
+ };
14
+ ```
15
+
16
+ - Updated dependencies [[`0a553f0`](https://github.com/mastra-ai/mastra/commit/0a553f0f6ff4150fa5d9897b53824a647f7ddd1e), [`99ca92b`](https://github.com/mastra-ai/mastra/commit/99ca92b60272063a5cb978628e5d286f86c37d4d), [`ba602f5`](https://github.com/mastra-ai/mastra/commit/ba602f5f4622d6a3e6beb6d2741e3d19efe57ca9), [`4d3ebbd`](https://github.com/mastra-ai/mastra/commit/4d3ebbd304509acb118f319e8467fc932079e715), [`a4d1537`](https://github.com/mastra-ai/mastra/commit/a4d1537e8bac1d3634059bdbc843d787f1ac91f2), [`5a37eef`](https://github.com/mastra-ai/mastra/commit/5a37eefdafb239ccc6514018113f3fb1a025ce9b), [`41dc481`](https://github.com/mastra-ai/mastra/commit/41dc481f1b3509ec6c2a06e63caaa4f630cafa80), [`500edeb`](https://github.com/mastra-ai/mastra/commit/500edeb84e7ec665b5af26e99fced33cf3c85bfd), [`8bea966`](https://github.com/mastra-ai/mastra/commit/8bea9662c5d89c248c7311fc6cca6523d552af5d), [`0a553f0`](https://github.com/mastra-ai/mastra/commit/0a553f0f6ff4150fa5d9897b53824a647f7ddd1e), [`697ea99`](https://github.com/mastra-ai/mastra/commit/697ea99ae93f6dae203cab0f059bc5f9d8316c1e), [`cbe76c0`](https://github.com/mastra-ai/mastra/commit/cbe76c0d76e5543866f918fd41041c4d73a1a172), [`eddb060`](https://github.com/mastra-ai/mastra/commit/eddb0605db3f0be107e5ea392c7a407675303185), [`eddb060`](https://github.com/mastra-ai/mastra/commit/eddb0605db3f0be107e5ea392c7a407675303185), [`49e59f2`](https://github.com/mastra-ai/mastra/commit/49e59f251e45de082262e3892b599f48c1ddb413), [`ba602f5`](https://github.com/mastra-ai/mastra/commit/ba602f5f4622d6a3e6beb6d2741e3d19efe57ca9), [`b3b2cd2`](https://github.com/mastra-ai/mastra/commit/b3b2cd292a314ed4b422dd055f8d1580c781860a), [`99ca92b`](https://github.com/mastra-ai/mastra/commit/99ca92b60272063a5cb978628e5d286f86c37d4d)]:
17
+ - @mastra/core@1.82.0-alpha.0
18
+ - @mastra/server@1.82.0-alpha.0
19
+ - @mastra/mcp@2.8.0-alpha.0
20
+
3
21
  ## 0.2.0
4
22
 
5
23
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -9,4 +9,5 @@ export { createHttpLoggingTestSuite, type HttpLoggingTestSuiteConfig } from './h
9
9
  export { createBodyLimitTestSuite, type BodyLimitTestSuiteConfig } from './body-limit-test-suite.js';
10
10
  export { createCustomRouteFGATestSuite, type CustomRouteFGATestSuiteConfig } from './custom-route-fga-test-suite.js';
11
11
  export { createDefaultTestContext, createStreamWithSensitiveData, createStreamWithUnserializableChunk, expectSerializedStreamChunks, consumeSSEStream, } from './test-helpers.js';
12
+ export { createKnowledgeRoutesTestSuite, type KnowledgeRoutesTestSuiteConfig } from './knowledge-routes-test-suite.js';
12
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,C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A2B,EAAE,KAAK,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AACtG,OAAO,EAAE,wBAAwB,EAAE,KAAK,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AACjG,OAAO,EAAE,0BAA0B,EAAE,KAAK,0BAA0B,EAAE,MAAM,2BAA2B,CAAC;AACxG,OAAO,EAAE,wBAAwB,EAAE,KAAK,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AAClG,OAAO,EAAE,6BAA6B,EAAE,KAAK,6BAA6B,EAAE,MAAM,+BAA+B,CAAC;AAElH,OAAO,EACL,wBAAwB,EACxB,6BAA6B,EAC7B,mCAAmC,EACnC,4BAA4B,EAC5B,gBAAgB,GACjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,8BAA8B,EAAE,KAAK,8BAA8B,EAAE,MAAM,+BAA+B,CAAC"}
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
2
2
  import { registerApiRoute } from "@mastra/core/server";
3
3
  import { handleError } from "@mastra/server/handlers/error";
4
- import { HTTPException, SERVER_ROUTES, WorkflowRegistry, createRoute, serializeStreamChunk } from "@mastra/server/server-adapter";
4
+ import { HTTPException, KNOWLEDGE_ROUTES, MASTRA_KNOWLEDGE_PRINCIPALS_KEY, SERVER_ROUTES, WorkflowRegistry, createRoute, serializeStreamChunk } from "@mastra/server/server-adapter";
5
5
  import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
6
6
  import * as zod from "zod";
7
7
  import { z } from "zod";
@@ -32,6 +32,7 @@ import { MCPServerBase } from "@mastra/core/mcp";
32
32
  import { RequestContext } from "@mastra/core/request-context";
33
33
  import { standardSchemaToJSONSchema } from "@mastra/core/schema";
34
34
  import { z as z$2 } from "zod/v4";
35
+ import { Knowledge } from "@mastra/core/knowledge";
35
36
  // src/route-test-utils.ts
36
37
  /**
37
38
  * Normalizes a route path to ensure consistent formatting.
@@ -348,6 +349,7 @@ function getDefaultValidPathParams(route) {
348
349
  if (route.path.includes(":serverId")) params.serverId = "test-server-1";
349
350
  if (route.path.includes(":toolId") && route.path.includes("/mcp/")) params.toolId = "getWeather";
350
351
  if (route.path.includes(":eventName")) params.eventName = "ticket.created";
352
+ if (route.path.includes(":taskKey")) params.taskKey = "test-task-key";
351
353
  if (route.path.includes(":workspaceId")) params.workspaceId = "test-workspace";
352
354
  if (route.path.includes(":skillName")) params.skillName = "test-skill";
353
355
  if (route.path.includes(":referencePath")) params.referencePath = "test-reference.md";
@@ -2168,6 +2170,9 @@ function createRouteAdapterTestSuite(config) {
2168
2170
  "/background-tasks/stream",
2169
2171
  "/agents/:agentId/observe",
2170
2172
  "/mcp/:serverId/events/:eventName/stream",
2173
+ "/mcp/:serverId/tasks/find",
2174
+ "/mcp/:serverId/tasks/:taskKey/cancel",
2175
+ "/mcp/:serverId/tasks/:taskKey/input",
2171
2176
  "/agents/:agentId/recover",
2172
2177
  "/agents/:agentId/runs/:runId/abort",
2173
2178
  "/agents/:agentId/plans/file",
@@ -5632,6 +5637,103 @@ function createCustomRouteFGATestSuite(config) {
5632
5637
  });
5633
5638
  });
5634
5639
  }
5635
- export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createRouteAdapterTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
5640
+ // src/knowledge-routes-test-suite.ts
5641
+ /**
5642
+ * The opt-in Knowledge routes on every adapter: RBAC `knowledge:read` gates them, the Knowledge principals
5643
+ * `authorize` sets through `context.get('requestContext')` reach the handler (no mapping is refused), and a
5644
+ * repeated query param (`scopeId`) arrives as a list.
5645
+ */
5646
+ function createKnowledgeRoutesTestSuite(config) {
5647
+ const { suiteName = "Knowledge routes", startServer } = config;
5648
+ describe(suiteName, () => {
5649
+ let close;
5650
+ afterEach(async () => {
5651
+ await close?.();
5652
+ close = void 0;
5653
+ });
5654
+ const setup = async () => {
5655
+ const knowledge = new Knowledge({
5656
+ id: "kb",
5657
+ scopes: {},
5658
+ storage: new InMemoryStore({ id: "kb" })
5659
+ });
5660
+ const store = await knowledge.getStorage();
5661
+ const provenance = { author: "test" };
5662
+ const scopes = {};
5663
+ for (const [team, page] of [
5664
+ ["support", "Payments runbook"],
5665
+ ["eng", "Payments service"],
5666
+ ["sales", "Payments pricing"]
5667
+ ]) {
5668
+ const { scope } = await store.materializeScope({
5669
+ address: `team:${team}`,
5670
+ declaration: "team:$teamId",
5671
+ name: team,
5672
+ provenance
5673
+ });
5674
+ scopes[team] = scope.id;
5675
+ await store.createPage({
5676
+ parentId: scope.id,
5677
+ name: page,
5678
+ provenance
5679
+ });
5680
+ }
5681
+ const permissions = {
5682
+ "reader-token": ["knowledge:read"],
5683
+ "unmapped-token": ["knowledge:read"],
5684
+ "other-token": []
5685
+ };
5686
+ const mastra = new Mastra({
5687
+ logger: false,
5688
+ knowledge: { main: knowledge },
5689
+ server: { apiRoutes: [...KNOWLEDGE_ROUTES] }
5690
+ });
5691
+ const originalGetServer = mastra.getServer.bind(mastra);
5692
+ mastra.getServer = () => ({
5693
+ ...originalGetServer(),
5694
+ rbac: {
5695
+ getRoles: async () => ["member"],
5696
+ getPermissions: async (user) => permissions[user.id] ?? []
5697
+ },
5698
+ auth: {
5699
+ authenticateToken: async (token) => token in permissions ? { id: token } : null,
5700
+ authorize: async (_path, _method, user, context) => {
5701
+ if (user.id === "reader-token") context.get("requestContext").set(MASTRA_KNOWLEDGE_PRINCIPALS_KEY, "all");
5702
+ return true;
5703
+ }
5704
+ }
5705
+ });
5706
+ const server = await startServer(mastra);
5707
+ close = server.close;
5708
+ const get = (path, token) => server.request(`/api/knowledge/main${path}`, token ? { headers: { authorization: `Bearer ${token}` } } : {});
5709
+ return {
5710
+ get,
5711
+ scopes
5712
+ };
5713
+ };
5714
+ it("serves a reader authorize mapped to Knowledge principals and refuses everyone else", async () => {
5715
+ const { get } = await setup();
5716
+ const roots = await get("/tree", "reader-token");
5717
+ expect(roots.status).toBe(200);
5718
+ const body = await roots.json();
5719
+ expect(body.pages.map((page) => page.name).sort()).toEqual([
5720
+ "eng",
5721
+ "sales",
5722
+ "support"
5723
+ ]);
5724
+ expect((await get("/tree", "unmapped-token")).status).toBe(403);
5725
+ expect((await get("/tree", "other-token")).status).toBe(403);
5726
+ expect((await get("/tree")).status).toBe(401);
5727
+ });
5728
+ it("reads a repeated scopeId param as a list", async () => {
5729
+ const { get, scopes } = await setup();
5730
+ const res = await get(`/search?q=payments&scopeId=${scopes.support}&scopeId=${scopes.eng}`, "reader-token");
5731
+ expect(res.status).toBe(200);
5732
+ const body = await res.json();
5733
+ expect(body.results.map((hit) => hit.page.name).sort()).toEqual(["Payments runbook", "Payments service"]);
5734
+ });
5735
+ });
5736
+ }
5737
+ export { consumeSSEStream, createBodyLimitTestSuite, createCustomRouteFGATestSuite, createDefaultTestContext, createDurableDisconnectTestSuite, createHttpLoggingTestSuite, createKnowledgeRoutesTestSuite, createMCPRouteTestSuite, createMCPTransportTestSuite, createMultipartTestSuite, createRouteAdapterTestSuite, createStreamWithSensitiveData, createStreamWithUnserializableChunk, createStructuredWakeTestSuite, expectSerializedStreamChunks };
5636
5738
 
5637
5739
  //# sourceMappingURL=index.js.map