test-dynamic-port.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { CLIProvider } from './src/core/llm-provider.js';
  2. import { createWorkspaceManagerTool } from './src/tools/workspace-manager.js';
  3. let passed = 0;
  4. let failed = 0;
  5. function test(name, fn) {
  6. return Promise.resolve()
  7. .then(fn)
  8. .then(() => {
  9. console.log(` ✓ ${name}`);
  10. passed++;
  11. })
  12. .catch((err) => {
  13. console.log(` ✗ ${name}: ${err.message}`);
  14. failed++;
  15. });
  16. }
  17. function assert(cond, msg) {
  18. if (!cond) throw new Error(msg || 'Assertion failed');
  19. }
  20. console.log('\n── Dynamic Port Regression ──');
  21. await test('CLIProvider reads the current config port instead of a startup snapshot', async () => {
  22. const config = { model: 'claude-opus-4-6', port: 3300 };
  23. const provider = new CLIProvider(config);
  24. assert(provider.getServerPort() === 3300, 'expected initial port 3300');
  25. config.port = 3301;
  26. assert(provider.getServerPort() === 3301, 'provider should see updated config port');
  27. });
  28. await test('WorkspaceManager uses the latest config port at execution time', async () => {
  29. const config = { port: 3300 };
  30. const calls = [];
  31. const originalFetch = global.fetch;
  32. global.fetch = async (url) => {
  33. calls.push(url);
  34. return {
  35. ok: true,
  36. async json() {
  37. return [];
  38. },
  39. };
  40. };
  41. try {
  42. const tool = createWorkspaceManagerTool(config);
  43. config.port = 3301;
  44. await tool.execute({ action: 'list' });
  45. } finally {
  46. global.fetch = originalFetch;
  47. }
  48. assert(calls.length === 1, 'expected one fetch call');
  49. assert(calls[0] === 'http://localhost:3301/api/workspaces', `unexpected URL: ${calls[0]}`);
  50. });
  51. console.log(`\n── Results ──\n\n ${passed} passed, ${failed} failed\n`);
  52. process.exit(failed > 0 ? 1 : 0);