feat(billing): bill a run on rented hardware to the box owner's team

A fleet machine belongs to an organisation, which is not something that
pays, so fleet runs were neither checked nor metered. The payer is now
the box owner's personal team, and the meter that starts and stops with
a run asks the same function the start check does, so the team checked
and the team billed cannot differ.
This commit is contained in:
Wanjohi
2026-10-07 09:25:32 +03:00
parent 08bbb22a1c
commit 2da95deb36
3 changed files with 59 additions and 14 deletions
+39
View File
@@ -57,6 +57,7 @@ function createBox(headers: Record<string, string>, body: object) {
}
afterAll(async () => {
await sql`delete from burn_segment where session_id in (select s.id from session s join box b on b.id = s.box_id where b.user_id = any(${createdUserIds}))`;
await sql`delete from session where box_id in (select id from box where user_id = any(${createdUserIds}))`;
await sql`delete from box where user_id = any(${createdUserIds})`;
await sql`delete from game where id = any(${createdGameIds})`;
@@ -167,3 +168,41 @@ describe('Entitlement on a fleet machine', () => {
expect((await may(p.owner.userId)).entitled).toBe(false);
});
});
describe('Billing a run on the fleet', () => {
test('a live fleet run accrues against the box owner’s personal team', async () => {
const f = await fleet(1);
useFleet(f.organisationId);
const machineId = f.machines[0]!.id;
const p = await person('bill-fleet');
const box = ((await (await createBox(p.headers, { on: 'fleet' })).json()) as any).data;
const [game] = await Game.upsert({
id: Identifier.ascending('game'),
steamAppId: 990002,
slug: 'fleet-bill',
name: 'Fleet Bill'
});
createdGameIds.push(game!.id);
const run = await Session.request({
id: Identifier.ascending('session'),
boxId: box.id,
gameId: game!.id,
linkedAccountId: p.owner.linkedAccountId
});
const claimToken = 'fleet-bill-claim-token-0001';
for (const state of ['starting', 'live'] as const) {
const r = await Session.transition({ id: run.id, machineId, state, claimToken });
expect(r.outcome).toBe('moved');
}
const open = await sql`select team_id, ended_at from burn_segment where session_id = ${run.id}`;
expect(open.length).toBe(1);
expect(open[0]!.team_id).toBe(p.owner.teamId);
expect(open[0]!.ended_at).toBeNull();
await Session.transition({ id: run.id, machineId, state: 'ended', claimToken });
const closed = await sql`select ended_at from burn_segment where session_id = ${run.id}`;
expect(closed[0]!.ended_at).not.toBeNull();
});
});