File size: 1,312 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { OpenAPIHono } from "@hono/zod-openapi";
import { createMiddleware } from "hono/factory";
import type { Context } from "../../rest/types";

export interface TestAppOptions {
  teamId?: string;
  userId?: string;
}

/**
 * Creates an isolated Hono test app with mocked auth middleware.
 * Use this for REST API integration tests.
 */
export function createTestApp(options: TestAppOptions = {}) {
  const { teamId = "test-team-id", userId = "test-user-id" } = options;

  const app = new OpenAPIHono<Context>();

  // Mock auth middleware - inject test team/user IDs
  app.use(
    "*",
    createMiddleware<Context>(async (c, next) => {
      c.set("teamId", teamId);
      c.set("session", {
        user: {
          id: userId,
          email: "test@example.com",
        },
      });
      c.set("db", {} as Context["Variables"]["db"]); // Mock DB instance - actual queries are mocked at module level
      // Set all scopes for testing (grants full access)
      c.set("scopes", [
        "transactions.read",
        "transactions.write",
        "invoices.read",
        "invoices.write",
        "customers.read",
        "customers.write",
        "bank_accounts.read",
        "bank_accounts.write",
      ] as Context["Variables"]["scopes"]);
      await next();
    }),
  );

  return app;
}