mirror of
https://github.com/nestriness/nestri.git
synced 2026-09-23 19:18:18 +03:00
feat(core): a box is a row, a session is the billing unit
Migration 1 of 0048, and the first of the seven weeks — nothing about a live
feed works without these two tables, so it is not a cleanup during them.
box a VM someone owns: an id that is also its DNS label, an editable
label, an owning user, the machine it sits on, a tier and a state.
Owned by a person and placed on a team's hardware, which are two
different relationships, hence both userId and machineId.
session one run of one box by one linked Steam account, and what costs
money. Separate from box because the ticket changes after bind as
addresses are discovered — the vsock contract calls it "a stream,
not one value" — so it is a column a client polls, not a value it
is handed once.
Box states are neslet's own three and no more. `starting` and `stopping` are
the obvious additions and both are omitted because nothing would ever write
them; a failed box is `stopped` with stopClean false, which is how neslet
models it too.
The generated migration would have failed on live rows in three ways, so it
is hand-written and tested against a database seeded at the old schema:
- machine.team_id becomes notNull, and *every existing row is null* because
the old registration path passed null. Personal teams are backfilled for
machine owners first, reusing a team they already own rather than minting
a second, with the owner membership row repaired where missing.
- game_download.host_id becomes a foreign key. It held free-form strings,
so unattributable rows are deleted before the cast — the only destructive
statement here, and a considered loss: it is a progress report neslet
re-derives from disk.
- Team.createPersonal was written and documented in packages/core/CLAUDE.md
as part of the login flow and never actually called, so no user has a
team. ensurePersonal is idempotent and now runs on every login, which is
what backfills accounts the migration does not reach.
Verified on a seeded legacy database: three null-team machines backfilled, an
existing team reused rather than duplicated, a blank display name handled, and
both unattributable download rows dropped while the attributable one survived.
Also fixes two things this work ran into rather than caused:
- Database.client() built a new postgres pool on every call, and use()
called it twice per invocation — pools of ten connections held for a 30s
idle timeout. Invisible in a Worker where requests are short; the suite
crossed 100 connections and Postgres said "sorry, too many clients
already" in whichever file ran last, which reads as a flaky test rather
than a leak. Now one pool per connection string.
- download.test.ts asserted against `hst_…` host ids, which is exactly the
unattributable row the new foreign key exists to refuse.
There is no "no team" any more: PATCH /machine/:id took teamId null to mean
"mine alone" and now requires a team, because the personal team is the one to
name. Its test is updated to the new contract rather than deleted.
113 → 128 tests, 0 fail.
This commit is contained in:
175
packages/core/src/session/session.test.ts
Normal file
175
packages/core/src/session/session.test.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
import { afterAll, describe, expect, test } from 'bun:test';
|
||||
|
||||
import { Box } from '../box/index.js';
|
||||
import { Fixtures } from '../db/fixtures.js';
|
||||
import { testDb } from '../db/test.js';
|
||||
import { Game } from '../game/index.js';
|
||||
import { Identifier } from '../id.js';
|
||||
import { Session } from './index.js';
|
||||
|
||||
const sql = testDb();
|
||||
|
||||
const createdUserIds: string[] = [];
|
||||
const createdGameIds: string[] = [];
|
||||
|
||||
async function newOwner(label: string) {
|
||||
const o = await Fixtures.owner(label);
|
||||
createdUserIds.push(o.userId);
|
||||
return o;
|
||||
}
|
||||
|
||||
async function newGame(steamAppId: number): Promise<string> {
|
||||
const [row] = await Game.upsert({
|
||||
id: Identifier.ascending('game'),
|
||||
steamAppId,
|
||||
slug: `session-test-${steamAppId}`,
|
||||
name: `Session Test ${steamAppId}`
|
||||
});
|
||||
if (!row) throw new Error('expected a game row');
|
||||
createdGameIds.push(row.id);
|
||||
return row.id;
|
||||
}
|
||||
|
||||
/** A user, a team, a machine, a box and a game — everything a session needs. */
|
||||
async function scene(label: string, steamAppId: number) {
|
||||
const owner = await newOwner(label);
|
||||
const machineId = await Fixtures.machine(owner);
|
||||
const box = await Box.create({
|
||||
id: Identifier.ascending('box'),
|
||||
userId: owner.userId,
|
||||
machineId,
|
||||
label,
|
||||
tier: 'sm'
|
||||
});
|
||||
return { owner, box, gameId: await newGame(steamAppId) };
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
if (createdUserIds.length > 0) {
|
||||
// session cascades from box; box has to precede the machine, which
|
||||
// cascades from the user.
|
||||
await sql`delete from "box" where user_id in ${sql(createdUserIds)}`;
|
||||
await sql`delete from "user" where id in ${sql(createdUserIds)}`;
|
||||
createdUserIds.length = 0;
|
||||
}
|
||||
if (createdGameIds.length > 0) {
|
||||
await sql`delete from "game" where id in ${sql(createdGameIds)}`;
|
||||
createdGameIds.length = 0;
|
||||
}
|
||||
});
|
||||
|
||||
describe('Session', () => {
|
||||
test('a session starts requested, with no ticket and no times', async () => {
|
||||
const { owner, box, gameId } = await scene('ses-defaults', 5400);
|
||||
|
||||
const session = await Session.create({
|
||||
id: Identifier.ascending('session'),
|
||||
boxId: box.id,
|
||||
gameId,
|
||||
linkedAccountId: owner.linkedAccountId
|
||||
});
|
||||
|
||||
expect(session.state).toBe('requested');
|
||||
expect(session.ticket).toBeNull();
|
||||
expect(session.timeStarted).toBeNull();
|
||||
expect(session.timeStopped).toBeNull();
|
||||
});
|
||||
|
||||
test('the ticket is a stream: a later one replaces the first', async () => {
|
||||
const { owner, box, gameId } = await scene('ses-ticket', 5401);
|
||||
const session = await Session.create({
|
||||
id: Identifier.ascending('session'),
|
||||
boxId: box.id,
|
||||
gameId,
|
||||
linkedAccountId: owner.linkedAccountId
|
||||
});
|
||||
|
||||
expect((await Session.setTicket({ id: session.id, ticket: 'ticket-one' }))?.ticket).toBe(
|
||||
'ticket-one'
|
||||
);
|
||||
// The vsock contract calls the ticket "a stream, not one value" — a second
|
||||
// ticket is a better address for the same session, not a new session.
|
||||
expect((await Session.setTicket({ id: session.id, ticket: 'ticket-two' }))?.ticket).toBe(
|
||||
'ticket-two'
|
||||
);
|
||||
expect(await Session.listByBox(box.id)).toHaveLength(1);
|
||||
});
|
||||
|
||||
test('going live stamps a start time, and a repeat report does not move it', async () => {
|
||||
const { owner, box, gameId } = await scene('ses-live', 5402);
|
||||
const session = await Session.create({
|
||||
id: Identifier.ascending('session'),
|
||||
boxId: box.id,
|
||||
gameId,
|
||||
linkedAccountId: owner.linkedAccountId
|
||||
});
|
||||
|
||||
const live = await Session.setState({ id: session.id, state: 'live', errorMessage: null });
|
||||
expect(live?.state).toBe('live');
|
||||
expect(live?.timeStarted).not.toBeNull();
|
||||
|
||||
// This is the billing property: a duplicate `live` must not extend a
|
||||
// session somebody is charged for.
|
||||
const again = await Session.setState({ id: session.id, state: 'live', errorMessage: null });
|
||||
expect(again?.timeStarted).toBe(live!.timeStarted);
|
||||
});
|
||||
|
||||
test('ending stamps a stop time once, and failing records why', async () => {
|
||||
const { owner, box, gameId } = await scene('ses-end', 5403);
|
||||
const session = await Session.create({
|
||||
id: Identifier.ascending('session'),
|
||||
boxId: box.id,
|
||||
gameId,
|
||||
linkedAccountId: owner.linkedAccountId
|
||||
});
|
||||
await Session.setState({ id: session.id, state: 'live', errorMessage: null });
|
||||
|
||||
const failed = await Session.setState({
|
||||
id: session.id,
|
||||
state: 'failed',
|
||||
errorMessage: 'steam guard timed out'
|
||||
});
|
||||
expect(failed?.state).toBe('failed');
|
||||
expect(failed?.errorMessage).toBe('steam guard timed out');
|
||||
expect(failed?.timeStopped).not.toBeNull();
|
||||
|
||||
const ended = await Session.setState({ id: session.id, state: 'ended', errorMessage: null });
|
||||
expect(ended?.timeStopped).toBe(failed!.timeStopped);
|
||||
// A state that is not `failed` carries no explanation.
|
||||
expect(ended?.errorMessage).toBeNull();
|
||||
});
|
||||
|
||||
test('the active session is the one that has not stopped', async () => {
|
||||
const { owner, box, gameId } = await scene('ses-active', 5404);
|
||||
const first = await Session.create({
|
||||
id: Identifier.ascending('session'),
|
||||
boxId: box.id,
|
||||
gameId,
|
||||
linkedAccountId: owner.linkedAccountId
|
||||
});
|
||||
await Session.setState({ id: first.id, state: 'ended', errorMessage: null });
|
||||
|
||||
expect(await Session.activeForBox(box.id)).toBeNull();
|
||||
|
||||
const second = await Session.create({
|
||||
id: Identifier.ascending('session'),
|
||||
boxId: box.id,
|
||||
gameId,
|
||||
linkedAccountId: owner.linkedAccountId
|
||||
});
|
||||
expect((await Session.activeForBox(box.id))?.id).toBe(second.id);
|
||||
});
|
||||
|
||||
test('deleting a box takes its sessions with it', async () => {
|
||||
const { owner, box, gameId } = await scene('ses-cascade', 5405);
|
||||
await Session.create({
|
||||
id: Identifier.ascending('session'),
|
||||
boxId: box.id,
|
||||
gameId,
|
||||
linkedAccountId: owner.linkedAccountId
|
||||
});
|
||||
|
||||
await sql`delete from "box" where id = ${box.id}`;
|
||||
expect(await Session.listByBox(box.id)).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user