@mastra/server-adapters-test-suite 0.3.0-alpha.1 → 0.3.0-alpha.3

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,27 @@
1
1
  # @mastra/server-adapters-test-suite
2
2
 
3
+ ## 0.3.0-alpha.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Added stream disconnect cases that check a stream failing partway still delivers what was sent before the failure and ends the response. Adapters that still drop the connection for `datastream-response` routes can pin those cases with the `datastreamUpstreamErrorDropsConnection` option. ([#29944](https://github.com/mastra-ai/mastra/pull/29944))
8
+
9
+ - Updated dependencies [[`749c09c`](https://github.com/mastra-ai/mastra/commit/749c09cf9120ff36cfa965444e2f0472b37c00ee), [`931c2bb`](https://github.com/mastra-ai/mastra/commit/931c2bb605a2a924844a4b75b2b8319673cc911b), [`22b24cc`](https://github.com/mastra-ai/mastra/commit/22b24cc40ad2923b22e11cfa2b5264b4ae0f1ffc), [`5810337`](https://github.com/mastra-ai/mastra/commit/5810337c710fdba9f3e1c3fe982eec8907f2f444), [`ec60314`](https://github.com/mastra-ai/mastra/commit/ec603149745f7592d3d559447f69c65277580c31), [`c3ef10e`](https://github.com/mastra-ai/mastra/commit/c3ef10ee3094f9d30d2aca6d59ef224b0340d6ba), [`df4b70b`](https://github.com/mastra-ai/mastra/commit/df4b70bd4185ce7c56699aac76321e64e59d7cdd), [`6ef0741`](https://github.com/mastra-ai/mastra/commit/6ef07410a40f69ebe316db2bd3190cd43177a46a), [`c3ef10e`](https://github.com/mastra-ai/mastra/commit/c3ef10ee3094f9d30d2aca6d59ef224b0340d6ba), [`01b2b95`](https://github.com/mastra-ai/mastra/commit/01b2b958af4a2a609e20ae2e39c0201383631760), [`e344f15`](https://github.com/mastra-ai/mastra/commit/e344f157171c02d43c41bd6f4a4487e0215726af), [`5de12fc`](https://github.com/mastra-ai/mastra/commit/5de12fce63626abb87fcb09d7628951e7ad96b4a), [`94bf876`](https://github.com/mastra-ai/mastra/commit/94bf8761f4db6650c95f59fc51481e662c0d3436), [`6b8f215`](https://github.com/mastra-ai/mastra/commit/6b8f215e1fadcba2d0e3ad6a1a9222f8f2c6da79), [`ab4c3fa`](https://github.com/mastra-ai/mastra/commit/ab4c3fafdbd5ff84dde97fa936e1e55d70ef3e4b), [`223204c`](https://github.com/mastra-ai/mastra/commit/223204cd3eb1b9e54a7be0130d2bf2e6c957b9cf), [`e349d05`](https://github.com/mastra-ai/mastra/commit/e349d05edd35a1889a4122f296693a15a8125a27)]:
10
+ - @mastra/core@1.83.0-alpha.6
11
+ - @mastra/mcp@2.9.0-alpha.3
12
+ - @mastra/server@1.83.0-alpha.6
13
+
14
+ ## 0.3.0-alpha.2
15
+
16
+ ### Patch Changes
17
+
18
+ - Added conformance tests for custom route auth: auth and FGA checks must follow the route that handles the request, including its path params, for overlapping routes, `HEAD` requests, letter case and percent-encoded paths. ([#29325](https://github.com/mastra-ai/mastra/pull/29325))
19
+
20
+ - Updated dependencies [[`ce86e53`](https://github.com/mastra-ai/mastra/commit/ce86e5327a0e835e26792e238dacc2c15a777bbf), [`ce76df9`](https://github.com/mastra-ai/mastra/commit/ce76df95d5d942940de7d5453fcbd38b7df5ea83), [`bfda77d`](https://github.com/mastra-ai/mastra/commit/bfda77de9773c383f23101cde8f4ee12af8bff19), [`66c620c`](https://github.com/mastra-ai/mastra/commit/66c620c086cd4a5d12347a832057290a19cb9db2), [`964e1ba`](https://github.com/mastra-ai/mastra/commit/964e1baa24a5a383c86b9f3aa639d488e8742b13), [`311d434`](https://github.com/mastra-ai/mastra/commit/311d4342cc61e9f5a621625e28e483a892fda6bc), [`9730315`](https://github.com/mastra-ai/mastra/commit/9730315a2fddf51580599030916d43b17e1ca553), [`bf9212b`](https://github.com/mastra-ai/mastra/commit/bf9212b308c1d418cac64d54aa380dac324db1bd), [`92f9771`](https://github.com/mastra-ai/mastra/commit/92f9771a0fefaf6e23541ac702850da63bfe4ef9)]:
21
+ - @mastra/core@1.83.0-alpha.5
22
+ - @mastra/server@1.83.0-alpha.5
23
+ - @mastra/mcp@2.9.0-alpha.2
24
+
3
25
  ## 0.3.0-alpha.1
4
26
 
5
27
  ### Minor Changes
@@ -1 +1 @@
1
- {"version":3,"file":"custom-route-fga-test-suite.d.ts","sourceRoot":"","sources":["../src/custom-route-fga-test-suite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAKtC;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C,8BAA8B;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;QACvC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;QACjE,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC7B,CAAC,CAAC;CACJ;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,6BAA6B,QA2ElF"}
1
+ {"version":3,"file":"custom-route-fga-test-suite.d.ts","sourceRoot":"","sources":["../src/custom-route-fga-test-suite.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAKtC;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C,8BAA8B;IAC9B,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;OAGG;IACH,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,OAAO,CAAC;QACvC,OAAO,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;QACjE,KAAK,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;KAC7B,CAAC,CAAC;CACJ;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,CAAC,MAAM,EAAE,6BAA6B,QAoOlF"}
package/dist/index.js CHANGED
@@ -1783,14 +1783,20 @@ function createRouteAdapterTestSuite(config) {
1783
1783
  let context;
1784
1784
  let app;
1785
1785
  let setup;
1786
+ let netlifyProviders;
1786
1787
  beforeEach(async () => {
1787
1788
  if (createTestContext) {
1788
1789
  const result = createTestContext();
1789
1790
  context = result instanceof Promise ? await result : result;
1790
1791
  } else context = await createDefaultTestContext();
1792
+ const netlify = context.mastra.listGateways()?.netlify;
1793
+ netlifyProviders = netlify ? vi.spyOn(netlify, "fetchProviders").mockResolvedValue({}) : void 0;
1791
1794
  setup = await setupAdapter(context);
1792
1795
  app = setup.app;
1793
1796
  });
1797
+ afterEach(() => {
1798
+ netlifyProviders?.mockRestore();
1799
+ });
1794
1800
  if (config.supportsPostQueryRequestContext) it("merges query requestContext into POST route handlers", async () => {
1795
1801
  await setup.adapter.registerRoute(app, {
1796
1802
  method: "POST",
@@ -4346,7 +4352,7 @@ function createStreamDisconnectTestSuite(config) {
4346
4352
  await close?.();
4347
4353
  close = void 0;
4348
4354
  });
4349
- async function setup({ responseType, chunks = 60, chunkBytes = 16, paceMs = 10, holdUntilDisconnect = false, compress = false }) {
4355
+ async function setup({ responseType, chunks = 60, chunkBytes = 16, paceMs = 10, holdUntilDisconnect = false, compress = false, errorAfter }) {
4350
4356
  const state = {
4351
4357
  pulls: 0,
4352
4358
  canceled: false
@@ -4370,8 +4376,10 @@ function createStreamDisconnectTestSuite(config) {
4370
4376
  const body = new ReadableStream({
4371
4377
  async pull(controller) {
4372
4378
  if (state.pulls >= chunks) return controller.close();
4373
- state.pulls++;
4379
+ const failing = errorAfter !== void 0 && state.pulls >= errorAfter;
4380
+ if (!failing) state.pulls++;
4374
4381
  if (paceMs) await sleep(paceMs);
4382
+ if (failing) return controller.error(/* @__PURE__ */ new Error("upstream failed"));
4375
4383
  controller.enqueue(responseType === "stream" ? {
4376
4384
  type: "chunk",
4377
4385
  n: state.pulls,
@@ -4468,6 +4476,18 @@ function createStreamDisconnectTestSuite(config) {
4468
4476
  expect(text.match(/"n":\d+|chunk-\d+/g)).toHaveLength(20);
4469
4477
  expect(state.canceled, "upstream cancel()").toBe(false);
4470
4478
  });
4479
+ (responseType === "datastream-response" && config.datastreamUpstreamErrorDropsConnection ? it.fails : it).each([0, 20])("delivers the chunks sent before an upstream error and ends the response cleanly (error after %ims)", async (paceMs) => {
4480
+ const { url } = await setup({
4481
+ responseType,
4482
+ chunks: 100,
4483
+ errorAfter: 3,
4484
+ paceMs
4485
+ });
4486
+ const response = await post(url);
4487
+ const text = await response.text();
4488
+ expect(response.status).toBe(200);
4489
+ expect(text.match(/"n":\d+|chunk-\d+/g)).toHaveLength(3);
4490
+ });
4471
4491
  it("pauses reading the upstream while a connected client is not reading, and cancels it once the client leaves", async () => {
4472
4492
  const { url, state } = await setup({
4473
4493
  responseType,
@@ -6357,6 +6377,148 @@ function createCustomRouteFGATestSuite(config) {
6357
6377
  expect(response.status).toBe(403);
6358
6378
  expect(checkedIds(check)).toEqual(["locked"]);
6359
6379
  });
6380
+ it("authenticates overlapping routes the same way they are dispatched", async () => {
6381
+ const mastra = new Mastra({
6382
+ logger: false,
6383
+ server: { apiRoutes: [registerApiRoute("/admin/:id", {
6384
+ method: "GET",
6385
+ handler: async (c) => c.json({ route: "admin" })
6386
+ }), registerApiRoute("/admin/public", {
6387
+ method: "GET",
6388
+ requiresAuth: false,
6389
+ handler: async (c) => c.json({ route: "public" })
6390
+ })] }
6391
+ });
6392
+ const originalGetServer = mastra.getServer.bind(mastra);
6393
+ mastra.getServer = () => ({
6394
+ ...originalGetServer(),
6395
+ auth: {
6396
+ authenticateToken: async (token) => token === "valid-token" ? { id: "user-1" } : null,
6397
+ authorize: async () => true
6398
+ }
6399
+ });
6400
+ const server = await startServer(mastra);
6401
+ close = server.close;
6402
+ expect((await server.request("/admin/public")).status).toBe(401);
6403
+ const authed = await server.request("/admin/public", { headers: { authorization: "Bearer valid-token" } });
6404
+ expect(authed.status).toBe(200);
6405
+ expect(await authed.json()).toEqual({ route: "admin" });
6406
+ });
6407
+ const startWithAuth = async (apiRoutes) => {
6408
+ const mastra = new Mastra({
6409
+ logger: false,
6410
+ server: { apiRoutes }
6411
+ });
6412
+ const originalGetServer = mastra.getServer.bind(mastra);
6413
+ mastra.getServer = () => ({
6414
+ ...originalGetServer(),
6415
+ auth: {
6416
+ authenticateToken: async (token) => token === "valid-token" ? { id: "user-1" } : null,
6417
+ authorize: async () => true
6418
+ }
6419
+ });
6420
+ const server = await startServer(mastra);
6421
+ close = server.close;
6422
+ return server;
6423
+ };
6424
+ const authed = { headers: { authorization: "Bearer valid-token" } };
6425
+ it("does not run a protected route shadowed by an earlier public one without auth", async () => {
6426
+ const protectedHandler = vi.fn(async (c) => c.json({ route: "protected" }));
6427
+ const response = await (await startWithAuth([registerApiRoute("/admin/:id", {
6428
+ method: "GET",
6429
+ requiresAuth: false,
6430
+ handler: async (c) => c.json({ route: "param" })
6431
+ }), registerApiRoute("/admin/public", {
6432
+ method: "GET",
6433
+ handler: protectedHandler
6434
+ })])).request("/admin/public");
6435
+ expect(response.status).toBe(200);
6436
+ expect(await response.json()).toEqual({ route: "param" });
6437
+ expect(protectedHandler).not.toHaveBeenCalled();
6438
+ });
6439
+ it("requires auth for HEAD requests to a protected GET route", async () => {
6440
+ const protectedHandler = vi.fn(async (c) => c.json({ secret: true }));
6441
+ const server = await startWithAuth([registerApiRoute("/secret", {
6442
+ method: "GET",
6443
+ handler: protectedHandler
6444
+ })]);
6445
+ expect((await server.request("/secret", { method: "HEAD" })).status).toBe(401);
6446
+ expect(protectedHandler).not.toHaveBeenCalled();
6447
+ expect((await server.request("/secret", {
6448
+ method: "HEAD",
6449
+ ...authed
6450
+ })).status).toBe(200);
6451
+ });
6452
+ it("matches routes case-sensitively when deciding auth", async () => {
6453
+ const protectedHandler = vi.fn(async (c) => c.json({ route: "protected" }));
6454
+ const server = await startWithAuth([registerApiRoute("/Admin", {
6455
+ method: "GET",
6456
+ requiresAuth: false,
6457
+ handler: async (c) => c.json({ route: "public" })
6458
+ }), registerApiRoute("/admin", {
6459
+ method: "GET",
6460
+ handler: protectedHandler
6461
+ })]);
6462
+ expect((await server.request("/admin")).status).toBe(401);
6463
+ expect(protectedHandler).not.toHaveBeenCalled();
6464
+ const publicResponse = await server.request("/Admin");
6465
+ expect(publicResponse.status).toBe(200);
6466
+ expect(await publicResponse.json()).toEqual({ route: "public" });
6467
+ });
6468
+ it("checks auth for percent-encoded paths against the route they decode to", async () => {
6469
+ const protectedHandler = vi.fn(async (c) => c.json({ route: "admin" }));
6470
+ const server = await startWithAuth([registerApiRoute("/admin", {
6471
+ method: "GET",
6472
+ handler: protectedHandler
6473
+ }), registerApiRoute("/:page", {
6474
+ method: "GET",
6475
+ requiresAuth: false,
6476
+ handler: async (c) => c.json({ route: "page" })
6477
+ })]);
6478
+ const response = await server.request("/%61dmin");
6479
+ expect(protectedHandler).not.toHaveBeenCalled();
6480
+ expect(response.status).not.toBe(200);
6481
+ expect((await server.request("/about")).status).toBe(200);
6482
+ });
6483
+ it("checks FGA with the params of the route that handles an overlapping path", async () => {
6484
+ const check = vi.fn(async (_user, { resource }) => resource.id === "open");
6485
+ const fga = {
6486
+ check,
6487
+ require: vi.fn(),
6488
+ filterAccessible: vi.fn()
6489
+ };
6490
+ const mastra = new Mastra({
6491
+ logger: false,
6492
+ server: {
6493
+ fga,
6494
+ apiRoutes: [registerApiRoute("/docs/:docId", {
6495
+ method: "GET",
6496
+ fga: {
6497
+ resourceType: "doc",
6498
+ resourceIdParam: "docId",
6499
+ permission: "docs:read"
6500
+ },
6501
+ handler: async (c) => c.json({ docId: c.req.param("docId") })
6502
+ }), registerApiRoute("/docs/open", {
6503
+ method: "GET",
6504
+ handler: async (c) => c.json({ route: "static" })
6505
+ })]
6506
+ }
6507
+ });
6508
+ const originalGetServer = mastra.getServer.bind(mastra);
6509
+ mastra.getServer = () => ({
6510
+ ...originalGetServer(),
6511
+ auth: {
6512
+ authenticateToken: async (token) => token === "valid-token" ? { id: "user-1" } : null,
6513
+ authorize: async () => true
6514
+ }
6515
+ });
6516
+ const server = await startServer(mastra);
6517
+ close = server.close;
6518
+ const response = await server.request("/docs/open", { headers: { authorization: "Bearer valid-token" } });
6519
+ expect(response.status).toBe(200);
6520
+ expect(checkedIds(check)).toEqual(["open"]);
6521
+ });
6360
6522
  });
6361
6523
  }
6362
6524
  // src/knowledge-routes-test-suite.ts