|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { MockOs } from '../mocks/system.js'; |
| 3 | +import { MockReporter } from '../mocks/reporter.js'; |
| 4 | +import { ConnectOrchestrator } from '../../../src/orchestrators/connect'; |
| 5 | +import * as net from 'node:net'; |
| 6 | +import { config } from '../../../src/config.js'; |
| 7 | +import { fakeLogin, fakeLogout } from '../mocks/mock-login'; |
| 8 | +import * as open from 'open' |
| 9 | +import { LoginOrchestrator } from '../../../src/orchestrators/login'; |
| 10 | +import { Server } from 'node:http'; |
| 11 | +import { vol } from 'memfs'; |
| 12 | + |
| 13 | +vi.mock(import('../../../src/orchestrators/login'), async () => { |
| 14 | + return { |
| 15 | + LoginOrchestrator: { |
| 16 | + run: async () => {} |
| 17 | + }, |
| 18 | + } |
| 19 | +}) |
| 20 | + |
| 21 | +vi.mock('node:fs', async () => { |
| 22 | + const { fs } = await import('memfs'); |
| 23 | + return fs |
| 24 | +}) |
| 25 | + |
| 26 | +vi.mock('node:fs/promises', async () => { |
| 27 | + const { fs } = await import('memfs'); |
| 28 | + return fs.promises; |
| 29 | +}) |
| 30 | + |
| 31 | +vi.mock(import('open'), async () => { |
| 32 | + return { |
| 33 | + default: vi.fn() |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +// The apply orchestrator directly calls plan so this will test both |
| 38 | +describe('Connect orchestrator tests', () => { |
| 39 | + beforeEach(() => { |
| 40 | + vol.reset(); |
| 41 | + }) |
| 42 | + |
| 43 | + it('It will start a local server on config.connectServerPort', async () => { |
| 44 | + const reporter = new MockReporter(); |
| 45 | + await fakeLogin(); |
| 46 | + |
| 47 | + const openSpy = vi.spyOn(open, 'default'); |
| 48 | + |
| 49 | + await new Promise<void>((done) => { |
| 50 | + ConnectOrchestrator.run('codify', reporter, false, async (connectionCode: string , server: Server) => { |
| 51 | + expect(connectionCode).to.be.a('string'); |
| 52 | + |
| 53 | + const portInUse = await checkPortStatus(config.connectServerPort); |
| 54 | + expect(portInUse).to.be.true; |
| 55 | + |
| 56 | + server.close(); |
| 57 | + done(); |
| 58 | + }) |
| 59 | + }) |
| 60 | + }); |
| 61 | + |
| 62 | + it('It will ask for a login if the user is not logged in', async () => { |
| 63 | + const reporter = new MockReporter({}); |
| 64 | + await fakeLogout(); |
| 65 | + |
| 66 | + const loginRunSpy = vi.spyOn(LoginOrchestrator, 'run'); |
| 67 | + const openSpy = vi.spyOn(open, 'default'); |
| 68 | + |
| 69 | + await new Promise<void>((done) => { |
| 70 | + ConnectOrchestrator.run('codify', reporter, false, async (connectionCode: string, server: Server) => { |
| 71 | + expect(connectionCode).to.be.a('string'); |
| 72 | + |
| 73 | + const portInUse = await checkPortStatus(config.connectServerPort); |
| 74 | + expect(portInUse).to.be.true; |
| 75 | + expect(loginRunSpy).toHaveBeenCalledOnce(); |
| 76 | + |
| 77 | + done(); |
| 78 | + server.close(); |
| 79 | + }) |
| 80 | + |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); |
| 85 | + |
| 86 | + afterEach(() => { |
| 87 | + vi.resetAllMocks(); |
| 88 | + MockOs.reset(); |
| 89 | + }) |
| 90 | + |
| 91 | + function checkPortStatus(port: number, host = '127.0.0.1') { |
| 92 | + return new Promise((resolve, reject) => { |
| 93 | + const socket = new net.Socket(); |
| 94 | + |
| 95 | + socket.once('connect', () => { |
| 96 | + // If 'connect' event fires, the port is open and listening |
| 97 | + socket.destroy(); |
| 98 | + resolve(true); // Port is in use |
| 99 | + }); |
| 100 | + |
| 101 | + socket.once('error', (err) => { |
| 102 | + // Any error typically means the port is not listening |
| 103 | + // EADDRNOTAVAIL, ECONNREFUSED, etc. |
| 104 | + reject(err); // Port is likely free or unreachable |
| 105 | + }); |
| 106 | + |
| 107 | + socket.once('timeout', () => { |
| 108 | + socket.destroy(); |
| 109 | + reject(new Error('Connection attempt timed out')); |
| 110 | + }); |
| 111 | + |
| 112 | + socket.connect(port, host); |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | +}) |
0 commit comments