|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | + |
| 4 | +import { command } from "./ailogic-config-set"; |
| 5 | +import * as ailogic from "../gcp/ailogic"; |
| 6 | +import * as projectUtils from "../projectUtils"; |
| 7 | +import * as prompt from "../prompt"; |
| 8 | +import * as utils from "../utils"; |
| 9 | +import { FirebaseError } from "../error"; |
| 10 | + |
| 11 | +const PROJECT_ID = "test-project"; |
| 12 | + |
| 13 | +describe("ailogic:config:set", () => { |
| 14 | + let updateStub: sinon.SinonStub; |
| 15 | + let getConfigStub: sinon.SinonStub; |
| 16 | + let confirmStub: sinon.SinonStub; |
| 17 | + let ensureStub: sinon.SinonStub; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + (command as unknown as { befores: unknown[] }).befores = []; // bypass pre-action hooks |
| 21 | + sinon.stub(projectUtils, "needProjectId").returns(PROJECT_ID); |
| 22 | + ensureStub = sinon.stub(ailogic, "ensureAILogicApiEnabled").resolves(); |
| 23 | + sinon.stub(utils, "logSuccess"); |
| 24 | + getConfigStub = sinon.stub(ailogic, "getConfig").resolves({ name: "config" }); |
| 25 | + updateStub = sinon.stub(ailogic, "updateConfig").resolves({ name: "config" }); |
| 26 | + confirmStub = sinon.stub(prompt, "confirm").resolves(true); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => sinon.restore()); |
| 30 | + |
| 31 | + it("throws on an unknown path listing the writable paths", async () => { |
| 32 | + await expect( |
| 33 | + command.runner()("security.authonly", "true", { project: PROJECT_ID }), |
| 34 | + ).to.be.rejectedWith(FirebaseError, /Unknown configuration path/); |
| 35 | + }); |
| 36 | + |
| 37 | + it("rejects a non-boolean value for a security path", async () => { |
| 38 | + await expect( |
| 39 | + command.runner()("security.auth-only", "yes", { project: PROJECT_ID }), |
| 40 | + ).to.be.rejectedWith(FirebaseError, /must be 'true' or 'false'/); |
| 41 | + }); |
| 42 | + |
| 43 | + it("validates input before triggering the API-enablement flow (fail-fast)", async () => { |
| 44 | + await expect( |
| 45 | + command.runner()("monitoring.sample-rate-percentage", "500", { project: PROJECT_ID }), |
| 46 | + ).to.be.rejectedWith(FirebaseError, /integer in the range 1-100/); |
| 47 | + expect(ensureStub).to.not.have.been.called; |
| 48 | + }); |
| 49 | + |
| 50 | + it("prompts when tightening auth-only from false to true, then updates", async () => { |
| 51 | + getConfigStub.resolves({ name: "config", trafficFilter: { firebaseAuthRequired: false } }); |
| 52 | + await command.runner()("security.auth-only", "true", { |
| 53 | + project: PROJECT_ID, |
| 54 | + interactive: true, |
| 55 | + }); |
| 56 | + expect(confirmStub).to.have.been.calledOnce; |
| 57 | + expect(updateStub).to.have.been.calledWith( |
| 58 | + PROJECT_ID, |
| 59 | + { trafficFilter: { firebaseAuthRequired: true } }, |
| 60 | + ["trafficFilter.firebaseAuthRequired"], |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it("does not prompt when auth-only is already true", async () => { |
| 65 | + getConfigStub.resolves({ name: "config", trafficFilter: { firebaseAuthRequired: true } }); |
| 66 | + await command.runner()("security.auth-only", "true", { |
| 67 | + project: PROJECT_ID, |
| 68 | + interactive: true, |
| 69 | + }); |
| 70 | + expect(confirmStub).to.not.have.been.called; |
| 71 | + expect(updateStub).to.have.been.calledOnce; |
| 72 | + }); |
| 73 | + |
| 74 | + it("does not prompt when relaxing auth-only to false", async () => { |
| 75 | + await command.runner()("security.auth-only", "false", { project: PROJECT_ID }); |
| 76 | + expect(confirmStub).to.not.have.been.called; |
| 77 | + expect(updateStub).to.have.been.calledWith( |
| 78 | + PROJECT_ID, |
| 79 | + { trafficFilter: { firebaseAuthRequired: false } }, |
| 80 | + ["trafficFilter.firebaseAuthRequired"], |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("propagates confirm() aborting in non-interactive mode without --force", async () => { |
| 85 | + // confirm() throws in non-interactive mode when no --force is given; the command |
| 86 | + // must surface that and not proceed to write. |
| 87 | + getConfigStub.resolves({ name: "config", trafficFilter: { firebaseAuthRequired: false } }); |
| 88 | + confirmStub.rejects(new FirebaseError("cannot be answered in non-interactive mode")); |
| 89 | + await expect( |
| 90 | + command.runner()("security.auth-only", "true", { project: PROJECT_ID, nonInteractive: true }), |
| 91 | + ).to.be.rejectedWith(FirebaseError, /non-interactive/); |
| 92 | + expect(updateStub).to.not.have.been.called; |
| 93 | + }); |
| 94 | + |
| 95 | + it("prompts when tightening template-only from false to true, then updates", async () => { |
| 96 | + getConfigStub.resolves({ name: "config", trafficFilter: { templateOnly: false } }); |
| 97 | + await command.runner()("security.template-only", "true", { |
| 98 | + project: PROJECT_ID, |
| 99 | + interactive: true, |
| 100 | + }); |
| 101 | + expect(confirmStub).to.have.been.calledOnce; |
| 102 | + expect(updateStub).to.have.been.calledWith( |
| 103 | + PROJECT_ID, |
| 104 | + { trafficFilter: { templateOnly: true } }, |
| 105 | + ["trafficFilter.templateOnly"], |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it("accepts a case-insensitive boolean value", async () => { |
| 110 | + await command.runner()("monitoring.state", "TRUE", { project: PROJECT_ID }); |
| 111 | + expect(updateStub).to.have.been.calledWith(PROJECT_ID, { telemetryConfig: { mode: "ALL" } }, [ |
| 112 | + "telemetryConfig.mode", |
| 113 | + ]); |
| 114 | + }); |
| 115 | + |
| 116 | + it("maps monitoring.state true to telemetryConfig.mode ALL", async () => { |
| 117 | + await command.runner()("monitoring.state", "true", { project: PROJECT_ID }); |
| 118 | + expect(updateStub).to.have.been.calledWith(PROJECT_ID, { telemetryConfig: { mode: "ALL" } }, [ |
| 119 | + "telemetryConfig.mode", |
| 120 | + ]); |
| 121 | + }); |
| 122 | + |
| 123 | + it("maps monitoring.state false to telemetryConfig.mode NONE without prompting", async () => { |
| 124 | + await command.runner()("monitoring.state", "false", { project: PROJECT_ID }); |
| 125 | + expect(confirmStub).to.not.have.been.called; |
| 126 | + expect(updateStub).to.have.been.calledWith(PROJECT_ID, { telemetryConfig: { mode: "NONE" } }, [ |
| 127 | + "telemetryConfig.mode", |
| 128 | + ]); |
| 129 | + }); |
| 130 | + |
| 131 | + it("maps a sample-rate percentage to a (0,1] sampling fraction", async () => { |
| 132 | + await command.runner()("monitoring.sample-rate-percentage", "50", { project: PROJECT_ID }); |
| 133 | + expect(updateStub).to.have.been.calledWith( |
| 134 | + PROJECT_ID, |
| 135 | + { telemetryConfig: { samplingRate: 0.5 } }, |
| 136 | + ["telemetryConfig.samplingRate"], |
| 137 | + ); |
| 138 | + }); |
| 139 | + |
| 140 | + it("rejects an out-of-range or non-integer sample rate", async () => { |
| 141 | + // "1e2", "0x32", and " 50 " all coerce to valid integers via Number(), so the |
| 142 | + // strict decimal check must reject them too. |
| 143 | + for (const bad of ["0", "101", "1.5", "abc", "1e2", "0x32", " 50 ", "50%"]) { |
| 144 | + await expect( |
| 145 | + command.runner()("monitoring.sample-rate-percentage", bad, { project: PROJECT_ID }), |
| 146 | + ).to.be.rejectedWith(FirebaseError, /integer in the range 1-100/); |
| 147 | + } |
| 148 | + expect(updateStub).to.not.have.been.called; |
| 149 | + }); |
| 150 | + |
| 151 | + it("normalizes a zero-padded sample rate in the echoed value", async () => { |
| 152 | + expect( |
| 153 | + await command.runner()("monitoring.sample-rate-percentage", "007", { project: PROJECT_ID }), |
| 154 | + ).to.deep.equal({ path: "monitoring.sample-rate-percentage", value: "7" }); |
| 155 | + expect(updateStub).to.have.been.calledWith( |
| 156 | + PROJECT_ID, |
| 157 | + { telemetryConfig: { samplingRate: 0.07 } }, |
| 158 | + ["telemetryConfig.samplingRate"], |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + it("returns the normalized value for --json output", async () => { |
| 163 | + expect( |
| 164 | + await command.runner()("monitoring.state", "TRUE", { project: PROJECT_ID }), |
| 165 | + ).to.deep.equal({ path: "monitoring.state", value: "true" }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments