|
1 | 1 | // deno-lint-ignore-file no-explicit-any |
2 | | -import type { Yelix } from '@/src/core/Yelix.ts'; |
3 | | -import { Store } from '@/src/test/store.ts'; |
4 | | - |
5 | | -type CleanupFuntion = (store?: Store<string | any>) => void | Promise<void>; |
| 2 | +import type { Yelix } from "@/src/core/Yelix.ts"; |
6 | 3 |
|
7 | 4 | type Config = { |
8 | 5 | method: string; |
9 | 6 | } & RequestInit; |
10 | 7 |
|
11 | 8 | class YelixTestClient { |
12 | 9 | private app: Yelix | null = null; |
13 | | - private dbCleanups: { |
14 | | - title: string; |
15 | | - fn: CleanupFuntion; |
16 | | - }[] = []; |
17 | | - |
18 | | - constructor() { |
19 | | - this.dbCleanups = []; |
20 | | - } |
21 | | - public GlobalStore = new Store<string | any>(); |
22 | 10 |
|
23 | | - setApp(app: Yelix) { |
24 | | - this.app = app; |
25 | | - } |
| 11 | + debugRoutes() { |
| 12 | + const app: any = this.app; |
| 13 | + if (!app) { |
| 14 | + throw new Error("No app found"); |
| 15 | + } |
| 16 | + console.log("=== DEBUG ROUTE INFORMATION ==="); |
| 17 | + console.log("Endpoints registered in Yelix:", app.endpointList); |
26 | 18 |
|
27 | | - cleanup(title: string, fn: CleanupFuntion) { |
28 | | - this.dbCleanups.push({ title, fn }); |
| 19 | + // Try to access Hono's internal route registry |
| 20 | + if (app.app && typeof app.app.routes === "function") { |
| 21 | + console.log("Hono routes:", app.app.routes()); |
| 22 | + } else if (app.app && app.app._handlers) { |
| 23 | + console.log("Hono handlers:", app.app._handlers); |
| 24 | + } else { |
| 25 | + console.log("Unable to access Hono route registry"); |
| 26 | + console.log("app.app structure:", Object.keys(app.app || {})); |
| 27 | + } |
| 28 | + |
| 29 | + console.log("=== END DEBUG INFO ==="); |
29 | 30 | } |
| 31 | +} |
30 | 32 |
|
31 | | - async afterAll() { |
32 | | - console.log('Cleaning up...', this.dbCleanups); |
33 | | - for (const cleanup of this.dbCleanups) { |
34 | | - console.log(`Cleaning up: ${cleanup.title}`); |
35 | | - console.group(); |
36 | | - await cleanup.fn(this.GlobalStore); |
37 | | - console.groupEnd(); |
38 | | - } |
| 33 | +async function request(app: Yelix, path: string, config: Config): Promise<any> { |
| 34 | + const hono = app.app; |
| 35 | + if (!hono) { |
| 36 | + throw new Error("No app found"); |
39 | 37 | } |
40 | 38 |
|
41 | | - async request(path: string, config?: Config) { |
42 | | - const hono = this.app?.app; |
43 | | - if (!hono) { |
44 | | - throw new Error('No app found'); |
45 | | - } |
| 39 | + const res = await hono.request(path, config); |
46 | 40 |
|
47 | | - const res = await hono.request(path, config); |
| 41 | + if (!res) { |
| 42 | + throw new Error("No response"); |
| 43 | + } |
48 | 44 |
|
49 | | - if (!res) { |
50 | | - throw new Error('No response'); |
51 | | - } |
52 | | - |
53 | | - let response; |
54 | | - try { |
55 | | - const clone = res.clone(); |
56 | | - response = await clone.json(); |
57 | | - } catch { |
58 | | - response = await res.text(); |
59 | | - } |
60 | | - |
61 | | - console.log( |
62 | | - 'REQUEST ', |
63 | | - `${config?.method} ${path}`, |
64 | | - config?.body && typeof config.body === 'string' |
65 | | - ? JSON.parse(config?.body) |
66 | | - : {} |
67 | | - ); |
68 | | - console.log('RESPONSE ', response); |
69 | | - |
70 | | - return { |
71 | | - req: res, |
72 | | - res: { |
73 | | - responseType: typeof response === 'string' ? 'text' : 'json', |
74 | | - text: response, |
75 | | - json: response |
76 | | - }, |
77 | | - }; |
| 45 | + let response; |
| 46 | + try { |
| 47 | + const clone = res.clone(); |
| 48 | + response = await clone.json(); |
| 49 | + } catch { |
| 50 | + response = await res.text(); |
78 | 51 | } |
79 | | - |
| 52 | + |
| 53 | + console.log( |
| 54 | + "REQUEST ", |
| 55 | + `${config?.method} ${path}`, |
| 56 | + config?.body && typeof config.body === "string" |
| 57 | + ? JSON.parse(config?.body) |
| 58 | + : {}, |
| 59 | + ); |
| 60 | + console.log("RESPONSE ", response); |
| 61 | + |
| 62 | + return { |
| 63 | + req: res, |
| 64 | + res: { |
| 65 | + responseType: typeof response === "string" ? "text" : "json", |
| 66 | + text: response, |
| 67 | + json: response, |
| 68 | + }, |
| 69 | + }; |
80 | 70 | } |
81 | 71 |
|
82 | | -export { YelixTestClient }; |
| 72 | +export { request, YelixTestClient }; |
0 commit comments