|
| 1 | +const should = require('should') // eslint-disable-line no-unused-vars |
| 2 | + |
| 3 | +const { expectToolMatchesRoute, createExpertMcpToken, toolFinder, recordingInject } = require('../../../../../../lib/mcpToolEquivalence') |
| 4 | + |
| 5 | +const FF_UTIL = require('flowforge-test-utils') |
| 6 | + |
| 7 | +const tools = FF_UTIL.require('forge/ee/lib/mcp/tools/billing') |
| 8 | +const findTool = toolFinder(tools) |
| 9 | + |
| 10 | +describe('MCP Billing Tools', function () { |
| 11 | + describe('platform_get_team_billing', function () { |
| 12 | + const tool = findTool('platform_get_team_billing') |
| 13 | + |
| 14 | + it('has read-only, non-destructive annotations', function () { |
| 15 | + tool.annotations.readOnlyHint.should.equal(true) |
| 16 | + tool.annotations.destructiveHint.should.equal(false) |
| 17 | + }) |
| 18 | + |
| 19 | + it('exposes the expected input schema keys', function () { |
| 20 | + Object.keys(tool.inputSchema).should.eql(['teamId']) |
| 21 | + }) |
| 22 | + |
| 23 | + it('requests the team billing endpoint', async function () { |
| 24 | + const { calls, inject } = recordingInject() |
| 25 | + await tool.handler({ teamId: 'team123' }, { inject }) |
| 26 | + calls.should.have.length(1) |
| 27 | + calls[0].method.should.equal('GET') |
| 28 | + calls[0].url.should.equal('/ee/billing/teams/team123') |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + describe('platform_get_team_customer_portal', function () { |
| 33 | + const tool = findTool('platform_get_team_customer_portal') |
| 34 | + |
| 35 | + it('has read-only, non-destructive annotations', function () { |
| 36 | + tool.annotations.readOnlyHint.should.equal(true) |
| 37 | + tool.annotations.destructiveHint.should.equal(false) |
| 38 | + }) |
| 39 | + |
| 40 | + it('exposes the expected input schema keys', function () { |
| 41 | + Object.keys(tool.inputSchema).should.eql(['teamId']) |
| 42 | + }) |
| 43 | + |
| 44 | + it('requests the team customer-portal endpoint', async function () { |
| 45 | + const { calls, inject } = recordingInject() |
| 46 | + await tool.handler({ teamId: 'team123' }, { inject }) |
| 47 | + calls.should.have.length(1) |
| 48 | + calls[0].method.should.equal('GET') |
| 49 | + calls[0].url.should.equal('/ee/billing/teams/team123/customer-portal') |
| 50 | + }) |
| 51 | + |
| 52 | + it('surfaces the portal URL from the response location header', async function () { |
| 53 | + const inject = async () => ({ statusCode: 200, headers: { location: 'https://billing.stripe.com/session/abc' } }) |
| 54 | + const response = await tool.handler({ teamId: 'team123' }, { inject }) |
| 55 | + response.statusCode.should.equal(200) |
| 56 | + response.json().should.eql({ url: 'https://billing.stripe.com/session/abc' }) |
| 57 | + }) |
| 58 | + |
| 59 | + it('returns the error response unchanged when the route fails', async function () { |
| 60 | + const errorResponse = { statusCode: 404, json: () => ({ error: 'not found' }) } |
| 61 | + const inject = async () => errorResponse |
| 62 | + const response = await tool.handler({ teamId: 'team123' }, { inject }) |
| 63 | + response.should.equal(errorResponse) |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + describe('Integration smoke', function () { |
| 68 | + const setup = require('../../../setup') |
| 69 | + |
| 70 | + let app |
| 71 | + let stripe |
| 72 | + let token |
| 73 | + |
| 74 | + before(async function () { |
| 75 | + stripe = setup.setupStripe({ |
| 76 | + billingPortal: { |
| 77 | + sessions: { |
| 78 | + create: async () => ({ url: 'https://billing.stripe.com/p/session/test_123' }) |
| 79 | + } |
| 80 | + } |
| 81 | + }) |
| 82 | + app = await setup({ |
| 83 | + ai: { enabled: true }, |
| 84 | + expert: { enabled: true } |
| 85 | + }) |
| 86 | + token = await createExpertMcpToken(app) |
| 87 | + }) |
| 88 | + |
| 89 | + after(async function () { |
| 90 | + await app.close() |
| 91 | + setup.resetStripe() |
| 92 | + }) |
| 93 | + |
| 94 | + it('platform_get_team_billing returns the team subscription details', async function () { |
| 95 | + const inject = (o) => app.inject({ ...o, headers: { ...(o.headers || {}), authorization: `Bearer ${token}` } }) |
| 96 | + const tool = findTool('platform_get_team_billing') |
| 97 | + |
| 98 | + const response = await tool.handler({ teamId: app.team.hashid }, { inject }) |
| 99 | + |
| 100 | + // app.team has an active subscription via the Stripe mock |
| 101 | + response.statusCode.should.equal(200) |
| 102 | + const body = response.json() |
| 103 | + body.should.have.property('items').which.is.an.Array() |
| 104 | + body.should.have.property('customer') |
| 105 | + should.equal(stripe.subscriptions.retrieve.called, true) |
| 106 | + }) |
| 107 | + |
| 108 | + it('platform_get_team_billing matches the team billing route response exactly', async function () { |
| 109 | + const inject = (o) => app.inject({ ...o, headers: { ...(o.headers || {}), authorization: `Bearer ${token}` } }) |
| 110 | + const tool = findTool('platform_get_team_billing') |
| 111 | + |
| 112 | + await expectToolMatchesRoute(inject, tool, { teamId: app.team.hashid }, { |
| 113 | + method: 'GET', |
| 114 | + url: `/ee/billing/teams/${app.team.hashid}` |
| 115 | + }) |
| 116 | + }) |
| 117 | + |
| 118 | + it('platform_get_team_customer_portal matches the customer-portal route redirect location', async function () { |
| 119 | + const inject = (o) => app.inject({ ...o, headers: { ...(o.headers || {}), authorization: `Bearer ${token}` } }) |
| 120 | + const tool = findTool('platform_get_team_customer_portal') |
| 121 | + |
| 122 | + const { routeResponse } = await expectToolMatchesRoute(inject, tool, { teamId: app.team.hashid }, { |
| 123 | + method: 'GET', |
| 124 | + url: `/ee/billing/teams/${app.team.hashid}/customer-portal`, |
| 125 | + transform: (r) => (r.statusCode >= 400 |
| 126 | + ? { statusCode: r.statusCode, body: r.json() } |
| 127 | + : { statusCode: 200, body: { url: r.headers.location } }) |
| 128 | + }) |
| 129 | + |
| 130 | + // Stripe mock's billing portal session is wired to a fixed URL |
| 131 | + routeResponse.headers.location.should.equal('https://billing.stripe.com/p/session/test_123') |
| 132 | + }) |
| 133 | + }) |
| 134 | +}) |
0 commit comments