Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions tests/constants.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, it } from 'bun:test';

const loadConstantsWithArgv = async (argv1: string) => {
const originalArgv = process.argv;
process.argv = ['node', argv1];
const url = `../src/utils/constants.ts?t=${Date.now()}_${Math.random()}`;
const mod = await import(url);
process.argv = originalArgv;
return mod;
};
Comment on lines +3 to +10
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Add try-finally block to guarantee cleanup of process.argv.

If the dynamic import throws (e.g., due to a syntax error in constants.ts during development), originalArgv will never be restored. This will pollute process.argv for all subsequent tests in the suite, potentially causing cascading failures or masking the root cause.

🛡️ Proposed fix to ensure cleanup
 const loadConstantsWithArgv = async (argv1: string) => {
   const originalArgv = process.argv;
-  process.argv = ['node', argv1];
-  const url = `../src/utils/constants.ts?t=${Date.now()}_${Math.random()}`;
-  const mod = await import(url);
-  process.argv = originalArgv;
-  return mod;
+  try {
+    process.argv = ['node', argv1];
+    const url = `../src/utils/constants.ts?t=${Date.now()}_${Math.random()}`;
+    return await import(url);
+  } finally {
+    process.argv = originalArgv;
+  }
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const loadConstantsWithArgv = async (argv1: string) => {
const originalArgv = process.argv;
process.argv = ['node', argv1];
const url = `../src/utils/constants.ts?t=${Date.now()}_${Math.random()}`;
const mod = await import(url);
process.argv = originalArgv;
return mod;
};
const loadConstantsWithArgv = async (argv1: string) => {
const originalArgv = process.argv;
try {
process.argv = ['node', argv1];
const url = `../src/utils/constants.ts?t=${Date.now()}_${Math.random()}`;
return await import(url);
} finally {
process.argv = originalArgv;
}
};
🤖 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 `@tests/constants.test.ts` around lines 3 - 10, The helper
loadConstantsWithArgv mutates process.argv but doesn’t guarantee restoration if
the dynamic import throws; wrap the import and return in a try block and move
the process.argv = originalArgv into a finally block so originalArgv is always
restored, referencing loadConstantsWithArgv, originalArgv, process.argv and the
dynamic import url to locate where to add the try/finally.


describe('constants', () => {
it('should initialize correctly when script is cresc', async () => {
const mod = await loadConstantsWithArgv('/usr/local/bin/cresc');
expect(mod.scriptName).toBe('cresc');
expect(mod.IS_CRESC).toBe(true);
expect(mod.credentialFile).toBe('.cresc.token');
expect(mod.updateJson).toBe('cresc.config.json');
expect(mod.tempDir).toBe('.cresc.temp');
expect(mod.pricingPageUrl).toBe('https://cresc.dev/pricing');
expect(mod.defaultEndpoints).toEqual([
'https://api.cresc.dev',
'https://api.cresc.app',
]);
});

it('should initialize correctly when script is pushy', async () => {
const mod = await loadConstantsWithArgv('/usr/local/bin/pushy');
expect(mod.scriptName).toBe('pushy');
expect(mod.IS_CRESC).toBe(false);
expect(mod.credentialFile).toBe('.update');
expect(mod.updateJson).toBe('update.json');
expect(mod.tempDir).toBe('.pushy');
expect(mod.pricingPageUrl).toBe(
'https://pushy.reactnative.cn/pricing.html',
);
expect(mod.defaultEndpoints).toEqual([
'https://update.reactnative.cn/api',
'https://update.react-native.cn/api',
]);
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it('should identify PPK bundle file names correctly', async () => {
const { isPPKBundleFileName } = await import('../src/utils/constants.ts');
expect(isPPKBundleFileName('index.bundlejs')).toBe(true);
expect(isPPKBundleFileName('bundle.harmony.js')).toBe(true);
expect(isPPKBundleFileName('index.js')).toBe(false);
expect(isPPKBundleFileName('main.bundlejs')).toBe(false);
});
});