mirror of
https://github.com/nestriness/nestri.git
synced 2026-09-19 17:25:19 +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:
82
packages/core/src/team/team.test.ts
Normal file
82
packages/core/src/team/team.test.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { afterAll, describe, expect, test } from 'bun:test';
|
||||
|
||||
import { Actor } from '../actor.js';
|
||||
import { Fixtures } from '../db/fixtures.js';
|
||||
import { testDb } from '../db/test.js';
|
||||
import { Identifier } from '../id.js';
|
||||
import { Member } from './member.js';
|
||||
import { Team } from './index.js';
|
||||
|
||||
const sql = testDb();
|
||||
|
||||
const createdUserIds: string[] = [];
|
||||
|
||||
async function newOwner(label: string) {
|
||||
const o = await Fixtures.owner(label);
|
||||
createdUserIds.push(o.userId);
|
||||
return o;
|
||||
}
|
||||
|
||||
afterAll(async () => {
|
||||
if (createdUserIds.length > 0) {
|
||||
await sql`delete from "user" where id in ${sql(createdUserIds)}`;
|
||||
createdUserIds.length = 0;
|
||||
}
|
||||
});
|
||||
|
||||
describe('Team.ensurePersonal', () => {
|
||||
test('a new user gets exactly one team, and owns it', async () => {
|
||||
const owner = await newOwner('team-first');
|
||||
|
||||
const team = await Team.personalFor(owner.userId);
|
||||
expect(team?.id).toBe(owner.teamId);
|
||||
expect(team?.ownerId).toBe(owner.userId);
|
||||
|
||||
const memberships = await Member.listByUser(owner.userId);
|
||||
expect(memberships).toHaveLength(1);
|
||||
expect(memberships[0]!.role).toBe('owner');
|
||||
});
|
||||
|
||||
test('it is idempotent, because it runs on every login', async () => {
|
||||
const owner = await newOwner('team-idempotent');
|
||||
|
||||
// The auth worker calls this each time somebody signs in, not only when
|
||||
// the user is created — that is what backfills accounts made before the
|
||||
// call existed. A second call must not mint a second team.
|
||||
const again = await Actor.with(
|
||||
{ type: 'user', properties: { userID: owner.userId, linkedAccountID: owner.linkedAccountId } },
|
||||
() => Team.ensurePersonal({ displayName: 'team-idempotent' })
|
||||
);
|
||||
|
||||
expect(again).toBe(owner.teamId);
|
||||
const rows = await sql`select count(*)::int as n from team where owner_id = ${owner.userId}`;
|
||||
expect(rows[0]!.n).toBe(1);
|
||||
});
|
||||
|
||||
test('a user who predates the personal team gets one on next login', async () => {
|
||||
// The legacy row the 0007 migration and this call between them repair: a
|
||||
// user created by Steam sign-in before `ensurePersonal` was ever wired up.
|
||||
const userId = Identifier.ascending('user');
|
||||
createdUserIds.push(userId);
|
||||
await sql`insert into "user" (id, name, email) values (${userId}, ${'legacy'}, ${`legacy-${userId}@example.test`})`;
|
||||
|
||||
expect(await Team.personalFor(userId)).toBeNull();
|
||||
|
||||
const teamId = await Actor.with(
|
||||
{ type: 'user', properties: { userID: userId, linkedAccountID: 'lac_unused' } },
|
||||
() => Team.ensurePersonal({ displayName: 'legacy' })
|
||||
);
|
||||
|
||||
expect(teamId).toBeTruthy();
|
||||
expect((await Team.personalFor(userId))?.id).toBe(teamId);
|
||||
});
|
||||
|
||||
test('two users with the same display name get distinct slugs', async () => {
|
||||
const a = await newOwner('same-name');
|
||||
const b = await newOwner('same-name');
|
||||
|
||||
const teamA = await Team.personalFor(a.userId);
|
||||
const teamB = await Team.personalFor(b.userId);
|
||||
expect(teamA!.slug).not.toBe(teamB!.slug);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user