cat / goq
clone

turns a linux box into a private steam streaming appliance: plug in, tap your key, play from another computer

ci/pipeline-ergonomics
goq / portal / focus-runtime.test.mjs
focus-runtime.test.mjs 5.3K
1import test from 'node:test';2import assert from 'node:assert/strict';34import { createFocusRuntime } from './focus-runtime.mjs';56test('focus request is non-optimistic and enforces one in-flight command', async () => {7 const calls = [];8 const session = {9 mode: 'control-v2', eligible: true, focused: false, nativeGeneration: 3, revision: 4,10 };11 const runtime = createFocusRuntime({12 invokeCommand: async (command, args) => { calls.push([command, args]); return true; },13 getSession: () => ({ ...session }),14 startFocusedInput: async () => {},15 activateLocalControl: async () => {},16 });17 assert.equal(await runtime.request({ controllerInitiated: true }), true);18 assert.equal(await runtime.request(), false);19 assert.equal(session.focused, false);20 assert.equal(calls[0][1].expectedRevision, 4);21});2223test('authoritative grant starts input before activation and preserves controller provenance', async () => {24 const order = [];25 const session = {26 mode: 'control-v2', eligible: true, focused: false, nativeGeneration: 3, revision: 4,27 };28 const runtime = createFocusRuntime({29 invokeCommand: async () => true,30 getSession: () => ({ ...session }),31 startFocusedInput: async () => order.push('input'),32 activateLocalControl: async ({ controllerInitiated }) => order.push(`activate:${controllerInitiated}`),33 });34 await runtime.request({ controllerInitiated: true });35 session.focused = true;36 session.focusGeneration = 8;37 session.revision = 5;38 assert.equal(await runtime.observeSession(), true);39 assert.deepEqual(order, ['input', 'activate:true']);40});4142test('release carries authoritative revision and focus generation', async () => {43 let call;44 const runtime = createFocusRuntime({45 invokeCommand: async (command, args) => { call = [command, args]; return true; },46 getSession: () => ({47 mode: 'control-v2', focused: true, nativeGeneration: 6, revision: 9, focusGeneration: 12,48 }),49 startFocusedInput: async () => {},50 activateLocalControl: async () => {},51 });52 assert.equal(await runtime.release(), true);53 assert.equal(call[0], 'iroh_client_release_focus');54 assert.equal(call[1].expectedRevision, 9);55 assert.equal(call[1].expectedFocusGeneration, 12);56});5758test('holder approve and deny are bound to the exact proposal and focus generation', async () => {59 const calls = [];60 const session = {61 mode: 'control-v2', eligible: true, focused: true, nativeGeneration: 6,62 revision: 9, focusGeneration: 12,63 snapshot: {64 self_presence_id: 'viewer-a',65 viewers: [{ presence_id: 'viewer-a', session_id: 1, you: true }],66 focus: { state: 'held', holder: 'viewer-a', session_id: 1, focus_generation: 12 },67 focus_proposal: {68 proposal_id: 4, holder: 'viewer-a', holder_session_id: 1,69 requester: 'viewer-b', requester_session_id: 2,70 },71 },72 };73 const runtime = createFocusRuntime({74 invokeCommand: async (command, args) => { calls.push([command, args]); return true; },75 getSession: () => structuredClone(session),76 startFocusedInput: async () => {},77 activateLocalControl: async () => {},78 });79 assert.equal(await runtime.approve(), true);80 assert.equal(calls[0][0], 'iroh_client_approve_focus');81 assert.equal(calls[0][1].expectedProposalId, 4);82 assert.equal(calls[0][1].expectedFocusGeneration, 12);83 runtime.observeResult({ request_id: calls[0][1].requestId, accepted: true });84 assert.equal(await runtime.deny(), true);85 assert.equal(calls[1][0], 'iroh_client_deny_focus');86});8788test('configured owner preemption remains non-optimistic and suppresses granting A on activation', async () => {89 const order = [];90 const session = {91 mode: 'control-v2', eligible: true, focused: false, nativeGeneration: 8,92 revision: 10, focusGeneration: null,93 snapshot: {94 self_is_focus_owner: true,95 focus: { state: 'held', holder: 'viewer-b', session_id: 2, focus_generation: 20 },96 },97 };98 const runtime = createFocusRuntime({99 invokeCommand: async (command, args) => { order.push([command, args]); return true; },100 getSession: () => structuredClone(session),101 startFocusedInput: async () => order.push('input'),102 activateLocalControl: async ({ controllerInitiated }) => order.push(`activate:${controllerInitiated}`),103 });104 assert.equal(await runtime.preempt({ controllerInitiated: true }), true);105 assert.equal(session.focused, false);106 session.focused = true;107 session.focusGeneration = 21;108 session.revision = 12;109 assert.equal(await runtime.observeSession(), true);110 assert.deepEqual(order.slice(1), ['input', 'activate:true']);111});112113test('denied and expired handoff requests clear without optimistic possession', async () => {114 const session = {115 mode: 'control-v2', eligible: true, focused: false, nativeGeneration: 3, revision: 4,116 snapshot: { self_presence_id: 'viewer-b', transition_reason: 'handoff_requested' },117 };118 const runtime = createFocusRuntime({119 invokeCommand: async () => true,120 getSession: () => structuredClone(session),121 startFocusedInput: async () => {},122 activateLocalControl: async () => {},123 });124 await runtime.request();125 session.revision = 5;126 session.snapshot.transition_reason = 'proposal_expired';127 await runtime.observeSession();128 assert.equal(runtime.snapshot().pending, false);129 assert.equal(session.focused, false);130});