|
| 1 | +import * as prompt from "../../../prompt"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import * as sinon from "sinon"; |
| 4 | +import * as init from "./index"; |
| 5 | +import * as utils from "./utils"; |
| 6 | +import * as apps from "../../../management/apps"; |
| 7 | +import * as provision from "../../../management/provisioning/provision"; |
| 8 | +import { Setup } from "../.."; |
| 9 | +import { AppPlatform } from "../../../management/apps"; |
| 10 | + |
| 11 | +describe("init ailogic", () => { |
| 12 | + let sandbox: sinon.SinonSandbox; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + sandbox = sinon.createSandbox(); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + sandbox.restore(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("askQuestions", () => { |
| 23 | + let listFirebaseAppsStub: sinon.SinonStub; |
| 24 | + let selectStub: sinon.SinonStub; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + listFirebaseAppsStub = sandbox.stub(apps, "listFirebaseApps"); |
| 28 | + selectStub = sandbox.stub(prompt, "select"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should populate ailogic featureInfo with selected app ID", async () => { |
| 32 | + const mockApps = [ |
| 33 | + { |
| 34 | + appId: "1:123456789:android:abcdef123456", |
| 35 | + displayName: "Test Android App", |
| 36 | + platform: AppPlatform.ANDROID, |
| 37 | + }, |
| 38 | + { |
| 39 | + appId: "1:123456789:web:fedcba654321", |
| 40 | + displayName: "Test Web App", |
| 41 | + platform: AppPlatform.WEB, |
| 42 | + }, |
| 43 | + ]; |
| 44 | + const mockSetup = { projectId: "test-project" } as Setup; |
| 45 | + |
| 46 | + listFirebaseAppsStub.resolves(mockApps); |
| 47 | + selectStub.resolves(mockApps[0]); // Select first app |
| 48 | + |
| 49 | + await init.askQuestions(mockSetup); |
| 50 | + |
| 51 | + expect(mockSetup.featureInfo).to.have.property("ailogic"); |
| 52 | + expect(mockSetup.featureInfo?.ailogic).to.deep.equal({ |
| 53 | + appId: "1:123456789:android:abcdef123456", |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should throw error when no project ID is found", async () => { |
| 58 | + const mockSetup = {} as Setup; // No projectId |
| 59 | + |
| 60 | + await expect(init.askQuestions(mockSetup)).to.be.rejectedWith( |
| 61 | + "No project ID found. Please ensure you are in a Firebase project directory or specify a project.", |
| 62 | + ); |
| 63 | + |
| 64 | + sinon.assert.notCalled(listFirebaseAppsStub); |
| 65 | + sinon.assert.notCalled(selectStub); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should throw error when no apps are found", async () => { |
| 69 | + const mockSetup = { projectId: "test-project" } as Setup; |
| 70 | + listFirebaseAppsStub.resolves([]); // No apps |
| 71 | + |
| 72 | + await expect(init.askQuestions(mockSetup)).to.be.rejectedWith( |
| 73 | + "No Firebase apps found in this project. Please create an app first using the Firebase Console or 'firebase apps:create'.", |
| 74 | + ); |
| 75 | + |
| 76 | + sinon.assert.calledWith(listFirebaseAppsStub, "test-project", AppPlatform.ANY); |
| 77 | + sinon.assert.notCalled(selectStub); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe("actuate", () => { |
| 82 | + let setup: Setup; |
| 83 | + let parseAppIdStub: sinon.SinonStub; |
| 84 | + let provisionFirebaseAppStub: sinon.SinonStub; |
| 85 | + let getConfigFileNameStub: sinon.SinonStub; |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + setup = { |
| 89 | + config: {}, |
| 90 | + rcfile: { projects: {}, targets: {}, etags: {} }, |
| 91 | + featureInfo: { |
| 92 | + ailogic: { |
| 93 | + appId: "1:123456789:android:abcdef123456", |
| 94 | + }, |
| 95 | + }, |
| 96 | + projectId: "test-project", |
| 97 | + instructions: [], |
| 98 | + } as Setup; |
| 99 | + |
| 100 | + // Stub only the functions used in actuate (no validation stubs) |
| 101 | + parseAppIdStub = sandbox.stub(utils, "parseAppId"); |
| 102 | + provisionFirebaseAppStub = sandbox.stub(provision, "provisionFirebaseApp"); |
| 103 | + getConfigFileNameStub = sandbox.stub(utils, "getConfigFileName"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should return early if no ailogic feature info", async () => { |
| 107 | + setup.featureInfo = {}; |
| 108 | + |
| 109 | + await init.actuate(setup); |
| 110 | + |
| 111 | + // No stubs should be called |
| 112 | + sinon.assert.notCalled(parseAppIdStub); |
| 113 | + sinon.assert.notCalled(provisionFirebaseAppStub); |
| 114 | + }); |
| 115 | + |
| 116 | + it("should provision existing app successfully", async () => { |
| 117 | + const mockAppInfo = { |
| 118 | + projectNumber: "123456789", |
| 119 | + appId: "1:123456789:android:abcdef123456", |
| 120 | + platform: AppPlatform.ANDROID, |
| 121 | + }; |
| 122 | + const mockConfigContent = '{"config": "content"}'; |
| 123 | + const base64Config = Buffer.from(mockConfigContent).toString("base64"); |
| 124 | + |
| 125 | + parseAppIdStub.returns(mockAppInfo); |
| 126 | + provisionFirebaseAppStub.resolves({ configData: base64Config }); |
| 127 | + getConfigFileNameStub.returns("google-services.json"); |
| 128 | + |
| 129 | + await init.actuate(setup); |
| 130 | + |
| 131 | + sinon.assert.calledWith(parseAppIdStub, "1:123456789:android:abcdef123456"); |
| 132 | + sinon.assert.calledOnce(provisionFirebaseAppStub); |
| 133 | + |
| 134 | + expect(setup.instructions).to.include( |
| 135 | + "Firebase AI Logic has been enabled for existing ANDROID app: 1:123456789:android:abcdef123456", |
| 136 | + ); |
| 137 | + expect(setup.instructions).to.include( |
| 138 | + "Save the following content as google-services.json in your app's root directory:", |
| 139 | + ); |
| 140 | + expect(setup.instructions).to.include(mockConfigContent); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should throw error if no project ID found", async () => { |
| 144 | + setup.projectId = undefined; |
| 145 | + |
| 146 | + await expect(init.actuate(setup)).to.be.rejectedWith( |
| 147 | + "AI Logic setup failed: No project ID found. Please ensure you are in a Firebase project directory or specify a project.", |
| 148 | + ); |
| 149 | + |
| 150 | + sinon.assert.calledOnce(parseAppIdStub); |
| 151 | + sinon.assert.notCalled(provisionFirebaseAppStub); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should handle provisioning errors gracefully", async () => { |
| 155 | + const mockAppInfo = { |
| 156 | + projectNumber: "123456789", |
| 157 | + appId: "1:123456789:android:abcdef123456", |
| 158 | + platform: AppPlatform.ANDROID, |
| 159 | + }; |
| 160 | + |
| 161 | + parseAppIdStub.returns(mockAppInfo); |
| 162 | + provisionFirebaseAppStub.throws(new Error("Provisioning API failed")); |
| 163 | + |
| 164 | + await expect(init.actuate(setup)).to.be.rejectedWith( |
| 165 | + "AI Logic setup failed: Provisioning API failed", |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should include config file content in instructions for iOS", async () => { |
| 170 | + if (setup.featureInfo?.ailogic) { |
| 171 | + setup.featureInfo.ailogic.appId = "1:123456789:ios:abcdef123456"; |
| 172 | + } |
| 173 | + const mockAppInfo = { |
| 174 | + projectNumber: "123456789", |
| 175 | + appId: "1:123456789:ios:abcdef123456", |
| 176 | + platform: AppPlatform.IOS, |
| 177 | + }; |
| 178 | + const mockConfigContent = '<?xml version="1.0" encoding="UTF-8"?>'; |
| 179 | + const base64Config = Buffer.from(mockConfigContent).toString("base64"); |
| 180 | + |
| 181 | + parseAppIdStub.returns(mockAppInfo); |
| 182 | + provisionFirebaseAppStub.resolves({ configData: base64Config }); |
| 183 | + getConfigFileNameStub.returns("GoogleService-Info.plist"); |
| 184 | + |
| 185 | + await init.actuate(setup); |
| 186 | + |
| 187 | + expect(setup.instructions).to.include( |
| 188 | + "Firebase AI Logic has been enabled for existing IOS app: 1:123456789:ios:abcdef123456", |
| 189 | + ); |
| 190 | + expect(setup.instructions).to.include( |
| 191 | + "Save the following content as GoogleService-Info.plist in your app's root directory:", |
| 192 | + ); |
| 193 | + expect(setup.instructions).to.include(mockConfigContent); |
| 194 | + }); |
| 195 | + |
| 196 | + it("should include platform placement guidance in instructions", async () => { |
| 197 | + const mockAppInfo = { |
| 198 | + projectNumber: "123456789", |
| 199 | + appId: "1:123456789:android:abcdef123456", |
| 200 | + platform: AppPlatform.ANDROID, |
| 201 | + }; |
| 202 | + const mockConfigContent = '{"config": "content"}'; |
| 203 | + const base64Config = Buffer.from(mockConfigContent).toString("base64"); |
| 204 | + |
| 205 | + parseAppIdStub.returns(mockAppInfo); |
| 206 | + provisionFirebaseAppStub.resolves({ configData: base64Config }); |
| 207 | + getConfigFileNameStub.returns("google-services.json"); |
| 208 | + |
| 209 | + await init.actuate(setup); |
| 210 | + |
| 211 | + expect(setup.instructions).to.include( |
| 212 | + "Place this config file in the appropriate location for your platform.", |
| 213 | + ); |
| 214 | + }); |
| 215 | + }); |
| 216 | +}); |
0 commit comments