Skip to content

Commit 69ca585

Browse files
More Jules generated tests (#8978)
* feat: Improve test coverage for emulator controller This change adds a new test case to `src/emulator/controller.spec.ts` to verify that the functions emulator does not start if the functions source directory is not configured, even when specified in the `--only` flag. This also fixes a failing test in the same file by correcting the mock options setup. * Reverting some unwanted eslint change * PR fix --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent fd963fc commit 69ca585

File tree

3 files changed

+152
-0
lines changed

3 files changed

+152
-0
lines changed

src/auth.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { expect } from "chai";
22
import * as sinon from "sinon";
33

44
import {
5+
assertAccount,
56
getAdditionalAccounts,
67
getAllAccounts,
78
getGlobalDefaultAccount,
@@ -148,4 +149,30 @@ describe("auth", () => {
148149
expect(account).to.deep.equal(additionalUser2);
149150
});
150151
});
152+
153+
describe("assertAccount", () => {
154+
const defaultAccount: Account = {
155+
user: {
156+
157+
},
158+
tokens: {
159+
access_token: "abc1234",
160+
},
161+
};
162+
163+
beforeEach(() => {
164+
configstore.set("user", defaultAccount.user);
165+
configstore.set("tokens", defaultAccount.tokens);
166+
});
167+
168+
it("should not throw an error if the account exists", () => {
169+
expect(() => assertAccount("[email protected]")).to.not.throw();
170+
});
171+
172+
it("should throw an error if the account does not exist", () => {
173+
expect(() => assertAccount("[email protected]")).to.throw(
174+
"Account [email protected] does not exist",
175+
);
176+
});
177+
});
151178
});

src/emulator/controller.spec.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@ import { Emulators } from "./types";
22
import { EmulatorRegistry } from "./registry";
33
import { expect } from "chai";
44
import { FakeEmulator } from "./testing/fakeEmulator";
5+
import { shouldStart } from "./controller";
6+
import { Options } from "../options";
7+
8+
function createMockOptions(
9+
only: string | undefined,
10+
configValues: { [key: string]: any },
11+
): Options {
12+
const config = {
13+
get: (key: string) => configValues[key],
14+
has: (key: string) => !!configValues[key],
15+
src: {
16+
emulators: configValues,
17+
functions: configValues.functions,
18+
},
19+
};
20+
return {
21+
only,
22+
config,
23+
project: "test-project",
24+
} as any;
25+
}
526

627
describe("EmulatorController", () => {
728
afterEach(async () => {
@@ -19,4 +40,68 @@ describe("EmulatorController", () => {
1940
expect(EmulatorRegistry.isRunning(name)).to.be.true;
2041
expect(EmulatorRegistry.getInfo(name)!.port).to.eql(fake.getInfo().port);
2142
});
43+
44+
describe("shouldStart", () => {
45+
it("should start the hub if a project is specified", () => {
46+
const options = { project: "test-project" } as Options;
47+
expect(shouldStart(options, Emulators.HUB)).to.be.true;
48+
});
49+
50+
it("should not start the hub if no project is specified", () => {
51+
const options = {} as Options;
52+
expect(shouldStart(options, Emulators.HUB)).to.be.false;
53+
});
54+
55+
it("should start the UI if options.ui is true", () => {
56+
const options = createMockOptions(undefined, {});
57+
options.ui = true;
58+
expect(shouldStart(options, Emulators.UI)).to.be.true;
59+
});
60+
61+
it("should start the UI if a project is specified and a UI-supported emulator is running", () => {
62+
const options = createMockOptions("firestore", { firestore: {} });
63+
expect(shouldStart(options, Emulators.UI)).to.be.true;
64+
});
65+
66+
it("should not start the UI if no project is specified", () => {
67+
const options = createMockOptions("firestore", { firestore: {} });
68+
delete options.project;
69+
expect(shouldStart(options, Emulators.UI)).to.be.false;
70+
});
71+
72+
it("should not start the UI if no UI-supported emulator is running", () => {
73+
const options = createMockOptions(undefined, {});
74+
expect(shouldStart(options, Emulators.UI)).to.be.false;
75+
});
76+
77+
it("should start an emulator if it's in the only string", () => {
78+
const options = createMockOptions("functions,hosting", {
79+
functions: { source: "functions" },
80+
hosting: {},
81+
});
82+
expect(shouldStart(options, Emulators.FUNCTIONS)).to.be.true;
83+
});
84+
85+
it("should not start an emulator if it's not in the only string", () => {
86+
const options = createMockOptions("functions", {
87+
functions: { source: "functions" },
88+
hosting: {},
89+
});
90+
expect(shouldStart(options, Emulators.HOSTING)).to.be.false;
91+
});
92+
93+
it("should not start an emulator if it's in the only string but has no config", () => {
94+
const options = createMockOptions("hosting,functions", {
95+
hosting: {},
96+
});
97+
expect(shouldStart(options, Emulators.FUNCTIONS)).to.be.false;
98+
});
99+
100+
it("should not start functions emulator if source directory is not configured", () => {
101+
const options = createMockOptions("functions", {
102+
functions: {}, // Config is present, but no source
103+
});
104+
expect(shouldStart(options, Emulators.FUNCTIONS)).to.be.false;
105+
});
106+
});
22107
}).timeout(2000);

src/track.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect } from "chai";
2+
import * as sinon from "sinon";
3+
4+
import { usageEnabled } from "./track";
5+
import { configstore } from "./configstore";
6+
7+
describe("track", () => {
8+
const sandbox: sinon.SinonSandbox = sinon.createSandbox();
9+
10+
afterEach(() => {
11+
sandbox.restore();
12+
delete process.env.IS_FIREBASE_CLI;
13+
delete process.env.IS_FIREBASE_MCP;
14+
});
15+
16+
describe("usageEnabled", () => {
17+
it("should return true if IS_FIREBASE_CLI and usage are true", () => {
18+
process.env.IS_FIREBASE_CLI = "true";
19+
sandbox.stub(configstore, "get").withArgs("usage").returns(true);
20+
expect(usageEnabled()).to.be.true;
21+
});
22+
23+
it("should return false if IS_FIREBASE_CLI is not set", () => {
24+
sandbox.stub(configstore, "get").withArgs("usage").returns(true);
25+
expect(usageEnabled()).to.be.false;
26+
});
27+
28+
it("should return false if usage is false", () => {
29+
process.env.IS_FIREBASE_CLI = "true";
30+
sandbox.stub(configstore, "get").withArgs("usage").returns(false);
31+
expect(usageEnabled()).to.be.false;
32+
});
33+
34+
it("should return true if IS_FIREBASE_MCP and usage are true", () => {
35+
process.env.IS_FIREBASE_MCP = "true";
36+
sandbox.stub(configstore, "get").withArgs("usage").returns(true);
37+
expect(usageEnabled()).to.be.true;
38+
});
39+
});
40+
});

0 commit comments

Comments
 (0)