mirror of
https://github.com/nestriness/nestri.git
synced 2026-11-25 08:29:52 +02:00
A finished session says why it ended; bash <(curl …) -u updates a host (#413)
- **Sessions (#412):** `end_reason` and `time_claimed`, with migration `0026_session_end_reason` (two nullable columns, readable by the release it replaces). `POST /session/:id/state` takes an optional `endReason`, and a run stopped before it went live can now go from `starting` to `ended`. - **Installer (#411):** - `bash <(curl -fsSL https://api.nestri.io/install.sh) -u` updates a host; `-h` prints help; a plain token installs. - When the script is not root, it asks sudo itself. - The installed host agent version is unchanged (0.2.18). **Ships before host agent 0.2.19**, which sends `endReason` and the new transition. Cut from prod with dev's files, so squash histories cannot conflict; the tree is identical to dev.
This commit is contained in:
@@ -146,7 +146,11 @@ describe('the line for a full host', () => {
|
||||
await Session.setState({ id: playing.run.id, state: 'ended' });
|
||||
expect((await h.jobs()).map((j) => j.sessionId)).toEqual([here.run.id]);
|
||||
const left = await Session.fromID(gone.run.id);
|
||||
expect(left).toMatchObject({ state: 'ended', errorMessage: 'Left the queue' });
|
||||
expect(left).toMatchObject({
|
||||
state: 'ended',
|
||||
errorMessage: 'Left the queue',
|
||||
endReason: 'left_queue'
|
||||
});
|
||||
});
|
||||
|
||||
test('a run with a slot is not in line', async () => {
|
||||
|
||||
@@ -160,11 +160,13 @@ describe('POST /session', () => {
|
||||
state: 'requested',
|
||||
tier: null,
|
||||
ticket: null,
|
||||
timeClaimed: null,
|
||||
timeStarted: null,
|
||||
timeStopped: null,
|
||||
timeStopRequested: null,
|
||||
trial: false,
|
||||
errorMessage: null
|
||||
errorMessage: null,
|
||||
endReason: null
|
||||
});
|
||||
expect(body.data.id.startsWith('ses_')).toBe(true);
|
||||
});
|
||||
@@ -478,7 +480,11 @@ describe('POST /session/:id/state', () => {
|
||||
body: JSON.stringify({ state: 'starting', claimToken: HOLDER })
|
||||
});
|
||||
expect(res.status).toBe(200);
|
||||
expect(((await res.json()) as any).data.state).toBe('starting');
|
||||
const claimed = ((await res.json()) as any).data;
|
||||
expect(claimed.state).toBe('starting');
|
||||
// The time it was taken is what bring-up is measured from.
|
||||
expect(body.data.timeClaimed).toBeNull();
|
||||
expect(claimed.timeClaimed).not.toBeNull();
|
||||
|
||||
const jobs = await app.request('/machine/jobs', { headers: s.host });
|
||||
expect(((await jobs.json()) as any).data).toEqual([]);
|
||||
@@ -638,6 +644,106 @@ describe('POST /session/:id/state', () => {
|
||||
expect(res.status).toBe(400);
|
||||
});
|
||||
|
||||
test('the report that ends a run says why, and the owner reads it', async () => {
|
||||
const s = await scene('route-reason-ended', 5570);
|
||||
const { body } = await requestSession(s);
|
||||
const report = (state: string, endReason?: string) =>
|
||||
app.request(`/session/${body.data.id}/state`, {
|
||||
method: 'POST',
|
||||
headers: s.host,
|
||||
body: JSON.stringify({ state, claimToken: HOLDER, endReason })
|
||||
});
|
||||
|
||||
await report('starting');
|
||||
await report('live');
|
||||
const ended = await report('ended', 'game_exited');
|
||||
expect(ended.status).toBe(200);
|
||||
expect(((await ended.json()) as any).data.endReason).toBe('game_exited');
|
||||
|
||||
const read = await app.request(`/session/${body.data.id}`, { headers: s.user });
|
||||
expect(((await read.json()) as any).data.endReason).toBe('game_exited');
|
||||
});
|
||||
|
||||
test('a run that failed says why it is over, beside what went wrong', async () => {
|
||||
const s = await scene('route-reason-failed', 5571);
|
||||
const { body } = await requestSession(s);
|
||||
const report = (payload: Record<string, unknown>) =>
|
||||
app.request(`/session/${body.data.id}/state`, {
|
||||
method: 'POST',
|
||||
headers: s.host,
|
||||
body: JSON.stringify({ claimToken: HOLDER, ...payload })
|
||||
});
|
||||
|
||||
await report({ state: 'starting' });
|
||||
const failed = await report({
|
||||
state: 'failed',
|
||||
errorMessage: 'the download could not finish',
|
||||
endReason: 'launch_failed'
|
||||
});
|
||||
expect(failed.status).toBe(200);
|
||||
expect(((await failed.json()) as any).data).toMatchObject({
|
||||
state: 'failed',
|
||||
errorMessage: 'the download could not finish',
|
||||
endReason: 'launch_failed'
|
||||
});
|
||||
});
|
||||
|
||||
test('a reason with a run that is not over is refused, and the run stays put', async () => {
|
||||
const s = await scene('route-reason-early', 5572);
|
||||
const { body } = await requestSession(s);
|
||||
await app.request(`/session/${body.data.id}/state`, {
|
||||
method: 'POST',
|
||||
headers: s.host,
|
||||
body: JSON.stringify({ state: 'starting', claimToken: HOLDER })
|
||||
});
|
||||
|
||||
const res = await app.request(`/session/${body.data.id}/state`, {
|
||||
method: 'POST',
|
||||
headers: s.host,
|
||||
body: JSON.stringify({ state: 'live', claimToken: HOLDER, endReason: 'idle' })
|
||||
});
|
||||
expect(res.status).toBe(400);
|
||||
expect(((await res.json()) as any).param).toBe('endReason');
|
||||
const run = await Session.fromID(body.data.id);
|
||||
expect(run?.state).toBe('starting');
|
||||
expect(run?.endReason).toBeNull();
|
||||
});
|
||||
|
||||
test('a reason nobody defined is a validation error', async () => {
|
||||
const s = await scene('route-reason-bogus', 5573);
|
||||
const { body } = await requestSession(s);
|
||||
await app.request(`/session/${body.data.id}/state`, {
|
||||
method: 'POST',
|
||||
headers: s.host,
|
||||
body: JSON.stringify({ state: 'starting', claimToken: HOLDER })
|
||||
});
|
||||
const res = await app.request(`/session/${body.data.id}/state`, {
|
||||
method: 'POST',
|
||||
headers: s.host,
|
||||
body: JSON.stringify({ state: 'failed', claimToken: HOLDER, endReason: 'bored' })
|
||||
});
|
||||
expect(res.status).toBe(400);
|
||||
expect((await Session.fromID(body.data.id))?.state).toBe('starting');
|
||||
});
|
||||
|
||||
test('a host agent that sends no reason still ends the run, with none recorded', async () => {
|
||||
const s = await scene('route-reason-absent', 5574);
|
||||
const { body } = await requestSession(s);
|
||||
const report = (state: string) =>
|
||||
app.request(`/session/${body.data.id}/state`, {
|
||||
method: 'POST',
|
||||
headers: s.host,
|
||||
body: JSON.stringify({ state, claimToken: HOLDER })
|
||||
});
|
||||
|
||||
await report('starting');
|
||||
await report('live');
|
||||
// The report every host agent already in the field sends.
|
||||
const ended = await report('ended');
|
||||
expect(ended.status).toBe(200);
|
||||
expect(((await ended.json()) as any).data).toMatchObject({ state: 'ended', endReason: null });
|
||||
});
|
||||
|
||||
test('a person cannot report a state on their own session', async () => {
|
||||
const s = await scene('route-claim-person', 5538);
|
||||
const { body } = await requestSession(s);
|
||||
@@ -721,6 +827,7 @@ describe('POST /session/:id/ticket', () => {
|
||||
const data = ((await read.json()) as any).data;
|
||||
expect(Object.keys(data).sort()).toEqual([
|
||||
'boxId',
|
||||
'endReason',
|
||||
'errorMessage',
|
||||
'gameId',
|
||||
'id',
|
||||
@@ -729,6 +836,7 @@ describe('POST /session/:id/ticket', () => {
|
||||
'state',
|
||||
'ticket',
|
||||
'tier',
|
||||
'timeClaimed',
|
||||
'timeStarted',
|
||||
'timeStopRequested',
|
||||
'timeStopped',
|
||||
@@ -901,6 +1009,9 @@ describe('POST /session/:id/stop', () => {
|
||||
const out = (await res.json()) as any;
|
||||
expect(out.data.state).toBe('ended');
|
||||
expect(out.data.timeStopped).not.toBeNull();
|
||||
// Ended here rather than by a host, so the reason is written here too.
|
||||
expect(out.data.endReason).toBe('user');
|
||||
expect(out.data.timeClaimed).toBeNull();
|
||||
const res2 = await app.request('/machine/jobs', { headers: s.host });
|
||||
expect(((await res2.json()) as any).data).toEqual([]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user