|
| 1 | +import {CxWrapper} from '../main/wrapper/CxWrapper'; |
| 2 | +import {CxConfig} from "../main/wrapper/CxConfig"; |
| 3 | +import {CxCommandOutput} from "../main/wrapper/CxCommandOutput"; |
| 4 | +import {ExecutionService} from "../main/wrapper/ExecutionService"; |
| 5 | +import {logger} from "../main/wrapper/loggerConfig"; |
| 6 | + |
| 7 | +// These tests never run the real cx: they check the arguments, and run Node itself for the output checks. |
| 8 | +describe("Configure API key", () => { |
| 9 | + const secret = "the-secret-api-key"; |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + jest.restoreAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + function wrapper(): CxWrapper { |
| 16 | + const config = new CxConfig(); |
| 17 | + config.pathToExecutable = process.execPath; |
| 18 | + config.apiKey = "key-from-config"; |
| 19 | + config.agentName = "VS Code"; |
| 20 | + return new CxWrapper(config); |
| 21 | + } |
| 22 | + |
| 23 | + function stubSensitiveCommands(): jest.SpyInstance { |
| 24 | + const done = new CxCommandOutput(); |
| 25 | + done.exitCode = 0; |
| 26 | + return jest.spyOn(ExecutionService.prototype, "executeSensitiveCommands").mockResolvedValue(done); |
| 27 | + } |
| 28 | + |
| 29 | + it('configureApiKey runs configure set for cx_apikey, with only the given value', async () => { |
| 30 | + const spy = stubSensitiveCommands(); |
| 31 | + |
| 32 | + const output = await wrapper().configureApiKey(secret); |
| 33 | + |
| 34 | + expect(output.exitCode).toBe(0); |
| 35 | + expect(spy).toHaveBeenCalledWith(process.execPath, |
| 36 | + ["configure", "set", "--prop-name", "cx_apikey", "--prop-value", secret]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('clearApiKey sets cx_apikey to an empty value', async () => { |
| 40 | + const spy = stubSensitiveCommands(); |
| 41 | + |
| 42 | + await wrapper().clearApiKey(); |
| 43 | + |
| 44 | + expect(spy).toHaveBeenCalledWith(process.execPath, |
| 45 | + ["configure", "set", "--prop-name", "cx_apikey", "--prop-value", ""]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('executeSensitiveCommands returns the exit code and never logs or returns the output', async () => { |
| 49 | + const info = jest.spyOn(logger, "info"); |
| 50 | + const error = jest.spyOn(logger, "error"); |
| 51 | + const script = `process.stdout.write('${secret}'); process.stderr.write('${secret}'); process.exit(3)`; |
| 52 | + |
| 53 | + const output = await new ExecutionService().executeSensitiveCommands(process.execPath, ["-e", script]); |
| 54 | + |
| 55 | + expect(output.exitCode).toBe(3); |
| 56 | + expect(output.status).not.toContain(secret); |
| 57 | + expect(output.payload).toBeUndefined(); |
| 58 | + const logged = [...info.mock.calls, ...error.mock.calls].map((call) => String(call[0])).join("\n"); |
| 59 | + expect(logged).not.toContain(secret); |
| 60 | + }); |
| 61 | + |
| 62 | + it('executeSensitiveCommands passes arguments unchanged', async () => { |
| 63 | + const value = `a"b'c, d`; |
| 64 | + const script = `process.exit(process.argv[1] === ${JSON.stringify(value)} ? 0 : 1)`; |
| 65 | + |
| 66 | + const output = await new ExecutionService().executeSensitiveCommands(process.execPath, ["-e", script, value]); |
| 67 | + |
| 68 | + expect(output.exitCode).toBe(0); |
| 69 | + }); |
| 70 | + |
| 71 | + it('executeSensitiveCommands reports a failure to start', async () => { |
| 72 | + const output = await new ExecutionService().executeSensitiveCommands("does-not-exist-cx-binary", []); |
| 73 | + |
| 74 | + expect(output.exitCode).toBe(-1); |
| 75 | + }); |
| 76 | +}); |
0 commit comments