|
| 1 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | +import { loadSchema } from '@zenstackhq/testtools'; |
| 4 | +import path from 'path'; |
| 5 | +import Rest from '../../src/api/rest'; |
| 6 | +import { TanStackStartHandler, TanStackStartOptions } from '../../src/tanstack-start'; |
| 7 | + |
| 8 | +function makeRequest(method: string, url: string, body?: any): Request { |
| 9 | + const payload = body ? JSON.stringify(body) : undefined; |
| 10 | + return new Request(url, { method, body: payload }); |
| 11 | +} |
| 12 | + |
| 13 | +async function unmarshal(response: Response): Promise<any> { |
| 14 | + const text = await response.text(); |
| 15 | + return JSON.parse(text); |
| 16 | +} |
| 17 | + |
| 18 | +interface TestClient { |
| 19 | + get: () => Promise<{ status: number; body: any }>; |
| 20 | + post: () => { send: (data: any) => Promise<{ status: number; body: any }> }; |
| 21 | + put: () => { send: (data: any) => Promise<{ status: number; body: any }> }; |
| 22 | + del: () => Promise<{ status: number; body: any }>; |
| 23 | +} |
| 24 | + |
| 25 | +function makeTestClient(apiPath: string, options: TanStackStartOptions, qArg?: unknown, otherArgs?: any): TestClient { |
| 26 | + const pathParts = apiPath.split('/').filter((p) => p); |
| 27 | + const path = pathParts.join('/'); |
| 28 | + |
| 29 | + const handler = TanStackStartHandler(options); |
| 30 | + |
| 31 | + const params = { |
| 32 | + _splat: path, |
| 33 | + ...otherArgs, |
| 34 | + }; |
| 35 | + |
| 36 | + const buildUrl = (method: string) => { |
| 37 | + const baseUrl = `http://localhost${apiPath}`; |
| 38 | + if (method === 'GET' || method === 'DELETE') { |
| 39 | + const url = new URL(baseUrl); |
| 40 | + if (qArg) { |
| 41 | + url.searchParams.set('q', JSON.stringify(qArg)); |
| 42 | + } |
| 43 | + if (otherArgs) { |
| 44 | + Object.entries(otherArgs).forEach(([key, value]) => { |
| 45 | + url.searchParams.set(key, String(value)); |
| 46 | + }); |
| 47 | + } |
| 48 | + return url.toString(); |
| 49 | + } |
| 50 | + return baseUrl; |
| 51 | + }; |
| 52 | + |
| 53 | + const executeRequest = async (method: string, body?: any) => { |
| 54 | + const url = buildUrl(method); |
| 55 | + const request = makeRequest(method, url, body); |
| 56 | + const response = await handler({ request, params }); |
| 57 | + const responseBody = await unmarshal(response); |
| 58 | + return { |
| 59 | + status: response.status, |
| 60 | + body: responseBody, |
| 61 | + }; |
| 62 | + }; |
| 63 | + |
| 64 | + return { |
| 65 | + get: async () => executeRequest('GET'), |
| 66 | + post: () => ({ |
| 67 | + send: async (data: any) => executeRequest('POST', data), |
| 68 | + }), |
| 69 | + put: () => ({ |
| 70 | + send: async (data: any) => executeRequest('PUT', data), |
| 71 | + }), |
| 72 | + del: async () => executeRequest('DELETE'), |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +describe('TanStack Start adapter tests - rpc handler', () => { |
| 77 | + let origDir: string; |
| 78 | + |
| 79 | + beforeEach(() => { |
| 80 | + origDir = process.cwd(); |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(() => { |
| 84 | + process.chdir(origDir); |
| 85 | + }); |
| 86 | + |
| 87 | + it('simple crud', async () => { |
| 88 | + const model = ` |
| 89 | +model M { |
| 90 | + id String @id @default(cuid()) |
| 91 | + value Int |
| 92 | +} |
| 93 | + `; |
| 94 | + |
| 95 | + const { prisma } = await loadSchema(model); |
| 96 | + |
| 97 | + const client = await makeTestClient('/m/create', { getPrisma: () => prisma }).post().send({ data: { id: '1', value: 1 } }); |
| 98 | + expect(client.status).toBe(201); |
| 99 | + expect(client.body.data.value).toBe(1); |
| 100 | + |
| 101 | + const findUnique = await makeTestClient('/m/findUnique', { getPrisma: () => prisma }, { where: { id: '1' } }).get(); |
| 102 | + expect(findUnique.status).toBe(200); |
| 103 | + expect(findUnique.body.data.value).toBe(1); |
| 104 | + |
| 105 | + const findFirst = await makeTestClient('/m/findFirst', { getPrisma: () => prisma }, { where: { id: '1' } }).get(); |
| 106 | + expect(findFirst.status).toBe(200); |
| 107 | + expect(findFirst.body.data.value).toBe(1); |
| 108 | + |
| 109 | + const findMany = await makeTestClient('/m/findMany', { getPrisma: () => prisma }, {}).get(); |
| 110 | + expect(findMany.status).toBe(200); |
| 111 | + expect(findMany.body.data).toHaveLength(1); |
| 112 | + |
| 113 | + const update = await makeTestClient('/m/update', { getPrisma: () => prisma }).put().send({ where: { id: '1' }, data: { value: 2 } }); |
| 114 | + expect(update.status).toBe(200); |
| 115 | + expect(update.body.data.value).toBe(2); |
| 116 | + |
| 117 | + const updateMany = await makeTestClient('/m/updateMany', { getPrisma: () => prisma }).put().send({ data: { value: 4 } }); |
| 118 | + expect(updateMany.status).toBe(200); |
| 119 | + expect(updateMany.body.data.count).toBe(1); |
| 120 | + |
| 121 | + const upsert1 = await makeTestClient('/m/upsert', { getPrisma: () => prisma }).post().send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } }); |
| 122 | + expect(upsert1.status).toBe(201); |
| 123 | + expect(upsert1.body.data.value).toBe(2); |
| 124 | + |
| 125 | + const upsert2 = await makeTestClient('/m/upsert', { getPrisma: () => prisma }).post().send({ where: { id: '2' }, create: { id: '2', value: 2 }, update: { value: 3 } }); |
| 126 | + expect(upsert2.status).toBe(201); |
| 127 | + expect(upsert2.body.data.value).toBe(3); |
| 128 | + |
| 129 | + const count1 = await makeTestClient('/m/count', { getPrisma: () => prisma }, { where: { id: '1' } }).get(); |
| 130 | + expect(count1.status).toBe(200); |
| 131 | + expect(count1.body.data).toBe(1); |
| 132 | + |
| 133 | + const count2 = await makeTestClient('/m/count', { getPrisma: () => prisma }, {}).get(); |
| 134 | + expect(count2.status).toBe(200); |
| 135 | + expect(count2.body.data).toBe(2); |
| 136 | + |
| 137 | + const aggregate = await makeTestClient('/m/aggregate', { getPrisma: () => prisma }, { _sum: { value: true } }).get(); |
| 138 | + expect(aggregate.status).toBe(200); |
| 139 | + expect(aggregate.body.data._sum.value).toBe(7); |
| 140 | + |
| 141 | + const groupBy = await makeTestClient('/m/groupBy', { getPrisma: () => prisma }, { by: ['id'], _sum: { value: true } }).get(); |
| 142 | + expect(groupBy.status).toBe(200); |
| 143 | + const data = groupBy.body.data; |
| 144 | + expect(data).toHaveLength(2); |
| 145 | + expect(data.find((item: any) => item.id === '1')._sum.value).toBe(4); |
| 146 | + expect(data.find((item: any) => item.id === '2')._sum.value).toBe(3); |
| 147 | + |
| 148 | + const deleteOne = await makeTestClient('/m/delete', { getPrisma: () => prisma }, { where: { id: '1' } }).del(); |
| 149 | + expect(deleteOne.status).toBe(200); |
| 150 | + expect(await prisma.m.count()).toBe(1); |
| 151 | + |
| 152 | + const deleteMany = await makeTestClient('/m/deleteMany', { getPrisma: () => prisma }, {}).del(); |
| 153 | + expect(deleteMany.status).toBe(200); |
| 154 | + expect(deleteMany.body.data.count).toBe(1); |
| 155 | + expect(await prisma.m.count()).toBe(0); |
| 156 | + }); |
| 157 | + |
| 158 | + it('custom load path', async () => { |
| 159 | + const model = ` |
| 160 | +model M { |
| 161 | + id String @id @default(cuid()) |
| 162 | + value Int |
| 163 | +} |
| 164 | + `; |
| 165 | + |
| 166 | + const { prisma, projectDir } = await loadSchema(model, { output: './zen' }); |
| 167 | + |
| 168 | + const client = await makeTestClient('/m/create', { |
| 169 | + getPrisma: () => prisma, |
| 170 | + modelMeta: require(path.join(projectDir, './zen/model-meta')).default, |
| 171 | + zodSchemas: require(path.join(projectDir, './zen/zod')), |
| 172 | + }).post().send({ data: { id: '1', value: 1 } }); |
| 173 | + |
| 174 | + expect(client.status).toBe(201); |
| 175 | + expect(client.body.data.value).toBe(1); |
| 176 | + }); |
| 177 | + |
| 178 | + it('access policy crud', async () => { |
| 179 | + const model = ` |
| 180 | +model M { |
| 181 | + id String @id @default(cuid()) |
| 182 | + value Int |
| 183 | +
|
| 184 | + @@allow('create', true) |
| 185 | + @@allow('read', value > 0) |
| 186 | + @@allow('update', future().value > 1) |
| 187 | + @@allow('delete', value > 2) |
| 188 | +} |
| 189 | + `; |
| 190 | + |
| 191 | + const { enhance } = await loadSchema(model); |
| 192 | + |
| 193 | + const createForbidden = await makeTestClient('/m/create', { getPrisma: () => enhance() }).post().send({ data: { value: 0 } }); |
| 194 | + expect(createForbidden.status).toBe(403); |
| 195 | + expect(createForbidden.body.error.reason).toBe('RESULT_NOT_READABLE'); |
| 196 | + |
| 197 | + const create = await makeTestClient('/m/create', { getPrisma: () => enhance() }).post().send({ data: { id: '1', value: 1 } }); |
| 198 | + expect(create.status).toBe(201); |
| 199 | + |
| 200 | + const findMany = await makeTestClient('/m/findMany', { getPrisma: () => enhance() }).get(); |
| 201 | + expect(findMany.status).toBe(200); |
| 202 | + expect(findMany.body.data).toHaveLength(1); |
| 203 | + |
| 204 | + const updateForbidden1 = await makeTestClient('/m/update', { getPrisma: () => enhance() }).put().send({ where: { id: '1' }, data: { value: 0 } }); |
| 205 | + expect(updateForbidden1.status).toBe(403); |
| 206 | + |
| 207 | + const update1 = await makeTestClient('/m/update', { getPrisma: () => enhance() }).put().send({ where: { id: '1' }, data: { value: 2 } }); |
| 208 | + expect(update1.status).toBe(200); |
| 209 | + |
| 210 | + const deleteForbidden = await makeTestClient('/m/delete', { getPrisma: () => enhance() }, { where: { id: '1' } }).del(); |
| 211 | + expect(deleteForbidden.status).toBe(403); |
| 212 | + |
| 213 | + const update2 = await makeTestClient('/m/update', { getPrisma: () => enhance() }).put().send({ where: { id: '1' }, data: { value: 3 } }); |
| 214 | + expect(update2.status).toBe(200); |
| 215 | + |
| 216 | + const deleteOne = await makeTestClient('/m/delete', { getPrisma: () => enhance() }, { where: { id: '1' } }).del(); |
| 217 | + expect(deleteOne.status).toBe(200); |
| 218 | + }); |
| 219 | +}); |
| 220 | + |
| 221 | +describe('TanStack Start adapter tests - rest handler', () => { |
| 222 | + let origDir: string; |
| 223 | + |
| 224 | + beforeEach(() => { |
| 225 | + origDir = process.cwd(); |
| 226 | + }); |
| 227 | + |
| 228 | + afterEach(() => { |
| 229 | + process.chdir(origDir); |
| 230 | + }); |
| 231 | + |
| 232 | + it('adapter test - rest', async () => { |
| 233 | + const model = ` |
| 234 | +model M { |
| 235 | + id String @id @default(cuid()) |
| 236 | + value Int |
| 237 | +} |
| 238 | + `; |
| 239 | + |
| 240 | + const { prisma, modelMeta } = await loadSchema(model); |
| 241 | + |
| 242 | + const options = { getPrisma: () => prisma, handler: Rest({ endpoint: 'http://localhost/api' }), modelMeta }; |
| 243 | + |
| 244 | + const create = await makeTestClient('/m', options).post().send({ data: { type: 'm', attributes: { id: '1', value: 1 } } }); |
| 245 | + expect(create.status).toBe(201); |
| 246 | + expect(create.body.data.attributes.value).toBe(1); |
| 247 | + |
| 248 | + const getOne = await makeTestClient('/m/1', options).get(); |
| 249 | + expect(getOne.status).toBe(200); |
| 250 | + expect(getOne.body.data.id).toBe('1'); |
| 251 | + |
| 252 | + const findWithFilter1 = await makeTestClient('/m', options, undefined, { 'filter[value]': '1' }).get(); |
| 253 | + expect(findWithFilter1.status).toBe(200); |
| 254 | + expect(findWithFilter1.body.data).toHaveLength(1); |
| 255 | + |
| 256 | + const findWithFilter2 = await makeTestClient('/m', options, undefined, { 'filter[value]': '2' }).get(); |
| 257 | + expect(findWithFilter2.status).toBe(200); |
| 258 | + expect(findWithFilter2.body.data).toHaveLength(0); |
| 259 | + |
| 260 | + const update = await makeTestClient('/m/1', options).put().send({ data: { type: 'm', attributes: { value: 2 } } }); |
| 261 | + expect(update.status).toBe(200); |
| 262 | + expect(update.body.data.attributes.value).toBe(2); |
| 263 | + |
| 264 | + const deleteOne = await makeTestClient('/m/1', options).del(); |
| 265 | + expect(deleteOne.status).toBe(200); |
| 266 | + expect(await prisma.m.count()).toBe(0); |
| 267 | + }); |
| 268 | +}); |
| 269 | + |
0 commit comments