|
| 1 | +import { expect } from "chai"; |
| 2 | +import * as sinon from "sinon"; |
| 3 | +import { get_user } from "./get_user"; |
| 4 | +import * as auth from "../../../gcp/auth"; |
| 5 | +import * as util from "../../util"; |
| 6 | +import { ServerToolContext } from "../../tool"; |
| 7 | + |
| 8 | +describe("get_user tool", () => { |
| 9 | + const projectId = "test-project"; |
| 10 | + const email = "test@example.com"; |
| 11 | + const phoneNumber = "+11234567890"; |
| 12 | + const uid = "test-uid"; |
| 13 | + const user = { uid, email, phoneNumber }; |
| 14 | + |
| 15 | + let findUserStub: sinon.SinonStub; |
| 16 | + let mcpErrorStub: sinon.SinonStub; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + findUserStub = sinon.stub(auth, "findUser"); |
| 20 | + mcpErrorStub = sinon.stub(util, "mcpError"); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + sinon.restore(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should return an error if no identifier is provided", async () => { |
| 28 | + await get_user.fn({}, { projectId } as ServerToolContext); |
| 29 | + expect(mcpErrorStub).to.be.calledWith("No user identifier supplied in auth_get_user tool"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should get a user by email", async () => { |
| 33 | + findUserStub.resolves(user); |
| 34 | + const result = await get_user.fn({ email }, { projectId } as ServerToolContext); |
| 35 | + expect(findUserStub).to.be.calledWith(projectId, email, undefined, undefined); |
| 36 | + expect(result).to.deep.equal(util.toContent(user)); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should get a user by phone number", async () => { |
| 40 | + findUserStub.resolves(user); |
| 41 | + const result = await get_user.fn({ phone_number: phoneNumber }, { |
| 42 | + projectId, |
| 43 | + } as ServerToolContext); |
| 44 | + expect(findUserStub).to.be.calledWith(projectId, undefined, phoneNumber, undefined); |
| 45 | + expect(result).to.deep.equal(util.toContent(user)); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should get a user by UID", async () => { |
| 49 | + findUserStub.resolves(user); |
| 50 | + const result = await get_user.fn({ uid }, { projectId } as ServerToolContext); |
| 51 | + expect(findUserStub).to.be.calledWith(projectId, undefined, undefined, uid); |
| 52 | + expect(result).to.deep.equal(util.toContent(user)); |
| 53 | + }); |
| 54 | + |
| 55 | + it("returns an error when no user exists", async () => { |
| 56 | + findUserStub.rejects(new Error("No users found")); |
| 57 | + await get_user.fn({ uid: "nonexistant@email.com" }, { projectId } as ServerToolContext); |
| 58 | + expect(mcpErrorStub).to.be.calledWith("Unable to find user"); |
| 59 | + }); |
| 60 | +}); |
0 commit comments