Files
netris-nestri/packages/core/src/examples.ts
T
Wanjohi b50dce2cef feat(session): a finished session says why it ended (#412)
Until now every finished run said only `ended` or `failed`. A user stop,
a trial window running out, an idle timeout, a host restart and a game
quitting were indistinguishable afterwards.

**Schema.** Migration `0026_session_end_reason` adds two nullable
columns to `session`:
- `end_reason`: one of `user`, `window`, `hours`, `idle`, `left_queue`,
`game_exited`, `launch_failed`, `host_stopped`, `host_restart`.
- `time_claimed`: when a host took the run, set on `requested →
starting`.

**API.**
- `POST /session/:id/state` takes an optional `endReason`. With `ended`
or `failed` it is stored. With `starting` or `live` it is a 400 and the
run does not move.
- Leaving it out of a terminal report stores null, so host agents that
predate this keep working.
- `endReason` and `timeClaimed` are returned on every session response.
- The API writes the reasons it decides itself: `user` when a run nobody
has claimed is stopped, and `left_queue` when a waiting run is abandoned
(the `Left the queue` message is kept).

**New transition, `starting → ended`.** A run stopped while its game is
still downloading has failed at nothing, and never reached `live`, which
is where billing starts. Until now the only way out of `starting` was
`failed`.

**Release order: this goes first.** The host agent that sends
`endReason` is a separate change. This route's body schema is strict, so
without this deployed the current API answers that agent with a 400.

**Tests.**
- API: 185 pass, 5 of them new.
- Core: 41 session tests pass, 3 of them new.
- The new tests fail when the implementation is broken on purpose.
- No new type errors.
2026-10-16 00:20:49 +03:00

207 lines
5.0 KiB
TypeScript

import { Identifier } from './id.js';
export namespace Examples {
// The width is taken from the generator rather than typed out. Counting
// twenty-six of anything by eye is a thing people get wrong once and then
// never look at again — this was one short, which made every documented id
// a value the schema that published it would reject.
export const Id = (prefix: keyof typeof Identifier.prefixes) =>
`${Identifier.prefixes[prefix]}_${'X'.repeat(Identifier.LENGTH)}`;
export const User = {
id: Id('user'),
name: 'John Doe',
email: 'johndoe@example.com',
emailVerified: true,
image: 'https://cdn.discordapp.com/avatars/xxxxxxx/xxxxxxx.png'
};
export const LinkedAccount = {
id: Id('linkedAccount'),
userId: Id('user'),
provider: 'steam',
providerAccountId: '76561197960287930',
profile: { personaname: 'John Doe', avatarfull: 'https://avatars.steamstatic.com/xxxx.jpg' }
};
export const Organisation = {
id: Id('organisation'),
name: 'Initech',
slug: 'initech',
domain: 'initech.example',
domainVerified: true
};
export const Team = {
id: Id('team'),
name: 'The A Team',
slug: 'the-a-team',
ownerId: Id('user'),
organisationId: null,
billingEmail: 'billing@example.com',
plan: 'free',
subscriptionStatus: 'active',
metadata: null
};
export const Member = {
id: Id('teamMember'),
teamId: Id('team'),
userId: Id('user'),
role: 'owner' as const
};
export const Fingerprint = {
id: Id('userFingerprint'),
userId: Id('user'),
fingerprint: 'a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4',
name: 'MacBook Air',
lastSeen: '2026-07-28T12:00:00.000Z'
};
export const PairingCode = {
id: Id('pairingCode'),
code: 'NESSH-7F2Q',
targetUserId: Id('user'),
newFingerprint: null,
expiresAt: '2026-07-28T12:10:00.000Z',
claimedAt: null,
isClaimed: false
};
export const Game = {
id: Id('game'),
steamAppId: 730,
slug: 'counter-strike-2',
name: 'Counter-Strike 2',
aliases: 'cs2 csgo',
type: 'game',
clientIcon: '5aad412d01a9b91ba0379f0b35f4eb0b69d9db08',
icon: 'f92b09dab91f1d1738f72fe0dd9be18dcc2901f9',
shortDescription: 'For 25 years...',
description: 'For over two decades...',
developers: ['Valve'],
publishers: ['Valve'],
primaryGenre: 'Action',
genres: ['Action', 'FPS'],
categories: ['Multi-player', 'Steam Achievements'],
oslist: ['windows', 'linux'],
sizeDownload: 35000000000,
sizeOnDisk: 40000000000,
controllerSupport: 'partial',
steamDeckCompat: 'perfect',
reviewScorePercent: 86,
reviewCount: 1500000,
metacriticScore: 89,
steamChangeNumber: 24605165,
publicBuildId: 12345678,
releaseDate: '2012-08-21T00:00:00.000Z',
timeEnriched: '2026-07-29T12:00:00.000Z'
};
export const Library = {
id: Id('userLibrary'),
userId: Id('user'),
gameId: Id('game'),
playtime2w: 3600,
playtimeForever: 150000,
lastPlayed: '2026-07-28T12:00:00.000Z'
};
export const Depot = {
id: Id('gameDepot'),
gameId: Id('game'),
depotId: 731,
branch: 'public',
steamManifestId: '3183503801510301321',
steamBuildId: 12345678,
installedManifestId: '3183503801510301321',
installedBuildId: 12345678,
sizeDownload: 1000,
sizeOnDisk: 2000,
status: 'complete' as const,
errorMessage: null,
oslist: 'linux'
};
export const AccessToken = {
id: Id('accessToken'),
ownerUserId: Id('user'),
teamId: null,
name: 'living-room-box',
expiresAt: null,
lastUsed: '2026-07-28T12:00:00.000Z'
};
export const Machine = {
id: Id('machine'),
ownerUserId: Id('user'),
teamId: Id('team'),
label: 'living-room-box',
slug: 'amber-otter-4821',
lastSeen: '2026-07-28T12:00:00.000Z',
endpointId: 'a'.repeat(64)
};
export const SteamEnrolment = {
machineId: Id('machine'),
userId: Id('user'),
steamId: '76561197960287930',
state: 'enrolled' as const,
enrolledAt: '2026-07-28T12:00:00.000Z',
lastOkAt: null,
revokedAt: null
};
export const Box = {
id: Id('box'),
userId: Id('user'),
machineId: Id('machine'),
label: 'living room',
tier: 'sm' as const,
state: 'created' as const,
stopReason: null,
stopClean: null,
gpu: 'NVIDIA GeForce RTX 3060',
host: 'm7k2q9',
fleet: false
};
export const Session = {
id: Id('session'),
boxId: Id('box'),
gameId: Id('game'),
linkedAccountId: Id('linkedAccount'),
state: 'live' as const,
ticket: 'nodeaaqf…',
// Shaped like the real thing so the docs do not teach a shorter one,
// and obviously not one.
claimToken: '00000000000000000000000000000000',
timeClaimed: '2026-07-28T11:59:30.000Z',
timeStarted: '2026-07-28T12:00:00.000Z',
timeStopped: null,
errorMessage: null,
endReason: null
};
export const GameDownload = {
id: Id('gameDownload'),
hostId: Id('machine'),
gameId: Id('game'),
status: 'downloading' as const,
progressBytes: 1073741824,
totalBytes: 5000000000,
timeStarted: '2026-07-28T12:00:00.000Z',
timeCompleted: null,
errorMessage: null,
gpu: 'NVIDIA GeForce RTX 3060'
};
export const WaitlistEntry = {
id: Id('waitlistEntry'),
email: 'johndoe@example.com',
source: 'machines',
timeCreated: '2026-07-28T12:00:00.000Z'
};
}