feat: add deriveHostedDbPlan function to manage subscription levels#1768
Conversation
📝 WalkthroughWalkthroughThe mock proxy API server now derives a ChangesMock API Plan Derivation
🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 5 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 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: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/proxy-mock-api/server.js`:
- Around line 14-22: Convert the function declaration deriveCompanyId into a
const arrow function (const deriveCompanyId = (username) => { ... }) and do the
same for the other helper function declarations flagged in this file (the two
other helpers noted in the review); preserve the existing logic, return values
and any references/exports, and ensure you don't rely on hoisting (move
declarations earlier if they are used before definition).
- Around line 74-83: The unguarded JSON.parse in the PUT handler for
'/api/test/subscription-level' can throw on malformed input; wrap the body
parsing/JSON.parse in a try/catch inside the req.on('end') callback, and on
catch respond with res.writeHead(400, { 'Content-Type': 'application/json' })
and res.end(JSON.stringify({ ok: false, error: 'Invalid JSON' })) so the server
doesn't crash; on success continue to set currentSubscriptionLevel and return
the existing 200 response.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: bfd1474a-9581-4153-9ec9-f8a17c3dc51b
📒 Files selected for processing (1)
test/proxy-mock-api/server.js
| function deriveCompanyId(username) { | ||
| // Single-username case (e.g. plain "proxy_user") keeps the legacy id so existing | ||
| // tests/clients that don't randomize still hit a stable companyId. | ||
| if (username === PROXY_USERNAME_PREFIX) { | ||
| return 'test-company-001'; | ||
| } | ||
| const hash = crypto.createHash('sha1').update(username).digest('hex').slice(0, 12); | ||
| return `test-company-${hash}`; | ||
| // Single-username case (e.g. plain "proxy_user") keeps the legacy id so existing | ||
| // tests/clients that don't randomize still hit a stable companyId. | ||
| if (username === PROXY_USERNAME_PREFIX) { | ||
| return 'test-company-001'; | ||
| } | ||
| const hash = crypto.createHash('sha1').update(username).digest('hex').slice(0, 12); | ||
| return `test-company-${hash}`; | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
Align helper declarations with arrow-function guideline.
These helpers are currently function declarations; convert them to const arrow functions for guideline compliance.
Proposed refactor
-function deriveCompanyId(username) {
+const deriveCompanyId = (username) => {
// Single-username case (e.g. plain "proxy_user") keeps the legacy id so existing
// tests/clients that don't randomize still hit a stable companyId.
if (username === PROXY_USERNAME_PREFIX) {
return 'test-company-001';
}
const hash = crypto.createHash('sha1').update(username).digest('hex').slice(0, 12);
return `test-company-${hash}`;
-}
+};
-function deriveConnectionId(username) {
+const deriveConnectionId = (username) => {
if (username === PROXY_USERNAME_PREFIX) {
return 'test-connection-001';
}
const hash = crypto.createHash('sha1').update(username).digest('hex').slice(0, 12);
return `test-connection-${hash}`;
-}
+};
-function deriveHostedDbPlan(level) {
+const deriveHostedDbPlan = (level) => {
if (level === 'frozen') return 'frozen';
if (level === 'TEST_TINY_PLAN') return 'TEST_TINY_PLAN';
if (level === 'HOSTED_DB_FREE' || level === 'FREE_PLAN') return 'HOSTED_DB_FREE';
return 'HOSTED_DB_PAID';
-}
+};Also applies to: 24-30, 53-58
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@test/proxy-mock-api/server.js` around lines 14 - 22, Convert the function
declaration deriveCompanyId into a const arrow function (const deriveCompanyId =
(username) => { ... }) and do the same for the other helper function
declarations flagged in this file (the two other helpers noted in the review);
preserve the existing logic, return values and any references/exports, and
ensure you don't rely on hoisting (move declarations earlier if they are used
before definition).
| if (req.method === 'PUT' && parsedUrl.pathname === '/api/test/subscription-level') { | ||
| let body = ''; | ||
| req.on('data', (chunk) => (body += chunk)); | ||
| req.on('end', () => { | ||
| const parsed = JSON.parse(body); | ||
| currentSubscriptionLevel = parsed.subscriptionLevel; | ||
| console.log(`[mock-api] Subscription level changed to: ${currentSubscriptionLevel}`); | ||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify({ ok: true, subscriptionLevel: currentSubscriptionLevel })); | ||
| }); |
There was a problem hiding this comment.
Handle malformed JSON explicitly in subscription-level endpoint.
JSON.parse(body) on Line 78 is unguarded; malformed input can throw and break this request path. Return a 400 with an error payload instead.
Proposed fix
if (req.method === 'PUT' && parsedUrl.pathname === '/api/test/subscription-level') {
let body = '';
req.on('data', (chunk) => (body += chunk));
req.on('end', () => {
- const parsed = JSON.parse(body);
- currentSubscriptionLevel = parsed.subscriptionLevel;
- console.log(`[mock-api] Subscription level changed to: ${currentSubscriptionLevel}`);
- res.writeHead(200, { 'Content-Type': 'application/json' });
- res.end(JSON.stringify({ ok: true, subscriptionLevel: currentSubscriptionLevel }));
+ try {
+ const parsed = JSON.parse(body);
+ currentSubscriptionLevel = parsed.subscriptionLevel;
+ console.log(`[mock-api] Subscription level changed to: ${currentSubscriptionLevel}`);
+ res.writeHead(200, { 'Content-Type': 'application/json' });
+ res.end(JSON.stringify({ ok: true, subscriptionLevel: currentSubscriptionLevel }));
+ } catch (e) {
+ res.writeHead(400, { 'Content-Type': 'application/json' });
+ res.end(JSON.stringify({ error: 'Invalid JSON body' }));
+ }
});
return;
}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@test/proxy-mock-api/server.js` around lines 74 - 83, The unguarded JSON.parse
in the PUT handler for '/api/test/subscription-level' can throw on malformed
input; wrap the body parsing/JSON.parse in a try/catch inside the req.on('end')
callback, and on catch respond with res.writeHead(400, { 'Content-Type':
'application/json' }) and res.end(JSON.stringify({ ok: false, error: 'Invalid
JSON' })) so the server doesn't crash; on success continue to set
currentSubscriptionLevel and return the existing 200 response.
There was a problem hiding this comment.
Pull request overview
Adds support in the proxy mock API to return a hostedDbPlan derived from the configured subscription level, aligning the mock response with how the proxy throttling logic is keyed.
Changes:
- Introduced
deriveHostedDbPlan(level)to map subscription levels to ahostedDbPlan. - Updated
GET /api/proxy/connectionsto includehostedDbPlanin the returned connection payload and logs. - Re-indented/reformatted the file (notably switching indentation style).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function deriveCompanyId(username) { | ||
| // Single-username case (e.g. plain "proxy_user") keeps the legacy id so existing | ||
| // tests/clients that don't randomize still hit a stable companyId. | ||
| if (username === PROXY_USERNAME_PREFIX) { | ||
| return 'test-company-001'; | ||
| } | ||
| const hash = crypto.createHash('sha1').update(username).digest('hex').slice(0, 12); | ||
| return `test-company-${hash}`; | ||
| // Single-username case (e.g. plain "proxy_user") keeps the legacy id so existing | ||
| // tests/clients that don't randomize still hit a stable companyId. | ||
| if (username === PROXY_USERNAME_PREFIX) { | ||
| return 'test-company-001'; |
| if (req.method === 'PUT' && parsedUrl.pathname === '/api/test/subscription-level') { | ||
| let body = ''; | ||
| req.on('data', (chunk) => (body += chunk)); | ||
| req.on('end', () => { | ||
| const parsed = JSON.parse(body); | ||
| currentSubscriptionLevel = parsed.subscriptionLevel; | ||
| console.log(`[mock-api] Subscription level changed to: ${currentSubscriptionLevel}`); | ||
| res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
| res.end(JSON.stringify({ ok: true, subscriptionLevel: currentSubscriptionLevel })); |
| return 'HOSTED_DB_PAID'; | ||
| } | ||
|
|
||
| // Store received usage reports so tests can verify them via GET /api/proxy/usage-reports |
| if (username === PROXY_USERNAME_PREFIX) { | ||
| return 'test-company-001'; | ||
| } | ||
| const hash = crypto.createHash('sha1').update(username).digest('hex').slice(0, 12); |
| if (username === PROXY_USERNAME_PREFIX) { | ||
| return 'test-connection-001'; | ||
| } | ||
| const hash = crypto.createHash('sha1').update(username).digest('hex').slice(0, 12); |
| ); | ||
| } else { | ||
| console.log( | ||
| `[mock-api] -> 404: username/database mismatch (expected ${PROXY_USERNAME_PREFIX}[_*]/${PROXY_DATABASE})`, |
Summary by CodeRabbit