Repository navigation
test(proxy): add ut for proxy - #2432
Conversation
|
Claude finished @jiashengguo's task —— View job PR Review Complete ✅I've reviewed the proxy unit test additions. Here's my comprehensive analysis:
📋 SummaryThis PR adds comprehensive unit tests for the proxy functionality and extracts the ✅ StrengthsCode Quality & Structure
Test Implementation
Security & Best Practices
🔍 Areas for ConsiderationMinor Improvements
Code Organization
🚀 Performance Considerations
🛡️ Security Assessment
📈 Test Coverage AnalysisThe tests cover:
Missing coverage (for future consideration):
🎯 RecommendationAPPROVE - This is a solid addition that improves testability and coverage without introducing risks. The code follows project conventions and includes meaningful test scenarios. |
📝 WalkthroughWalkthroughThe proxy module refactors app creation into a new exported Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/cli/test/proxy.test.ts`:
- Around line 9-18: The test teardown currently only closes the HTTP server
(server.close) but never disconnects Kysely clients created by
createTestClient(), which leaks DB connections; modify the test file to track
created clients (e.g., push each result of createTestClient() into a testClients
array or a known client variable) and in afterEach await
Promise.all(testClients.map(c => c.$disconnect())) (or await
client.$disconnect() if a single client), then clear the references (set to
undefined/empty array) before resolving the teardown; update afterEach alongside
the existing server.close logic to ensure all clients are properly disconnected.
| afterEach(async () => { | ||
| await new Promise<void>((resolve) => { | ||
| if (server) { | ||
| server.close(() => resolve()); | ||
| server = undefined; | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# First, check the file exists and show the afterEach block
echo "=== Lines 9-18 (afterEach) ==="
sed -n '9,18p' packages/cli/test/proxy.test.ts
echo -e "\n=== Full file structure and client creation sites ==="
rg -n 'createTestClient\(|afterEach|describe' packages/cli/test/proxy.test.ts | head -30
echo -e "\n=== Search for createTestClient calls with context ==="
rg -n -B2 -A2 'createTestClient\(' packages/cli/test/proxy.test.ts
echo -e "\n=== Search for $disconnect calls in this file ==="
rg -n '\$disconnect' packages/cli/test/proxy.test.ts || echo "No \$disconnect found"
echo -e "\n=== Repository-wide createTestClient usage ==="
rg -l 'createTestClient' packages --type=ts | head -5
echo -e "\n=== Check return type and definition of createTestClient ==="
rg -B2 -A10 'export.*createTestClient|function.*createTestClient' packages --type=ts | head -40Repository: zenstackhq/zenstack
Length of output: 1539
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Check for disconnect calls for the created clients ==="
grep -n 'disconnect' packages/cli/test/proxy.test.ts || echo "No disconnect found in file"
echo -e "\n=== Check full test file to see if clients are used and cleaned up ==="
wc -l packages/cli/test/proxy.test.ts
echo -e "\n=== Show the context around each createTestClient call ==="
sed -n '35,45p' packages/cli/test/proxy.test.ts
echo "---"
sed -n '68,80p' packages/cli/test/proxy.test.ts
echo "---"
sed -n '119,130p' packages/cli/test/proxy.test.ts
echo -e "\n=== Find createTestClient definition ==="
rg -n 'export.*createTestClient|^export function createTestClient|^function createTestClient' packages --type=ts
echo -e "\n=== Look at createTestClient export and understand return type ==="
rg -B5 -A20 'createTestClient' packages/cli/src/test-client.ts 2>/dev/null | head -60 || echo "File may not exist, searching more broadly..."
rg -B3 -A15 'export.*function.*createTestClient' packages --type=ts | head -50Repository: zenstackhq/zenstack
Length of output: 4089
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Find ClientContract definition ==="
rg -B5 -A15 'type ClientContract|interface ClientContract' packages --type=ts | head -80
echo -e "\n=== Search for $disconnect method definition ==="
rg -B3 -A8 '\$disconnect' packages --type=ts | head -100
echo -e "\n=== Check how other tests use createTestClient and cleanup ==="
rg -B2 -A5 'createTestClient' packages --type=ts | grep -A5 'disconnect\|afterEach\|cleanup' | head -50 || echo "No disconnect pattern found in other tests"
echo -e "\n=== Check test files that import createTestClient ==="
rg -l 'createTestClient' packages --type=ts | head -10
</script>Repository: zenstackhq/zenstack
Length of output: 6819
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Search for disconnect method ==="
rg -n 'disconnect' packages/orm --type=ts | grep -v node_modules | head -30
echo -e "\n=== Search for ClientContract type definition ==="
rg -B10 -A25 'export.*type ClientContract' packages/orm --type=ts | head -100
echo -e "\n=== Look at other test files to see pattern of client cleanup ==="
fd -e test.ts -e test.tsx packages | head -5
echo -e "\n=== Check one other test file for cleanup patterns ==="
fd -e test.ts packages | head -1 | xargs head -100
echo -e "\n=== Check if there are any other afterEach or test cleanup patterns in CLI tests ==="
find packages/cli/test -name '*.test.ts' -exec grep -l 'afterEach\|afterAll' {} \;
</script>Repository: zenstackhq/zenstack
Length of output: 3497
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Check ClientContract $disconnect method definition ==="
sed -n '200,210p' packages/orm/src/client/contract.ts
echo -e "\n=== Check client-impl disconnect implementation ==="
sed -n '334,350p' packages/orm/src/client/client-impl.ts
echo -e "\n=== Verify the three client creations are indeed at lines 37, 70, 121 ==="
sed -n '37p;70p;121p' packages/cli/test/proxy.test.ts
echo -e "\n=== Check if any client is used after creation and before test end ==="
sed -n '37,65p' packages/cli/test/proxy.test.ts | grep -E 'client\.|await|expect'
echo -e "\n=== Full context of test 1 (around line 37) ==="
sed -n '20,67p' packages/cli/test/proxy.test.ts
echo -e "\n=== Full context of test 2 (around line 70) ==="
sed -n '53,105p' packages/cli/test/proxy.test.tsRepository: zenstackhq/zenstack
Length of output: 110
🏁 Script executed:
#!/bin/bash
set -euo pipefail
echo "=== Check ClientContract disconnect method definition ==="
sed -n '200,210p' packages/orm/src/client/contract.ts
echo "=== Check client-impl disconnect implementation ==="
sed -n '334,350p' packages/orm/src/client/client-impl.ts
echo "=== Verify the three client creations ==="
sed -n '37p;70p;121p' packages/cli/test/proxy.test.ts
echo "=== Check if any client is used after creation ==="
sed -n '37,65p' packages/cli/test/proxy.test.ts
echo "=== Full context of test 1 ==="
sed -n '20,67p' packages/cli/test/proxy.test.ts
echo "=== Full context of test 2 ==="
sed -n '53,105p' packages/cli/test/proxy.test.ts
echo "=== Full context of test 3 ==="
sed -n '105,150p' packages/cli/test/proxy.test.tsRepository: 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, but afterEach only 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
Verify each finding against the current code and only fix it if needed.
In `@packages/cli/test/proxy.test.ts` around lines 9 - 18, The test teardown
currently only closes the HTTP server (server.close) but never disconnects
Kysely clients created by createTestClient(), which leaks DB connections; modify
the test file to track created clients (e.g., push each result of
createTestClient() into a testClients array or a known client variable) and in
afterEach await Promise.all(testClients.map(c => c.$disconnect())) (or await
client.$disconnect() if a single client), then clear the references (set to
undefined/empty array) before resolving the teardown; update afterEach alongside
the existing server.close logic to ensure all clients are properly disconnected.
Summary by CodeRabbit
New Features
Tests