Repository navigation
test(proxy): add ut for proxy #2432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
| import http from 'node:http'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { createProxyApp } from '../src/actions/proxy'; | ||
|
|
||
| describe('CLI proxy tests', () => { | ||
| let server: http.Server | undefined; | ||
|
|
||
| afterEach(async () => { | ||
| await new Promise<void>((resolve) => { | ||
| if (server) { | ||
| server.close(() => resolve()); | ||
| server = undefined; | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| async function startAt(app: ReturnType<typeof createProxyApp>): Promise<string> { | ||
| return new Promise((resolve) => { | ||
| server = app.listen(0, () => { | ||
| const addr = server!.address() as { port: number }; | ||
| resolve(`http://localhost:${addr.port}`); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| it('should serve schema at /api/schema endpoint', async () => { | ||
| const zmodel = ` | ||
| model User { | ||
| id String @id @default(cuid()) | ||
| email String @unique | ||
| } | ||
| `; | ||
|
|
||
| const client = await createTestClient(zmodel); | ||
| const app = createProxyApp(client, client.$schema); | ||
| const baseUrl = await startAt(app); | ||
|
|
||
| const r = await fetch(`${baseUrl}/api/schema`); | ||
| expect(r.status).toBe(200); | ||
|
|
||
| const body = await r.json(); | ||
| // schema fields are present | ||
| expect(body).toHaveProperty('models'); | ||
| expect(body.models).toHaveProperty('User'); | ||
| expect(body).toHaveProperty('provider'); | ||
| // zenstackVersion is injected by the proxy; when running tests directly | ||
| // from source (no built dist/) getVersion() returns undefined and the | ||
| // key is omitted from JSON — tolerate that, but if present it must be a string. | ||
| if ('zenstackVersion' in body) { | ||
| expect(typeof body.zenstackVersion).toBe('string'); | ||
| } | ||
| }); | ||
|
|
||
| it('should omit computed fields from default query responses', async () => { | ||
| // postCount is a @computed field — the proxy must not try to SELECT it | ||
| // by default (it has no backing column in the DB). | ||
| const zmodel = ` | ||
| model User { | ||
| id String @id @default(cuid()) | ||
| name String | ||
| postCount Int @computed | ||
| } | ||
| `; | ||
|
|
||
| // Mirror what proxy.ts does: build omit config from the schema, then | ||
| // create the client with skipValidationForComputedFields. | ||
| const client = await createTestClient(zmodel, { | ||
| skipValidationForComputedFields: true, | ||
| omit: { User: { postCount: true } }, | ||
| }); | ||
|
|
||
| const app = createProxyApp(client, client.$schema); | ||
| const baseUrl = await startAt(app); | ||
|
|
||
| // Create a user via the proxy API. | ||
| const createRes = await fetch(`${baseUrl}/api/model/user/create`, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify({ data: { name: 'Alice' } }), | ||
| }); | ||
| expect(createRes.status).toBe(201); | ||
| const created = await createRes.json(); | ||
|
|
||
| // The regular fields should be present … | ||
| expect(created.data).toHaveProperty('id'); | ||
| expect(created.data).toHaveProperty('name', 'Alice'); | ||
| // … but the computed field must be absent in the default response. | ||
| expect(created.data).not.toHaveProperty('postCount'); | ||
|
|
||
| // A findMany should behave the same way. | ||
| const listRes = await fetch(`${baseUrl}/api/model/user/findMany`); | ||
| expect(listRes.status).toBe(200); | ||
| const list = await listRes.json(); | ||
| expect(list.data).toHaveLength(1); | ||
| expect(list.data[0]).not.toHaveProperty('postCount'); | ||
| }); | ||
|
|
||
| it('should handle sequential transaction calls', async () => { | ||
| const zmodel = ` | ||
| model User { | ||
| id String @id @default(cuid()) | ||
| email String @unique | ||
| posts Post[] | ||
|
|
||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model Post { | ||
| id String @id @default(cuid()) | ||
| title String | ||
| author User? @relation(fields: [authorId], references: [id]) | ||
| authorId String? | ||
|
|
||
| @@allow('all', true) | ||
| } | ||
| `; | ||
|
|
||
| const client = await createTestClient(zmodel); | ||
| const app = createProxyApp(client, client.$schema); | ||
| const baseUrl = await startAt(app); | ||
|
|
||
| const txRes = await fetch(`${baseUrl}/api/model/$transaction/sequential`, { | ||
| method: 'POST', | ||
| headers: { 'Content-Type': 'application/json' }, | ||
| body: JSON.stringify([ | ||
| { | ||
| model: 'User', | ||
| op: 'create', | ||
| args: { data: { id: 'u1', email: 'alice@example.com' } }, | ||
| }, | ||
| { | ||
| model: 'Post', | ||
| op: 'create', | ||
| args: { data: { id: 'p1', title: 'Hello World', authorId: 'u1' } }, | ||
| }, | ||
| { | ||
| model: 'Post', | ||
| op: 'findMany', | ||
| args: { where: { authorId: 'u1' } }, | ||
| }, | ||
| ]), | ||
| }); | ||
| expect(txRes.status).toBe(200); | ||
| const tx = await txRes.json(); | ||
|
|
||
| // Should return results for each operation in the transaction. | ||
| expect(Array.isArray(tx.data)).toBe(true); | ||
| expect(tx.data).toHaveLength(3); | ||
|
|
||
| // First result: created user | ||
| expect(tx.data[0]).toMatchObject({ id: 'u1', email: 'alice@example.com' }); | ||
| // Second result: created post | ||
| expect(tx.data[1]).toMatchObject({ id: 'p1', title: 'Hello World', authorId: 'u1' }); | ||
| // Third result: findMany — should find the newly created post | ||
| expect(Array.isArray(tx.data[2])).toBe(true); | ||
| expect(tx.data[2]).toHaveLength(1); | ||
| expect(tx.data[2][0]).toMatchObject({ id: 'p1', title: 'Hello World' }); | ||
|
|
||
| // Confirm persisted outside transaction too. | ||
| const userRes = await fetch( | ||
| `${baseUrl}/api/model/user/findUnique?q=${encodeURIComponent(JSON.stringify({ where: { id: 'u1' } }))}`, | ||
| ); | ||
| expect(userRes.status).toBe(200); | ||
| const user = await userRes.json(); | ||
| expect(user.data).toMatchObject({ id: 'u1', email: 'alice@example.com' }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 1539
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 4089
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 6819
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 3497
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 110
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 8178
Disconnect test clients in teardown to avoid leaked database connections.
Each test creates a client via
createTestClient()at lines 37, 70, and 121, butafterEachonly closes the HTTP server. The clients maintain database connections through Kysely that are never cleaned up, causing connection leaks and potential test flakiness. Call$disconnect()for each created client during teardown.💡 Suggested fix
describe('CLI proxy tests', () => { let server: http.Server | undefined; + let clients: Array<Awaited<ReturnType<typeof createTestClient>>> = []; afterEach(async () => { await new Promise<void>((resolve) => { if (server) { server.close(() => resolve()); server = undefined; } else { resolve(); } }); + await Promise.all(clients.map((client) => client.$disconnect())); + clients = []; }); @@ - const client = await createTestClient(zmodel); + const client = await createTestClient(zmodel); + clients.push(client); @@ - const client = await createTestClient(zmodel, { + const client = await createTestClient(zmodel, { skipValidationForComputedFields: true, omit: { User: { postCount: true } }, }); + clients.push(client); @@ - const client = await createTestClient(zmodel); + const client = await createTestClient(zmodel); + clients.push(client);🤖 Prompt for AI Agents