|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | +/// <reference types="@types/jest" /> |
| 4 | +import { loadSchema } from '@zenstackhq/testtools'; |
| 5 | +import 'isomorphic-fetch'; |
| 6 | +import path from 'path'; |
| 7 | +import superjson from 'superjson'; |
| 8 | +import Rest from '../../src/api/rest'; |
| 9 | +import { createHonoHandler } from '../../src/hono'; |
| 10 | +import { makeUrl, schema } from '../utils'; |
| 11 | +import { Hono, MiddlewareHandler } from 'hono'; |
| 12 | + |
| 13 | +describe('Hono adapter tests - rpc handler', () => { |
| 14 | + it('run hooks regular json', async () => { |
| 15 | + const { prisma, zodSchemas } = await loadSchema(schema); |
| 16 | + |
| 17 | + const handler = await createHonoApp(createHonoHandler({ getPrisma: () => prisma, zodSchemas })); |
| 18 | + |
| 19 | + let r = await handler(makeRequest('GET', makeUrl('/api/post/findMany', { where: { id: { equals: '1' } } }))); |
| 20 | + expect(r.status).toBe(200); |
| 21 | + expect((await unmarshal(r)).data).toHaveLength(0); |
| 22 | + |
| 23 | + r = await handler( |
| 24 | + makeRequest('POST', '/api/user/create', { |
| 25 | + include: { posts: true }, |
| 26 | + data: { |
| 27 | + id: 'user1', |
| 28 | + email: 'user1@abc.com', |
| 29 | + posts: { |
| 30 | + create: [ |
| 31 | + { title: 'post1', published: true, viewCount: 1 }, |
| 32 | + { title: 'post2', published: false, viewCount: 2 }, |
| 33 | + ], |
| 34 | + }, |
| 35 | + }, |
| 36 | + }) |
| 37 | + ); |
| 38 | + expect(r.status).toBe(201); |
| 39 | + expect((await unmarshal(r)).data).toMatchObject({ |
| 40 | + email: 'user1@abc.com', |
| 41 | + posts: expect.arrayContaining([ |
| 42 | + expect.objectContaining({ title: 'post1' }), |
| 43 | + expect.objectContaining({ title: 'post2' }), |
| 44 | + ]), |
| 45 | + }); |
| 46 | + |
| 47 | + r = await handler(makeRequest('GET', makeUrl('/api/post/findMany'))); |
| 48 | + expect(r.status).toBe(200); |
| 49 | + expect((await unmarshal(r)).data).toHaveLength(2); |
| 50 | + |
| 51 | + r = await handler(makeRequest('GET', makeUrl('/api/post/findMany', { where: { viewCount: { gt: 1 } } }))); |
| 52 | + expect(r.status).toBe(200); |
| 53 | + expect((await unmarshal(r)).data).toHaveLength(1); |
| 54 | + |
| 55 | + r = await handler( |
| 56 | + makeRequest('PUT', '/api/user/update', { where: { id: 'user1' }, data: { email: 'user1@def.com' } }) |
| 57 | + ); |
| 58 | + expect(r.status).toBe(200); |
| 59 | + expect((await unmarshal(r)).data.email).toBe('user1@def.com'); |
| 60 | + |
| 61 | + r = await handler(makeRequest('GET', makeUrl('/api/post/count', { where: { viewCount: { gt: 1 } } }))); |
| 62 | + expect(r.status).toBe(200); |
| 63 | + expect((await unmarshal(r)).data).toBe(1); |
| 64 | + |
| 65 | + r = await handler(makeRequest('GET', makeUrl('/api/post/aggregate', { _sum: { viewCount: true } }))); |
| 66 | + expect(r.status).toBe(200); |
| 67 | + expect((await unmarshal(r)).data._sum.viewCount).toBe(3); |
| 68 | + |
| 69 | + r = await handler( |
| 70 | + makeRequest('GET', makeUrl('/api/post/groupBy', { by: ['published'], _sum: { viewCount: true } })) |
| 71 | + ); |
| 72 | + expect(r.status).toBe(200); |
| 73 | + expect((await unmarshal(r)).data).toEqual( |
| 74 | + expect.arrayContaining([ |
| 75 | + expect.objectContaining({ published: true, _sum: { viewCount: 1 } }), |
| 76 | + expect.objectContaining({ published: false, _sum: { viewCount: 2 } }), |
| 77 | + ]) |
| 78 | + ); |
| 79 | + |
| 80 | + r = await handler(makeRequest('DELETE', makeUrl('/api/user/deleteMany', { where: { id: 'user1' } }))); |
| 81 | + expect(r.status).toBe(200); |
| 82 | + expect((await unmarshal(r)).data.count).toBe(1); |
| 83 | + }); |
| 84 | + |
| 85 | + it('custom load path', async () => { |
| 86 | + const { prisma, projectDir } = await loadSchema(schema, { output: './zen' }); |
| 87 | + |
| 88 | + const handler = await createHonoApp( |
| 89 | + createHonoHandler({ |
| 90 | + getPrisma: () => prisma, |
| 91 | + modelMeta: require(path.join(projectDir, './zen/model-meta')).default, |
| 92 | + zodSchemas: require(path.join(projectDir, './zen/zod')), |
| 93 | + }) |
| 94 | + ); |
| 95 | + |
| 96 | + const r = await handler( |
| 97 | + makeRequest('POST', '/api/user/create', { |
| 98 | + include: { posts: true }, |
| 99 | + data: { |
| 100 | + id: 'user1', |
| 101 | + email: 'user1@abc.com', |
| 102 | + posts: { |
| 103 | + create: [ |
| 104 | + { title: 'post1', published: true, viewCount: 1 }, |
| 105 | + { title: 'post2', published: false, viewCount: 2 }, |
| 106 | + ], |
| 107 | + }, |
| 108 | + }, |
| 109 | + }) |
| 110 | + ); |
| 111 | + expect(r.status).toBe(201); |
| 112 | + }); |
| 113 | +}); |
| 114 | + |
| 115 | +describe('Hono adapter tests - rest handler', () => { |
| 116 | + it('run hooks', async () => { |
| 117 | + const { prisma, modelMeta, zodSchemas } = await loadSchema(schema); |
| 118 | + |
| 119 | + const handler = await createHonoApp( |
| 120 | + createHonoHandler({ |
| 121 | + getPrisma: () => prisma, |
| 122 | + handler: Rest({ endpoint: 'http://localhost/api' }), |
| 123 | + modelMeta, |
| 124 | + zodSchemas, |
| 125 | + }) |
| 126 | + ); |
| 127 | + |
| 128 | + let r = await handler(makeRequest('GET', makeUrl('/api/post/1'))); |
| 129 | + expect(r.status).toBe(404); |
| 130 | + |
| 131 | + r = await handler( |
| 132 | + makeRequest('POST', '/api/user', { |
| 133 | + data: { |
| 134 | + type: 'user', |
| 135 | + attributes: { id: 'user1', email: 'user1@abc.com' }, |
| 136 | + }, |
| 137 | + }) |
| 138 | + ); |
| 139 | + expect(r.status).toBe(201); |
| 140 | + expect(await unmarshal(r)).toMatchObject({ |
| 141 | + data: { |
| 142 | + id: 'user1', |
| 143 | + attributes: { |
| 144 | + email: 'user1@abc.com', |
| 145 | + }, |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + r = await handler(makeRequest('GET', makeUrl('/api/user?filter[id]=user1'))); |
| 150 | + expect(r.status).toBe(200); |
| 151 | + expect((await unmarshal(r)).data).toHaveLength(1); |
| 152 | + |
| 153 | + r = await handler(makeRequest('GET', makeUrl('/api/user?filter[id]=user2'))); |
| 154 | + expect(r.status).toBe(200); |
| 155 | + expect((await unmarshal(r)).data).toHaveLength(0); |
| 156 | + |
| 157 | + r = await handler(makeRequest('GET', makeUrl('/api/user?filter[id]=user1&filter[email]=xyz'))); |
| 158 | + expect(r.status).toBe(200); |
| 159 | + expect((await unmarshal(r)).data).toHaveLength(0); |
| 160 | + |
| 161 | + r = await handler( |
| 162 | + makeRequest('PUT', makeUrl('/api/user/user1'), { |
| 163 | + data: { type: 'user', attributes: { email: 'user1@def.com' } }, |
| 164 | + }) |
| 165 | + ); |
| 166 | + expect(r.status).toBe(200); |
| 167 | + expect((await unmarshal(r)).data.attributes.email).toBe('user1@def.com'); |
| 168 | + |
| 169 | + r = await handler(makeRequest('DELETE', makeUrl(makeUrl('/api/user/user1')))); |
| 170 | + expect(r.status).toBe(204); |
| 171 | + expect(await prisma.user.findMany()).toHaveLength(0); |
| 172 | + }); |
| 173 | +}); |
| 174 | + |
| 175 | +function makeRequest(method: string, path: string, body?: any) { |
| 176 | + const payload = body ? JSON.stringify(body) : undefined; |
| 177 | + return new Request(`http://localhost${path}`, { method, body: payload }); |
| 178 | +} |
| 179 | + |
| 180 | +async function unmarshal(r: Response, useSuperJson = false) { |
| 181 | + const text = await r.text(); |
| 182 | + return (useSuperJson ? superjson.parse(text) : JSON.parse(text)) as any; |
| 183 | +} |
| 184 | + |
| 185 | +async function createHonoApp(middleware: MiddlewareHandler) { |
| 186 | + const app = new Hono(); |
| 187 | + |
| 188 | + app.use('/api/*', middleware); |
| 189 | + |
| 190 | + return app.fetch; |
| 191 | +} |
0 commit comments