|
| 1 | +import quibble from "quibble"; |
| 2 | +import sinon from "sinon"; |
| 3 | +import { assert, beforeEach, afterEach, describe, it } from "poku"; |
| 4 | + |
| 5 | +describe("CLI tests", () => { |
| 6 | + let gotStub; |
| 7 | + let submitBom; |
| 8 | + |
| 9 | + beforeEach(async () => { |
| 10 | + // Create a sinon stub that mimics got() |
| 11 | + const fakeGotResponse = { |
| 12 | + json: sinon.stub().resolves({ success: true }), |
| 13 | + }; |
| 14 | + |
| 15 | + gotStub = sinon.stub().returns(fakeGotResponse); |
| 16 | + |
| 17 | + // Attach extend to the function itself |
| 18 | + gotStub.extend = sinon.stub().returns(gotStub); |
| 19 | + |
| 20 | + // Replace the real 'got' module with our stub |
| 21 | + await quibble.esm("got", { |
| 22 | + default: gotStub, |
| 23 | + }); |
| 24 | + |
| 25 | + // Import the module under test AFTER quibble |
| 26 | + ({ submitBom } = await import("./index.js")); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + quibble.reset(); // Restore real modules |
| 31 | + }); |
| 32 | + |
| 33 | + it("should report the SBOM with given project tag", async () => { |
| 34 | + const serverUrl = "https://api.example.com/upload"; |
| 35 | + const projectId = "1111"; |
| 36 | + const projectName = "test"; |
| 37 | + const projectVersion = "1.0.0"; |
| 38 | + const bomPayload = { bom: "test" }; |
| 39 | + |
| 40 | + await submitBom( |
| 41 | + { serverUrl, projectId, projectName, projectVersion }, |
| 42 | + bomPayload, |
| 43 | + ); |
| 44 | + |
| 45 | + // Verify got was called exactly once |
| 46 | + sinon.assert.calledOnce(gotStub); |
| 47 | + |
| 48 | + // Grab call arguments |
| 49 | + const [calledUrl, options] = gotStub.firstCall.args; |
| 50 | + |
| 51 | + assert.equal(calledUrl, serverUrl); |
| 52 | + assert.equal(options.method, "PUT"); |
| 53 | + assert.equal(options.https.rejectUnauthorized, true); |
| 54 | + assert.equal(options.headers["X-Api-Key"], "MY_API_KEY"); |
| 55 | + assert.match(options.headers["user-agent"], /@CycloneDX\/cdxgen/); |
| 56 | + assert.deepEqual(options.json, bomPayload); |
| 57 | + }); |
| 58 | +}); |
0 commit comments