pinokiod 8.3.28 → 8.3.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,358 @@
1
+ // "Open a shell" on home (server/public/home-shell.js): the terminal icon on
2
+ // each row of My Apps and in the top bar. A click shows the folder's shells;
3
+ // picking one opens a new shell in the docked window without loading the app's
4
+ // page, so the app is not started.
5
+ //
6
+ // The real scripts run here in a page that has only what they look for. Layout
7
+ // is not covered: this page lays nothing out.
8
+ const assert = require('node:assert/strict')
9
+ const fs = require('node:fs')
10
+ const path = require('node:path')
11
+ const test = require('node:test')
12
+ const ejs = require('ejs')
13
+ const { JSDOM } = require('jsdom')
14
+
15
+ const script = (name) => fs.readFileSync(path.resolve(__dirname, '../server/public', name), 'utf8')
16
+ const BUTTON = path.resolve(__dirname, '../server/views/partials/home_shell_button.ejs')
17
+ const button = (data) => ejs.renderFile(BUTTON, data)
18
+
19
+ const SHELLS = {
20
+ projectShell: {
21
+ items: [
22
+ { title: 'Shell', subtitle: 'Open a plain Bash shell', href: '/shell/demo_dev.bash.plain?path=%2Fhome%2Fapi%2Fdemo&session=running-one' },
23
+ { title: 'Python Shell', href: '/shell/demo_dev.bash.venv.0?path=%2Fhome%2Fapi%2Fdemo' },
24
+ { title: 'Not a shell', href: '/run/plugin/codex/pinokio.js' }
25
+ ]
26
+ }
27
+ }
28
+
29
+ async function settle(window) {
30
+ for (let i = 0; i < 6; i += 1) await new Promise((resolve) => window.setTimeout(resolve, 0))
31
+ }
32
+
33
+ // ---- the buttons ------------------------------------------------------------
34
+ test('the row button carries the app\'s folder and name', async () => {
35
+ const html = await button({ folder: '/home/api/demo', name: 'Demo "App"' })
36
+ const dom = new JSDOM(html)
37
+ const node = dom.window.document.querySelector('button.home-shell-button')
38
+ assert.ok(node)
39
+ assert.equal(node.dataset.shellFolder, '/home/api/demo')
40
+ assert.equal(node.getAttribute('aria-label'), 'Open a shell in Demo "App"')
41
+ assert.ok(node.classList.contains('home-icon-btn'))
42
+ assert.equal(node.type, 'button')
43
+ })
44
+
45
+ test('the top bar button is a top bar button', async () => {
46
+ const html = await button({ folder: '/home', name: 'Pinokio home', navbar: true })
47
+ const node = new JSDOM(html).window.document.querySelector('button.home-shell-button')
48
+ assert.ok(node.classList.contains('btn2'))
49
+ assert.equal(node.dataset.shellFolder, '/home')
50
+ // plain shells only
51
+ assert.equal(node.dataset.shellPlain, 'true')
52
+ })
53
+
54
+ test('no folder, no button', async () => {
55
+ assert.equal((await button({ folder: undefined, name: 'Pinokio home', navbar: true })).trim(), '')
56
+ })
57
+
58
+ // ---- the menu ---------------------------------------------------------------
59
+ async function home(t, options = {}) {
60
+ const reads = []
61
+ const opened = []
62
+ const left = []
63
+ const rowClicks = []
64
+ let failing = options.fail === true
65
+ const dom = new JSDOM('<!doctype html><body class="is-home main-sidebar-page">'
66
+ + '<header class="navheader"><h1>'
67
+ + await button({ folder: '/home', name: 'Pinokio home', navbar: true })
68
+ + '</h1></header>'
69
+ + '<div class="home-app-line" id="demo" role="link" tabindex="0"><div class="menu-btns">'
70
+ + await button({ folder: '/home/api/demo', name: 'Demo' })
71
+ + '</div></div>'
72
+ + '<div class="home-app-line" id="other" role="link" tabindex="0"><div class="menu-btns">'
73
+ + await button({ folder: '/home/api/other', name: 'Other' })
74
+ + '</div></div></body>', {
75
+ url: 'http://127.0.0.1:42000/home',
76
+ runScripts: 'outside-only',
77
+ pretendToBeVisual: true,
78
+ beforeParse(window) {
79
+ window.fetch = async (url) => {
80
+ reads.push(String(url))
81
+ if (failing) return { ok: false, json: async () => ({}) }
82
+ return { ok: true, json: async () => options.shells || SHELLS }
83
+ }
84
+ }
85
+ })
86
+ t.after(() => dom.window.close())
87
+ const { window } = dom
88
+ const { document } = window
89
+ if (options.dock !== false) {
90
+ window.PinokioAgentBar = { openShell: (shell, folder) => { opened.push({ shell, folder }); return options.taken !== false } }
91
+ }
92
+ window.PinokioHomeGuardNavigate = (href) => left.push(href)
93
+ // the page's own handlers: a click or Enter anywhere on a row opens the app
94
+ document.addEventListener('click', (event) => {
95
+ if (event.target.closest('.home-app-line')) rowClicks.push('click')
96
+ })
97
+ document.addEventListener('keydown', (event) => {
98
+ if (event.key === 'Enter' && event.target.closest('.home-app-line')) rowClicks.push('enter')
99
+ })
100
+ window.eval(script('home-shell.js'))
101
+ const menu = () => document.querySelector('.home-shell-menu')
102
+ return {
103
+ window,
104
+ document,
105
+ reads,
106
+ opened,
107
+ left,
108
+ rowClicks,
109
+ menu,
110
+ isOpen: () => Boolean(menu()) && !menu().hidden,
111
+ rows: () => Array.from(document.querySelectorAll('.home-shell-menu__row')),
112
+ text: () => (menu() ? menu().textContent : ''),
113
+ rowButton: (id) => document.querySelector(`#${id} .home-shell-button`),
114
+ barButton: () => document.querySelector('.navheader .home-shell-button'),
115
+ recover() { failing = false },
116
+ settle: () => settle(window),
117
+ async click(node) {
118
+ node.dispatchEvent(new window.MouseEvent('click', { bubbles: true, cancelable: true }))
119
+ await settle(window)
120
+ }
121
+ }
122
+ }
123
+
124
+ test('a click on a row\'s button shows that app\'s shells and does not open the app', async (t) => {
125
+ const p = await home(t)
126
+ await p.click(p.rowButton('demo'))
127
+ assert.equal(p.isOpen(), true)
128
+ assert.deepEqual(p.reads, ['/api/plugin/menu?workspace=%2Fhome%2Fapi%2Fdemo'])
129
+ // only the shells are listed
130
+ assert.deepEqual(p.rows().map((row) => row.querySelector('.home-shell-menu__row-label').textContent), ['Shell', 'Python Shell'])
131
+ assert.equal(p.rows()[0].querySelector('.home-shell-menu__row-note').textContent, 'Open a plain Bash shell')
132
+ assert.equal(p.rowButton('demo').getAttribute('aria-expanded'), 'true')
133
+ assert.deepEqual(p.rowClicks, [])
134
+ assert.deepEqual(p.opened, [])
135
+ })
136
+
137
+ test('picking a shell opens a new one in the docked window, in the app\'s folder', async (t) => {
138
+ const p = await home(t)
139
+ await p.click(p.rowButton('demo'))
140
+ await p.click(p.rows()[0])
141
+ assert.equal(p.opened.length, 1)
142
+ assert.equal(p.opened[0].folder, '/home/api/demo')
143
+ // a new shell: the running shell's session is not on the link
144
+ assert.equal(p.opened[0].shell.href, '/shell/demo_dev.bash.plain?path=%2Fhome%2Fapi%2Fdemo')
145
+ assert.equal(p.opened[0].shell.label, 'Shell')
146
+ assert.equal(p.isOpen(), false)
147
+ assert.equal(p.rowButton('demo').getAttribute('aria-expanded'), 'false')
148
+ // nothing left the page, and the row was not opened
149
+ assert.deepEqual(p.left, [])
150
+ assert.deepEqual(p.rowClicks, [])
151
+ })
152
+
153
+ test('the top bar button opens the plain shell in Pinokio\'s own folder at once, with no menu', async (t) => {
154
+ const p = await home(t)
155
+ await p.click(p.barButton())
156
+ assert.deepEqual(p.reads, ['/api/plugin/menu?workspace=%2Fhome'])
157
+ assert.equal(p.isOpen(), false)
158
+ assert.equal(p.opened.length, 1)
159
+ assert.equal(p.opened[0].folder, '/home')
160
+ assert.equal(p.opened[0].shell.href, '/shell/demo_dev.bash.plain?path=%2Fhome%2Fapi%2Fdemo')
161
+ // each click is a new shell
162
+ await p.click(p.barButton())
163
+ assert.equal(p.opened.length, 2)
164
+ })
165
+
166
+ test('the top bar button with two plain shells (Cmd and Bash) lists those, and no environments', async (t) => {
167
+ const p = await home(t, {
168
+ shells: {
169
+ projectShell: {
170
+ items: [
171
+ { title: 'Shell', subtitle: 'Open a plain Cmd shell', href: '/shell/home_dev.cmd.plain?path=%2Fhome' },
172
+ { title: 'Conda Shell', href: '/shell/home_dev.cmd.conda.0?path=%2Fhome' },
173
+ { title: 'Shell', subtitle: 'Open a plain Bash shell', href: '/shell/home_dev.bash.plain?path=%2Fhome' },
174
+ { title: 'Python Shell', href: '/shell/home_dev.bash.venv.1?path=%2Fhome' }
175
+ ]
176
+ }
177
+ }
178
+ })
179
+ await p.click(p.barButton())
180
+ assert.deepEqual(p.opened, [])
181
+ assert.equal(p.isOpen(), true)
182
+ assert.deepEqual(p.rows().map((row) => row.querySelector('.home-shell-menu__row-note').textContent), ['Open a plain Cmd shell', 'Open a plain Bash shell'])
183
+ await p.click(p.rows()[1])
184
+ assert.equal(p.opened[0].shell.href, '/shell/home_dev.bash.plain?path=%2Fhome')
185
+ assert.equal(p.opened[0].folder, '/home')
186
+ })
187
+
188
+ test('the top bar button says so when the shells cannot be read', async (t) => {
189
+ const p = await home(t, { fail: true })
190
+ await p.click(p.barButton())
191
+ assert.deepEqual(p.opened, [])
192
+ assert.match(p.text(), /Couldn't read this folder's shells\./)
193
+ })
194
+
195
+ test('the list is read on the way to a button', async (t) => {
196
+ const p = await home(t)
197
+ p.rowButton('demo').dispatchEvent(new p.window.Event('pointerover', { bubbles: true }))
198
+ await p.settle()
199
+ assert.deepEqual(p.reads, ['/api/plugin/menu?workspace=%2Fhome%2Fapi%2Fdemo'])
200
+ await p.click(p.rowButton('demo'))
201
+ assert.equal(p.reads.length, 1)
202
+ assert.equal(p.rows().length, 2)
203
+ })
204
+
205
+ test('without a docked window the shell\'s own page is opened, as a new shell', async (t) => {
206
+ for (const options of [{ dock: false }, { taken: false }]) {
207
+ const p = await home(t, options)
208
+ await p.click(p.rowButton('demo'))
209
+ await p.click(p.rows()[0])
210
+ assert.equal(p.left.length, 1)
211
+ assert.match(p.left[0], /^\/shell\/demo_dev\.bash\.plain\?path=%2Fhome%2Fapi%2Fdemo&session=[^&]+$/)
212
+ assert.doesNotMatch(p.left[0], /running-one/)
213
+ }
214
+ })
215
+
216
+ test('one menu: another button moves it, the same button closes it', async (t) => {
217
+ const p = await home(t)
218
+ await p.click(p.rowButton('demo'))
219
+ await p.click(p.rowButton('other'))
220
+ assert.equal(p.document.querySelectorAll('.home-shell-menu').length, 1)
221
+ assert.equal(p.isOpen(), true)
222
+ assert.equal(p.rowButton('demo').getAttribute('aria-expanded'), 'false')
223
+ assert.equal(p.rowButton('other').getAttribute('aria-expanded'), 'true')
224
+ await p.click(p.rowButton('other'))
225
+ assert.equal(p.isOpen(), false)
226
+ })
227
+
228
+ test('a folder\'s shells are read once; a failed read is tried again', async (t) => {
229
+ const p = await home(t, { fail: true })
230
+ await p.click(p.rowButton('demo'))
231
+ assert.match(p.text(), /Couldn't read this folder's shells\./)
232
+ await p.click(p.rowButton('demo'))
233
+ p.recover()
234
+ await p.click(p.rowButton('demo'))
235
+ assert.equal(p.rows().length, 2)
236
+ await p.click(p.rowButton('demo'))
237
+ await p.click(p.rowButton('demo'))
238
+ assert.equal(p.reads.length, 2)
239
+ })
240
+
241
+ test('Escape and a press elsewhere close the menu; Enter on the button is not the row\'s', async (t) => {
242
+ const p = await home(t)
243
+ const key = (node, name) => node.dispatchEvent(new p.window.KeyboardEvent('keydown', { key: name, bubbles: true, cancelable: true }))
244
+ await p.click(p.rowButton('demo'))
245
+ key(p.rowButton('demo'), 'Escape')
246
+ assert.equal(p.isOpen(), false)
247
+ await p.click(p.rowButton('demo'))
248
+ p.document.body.dispatchEvent(new p.window.Event('pointerdown', { bubbles: true }))
249
+ assert.equal(p.isOpen(), false)
250
+ key(p.rowButton('demo'), 'Enter')
251
+ assert.deepEqual(p.rowClicks, [])
252
+ // Enter elsewhere on the row is still the row's
253
+ key(p.document.getElementById('demo'), 'Enter')
254
+ assert.deepEqual(p.rowClicks, ['enter'])
255
+ })
256
+
257
+ test('the press that closes the menu does not also open the row under it', async (t) => {
258
+ const p = await home(t)
259
+ const row = p.document.getElementById('other')
260
+ const press = () => row.dispatchEvent(new p.window.Event('pointerdown', { bubbles: true }))
261
+ await p.click(p.rowButton('demo'))
262
+ press()
263
+ assert.equal(p.isOpen(), false)
264
+ await p.click(row)
265
+ assert.deepEqual(p.rowClicks, [])
266
+ // the click after that is an ordinary one
267
+ press()
268
+ await p.click(row)
269
+ assert.deepEqual(p.rowClicks, ['click'])
270
+ })
271
+
272
+ // ---- the docked window ------------------------------------------------------
273
+ // The real agent-bar.js and agent-bar-panel.js, on a page with the main sidebar.
274
+ async function dock(t, { shells, homeDir }) {
275
+ const running = { list: shells }
276
+ const dom = new JSDOM('<!doctype html><body class="main-sidebar-page"><header class="navheader"><h1><div class="flexible"></div></h1></header><main id="page"></main>'
277
+ + `<script type="text/plain" data-agent-bar-panel="true"${homeDir ? ` data-pinokio-home="${homeDir}"` : ''}></script></body>`, {
278
+ url: 'http://127.0.0.1:42000/home',
279
+ runScripts: 'outside-only',
280
+ pretendToBeVisual: true,
281
+ beforeParse(window) {
282
+ window.matchMedia = () => ({ matches: false, addEventListener() {}, removeEventListener() {} })
283
+ window.Element.prototype.getBoundingClientRect = function () {
284
+ return { left: 300, top: 8, right: 900, bottom: 40, width: 600, height: 32, x: 300, y: 8 }
285
+ }
286
+ window.fetch = async (url) => {
287
+ const address = String(url)
288
+ if (address === '/info/shells') return { ok: true, json: async () => running.list }
289
+ if (address.split('?')[0] === '/api/plugin/menu') {
290
+ return { ok: true, json: async () => ({ menu: [{ href: '/run/plugin/codex/pinokio.js', title: 'Codex', default: true }], projectShell: { items: [] } }) }
291
+ }
292
+ if (address === '/info/apps') return { ok: true, json: async () => ({ apps: [{ name: 'demo', title: 'Demo App' }] }) }
293
+ return { ok: false, json: async () => ({}) }
294
+ }
295
+ }
296
+ })
297
+ t.after(() => dom.window.close())
298
+ const { window } = dom
299
+ window.eval(script('agent-bar.js'))
300
+ window.eval(script('agent-bar-panel.js'))
301
+ await window.PinokioAgentBar.ready
302
+ await settle(window)
303
+ const root = window.document.querySelector('.agent-bar')
304
+ const chip = () => root.querySelector('.agent-bar__running')
305
+ return {
306
+ window,
307
+ running,
308
+ bar: window.PinokioAgentBar,
309
+ count: () => (chip() && !chip().hidden ? parseInt(chip().textContent, 10) : 0),
310
+ async titles() {
311
+ chip().click()
312
+ await settle(window)
313
+ return Array.from(root.querySelectorAll('.agent-bar__menu--running .agent-bar__menu-title')).map((node) => node.textContent)
314
+ },
315
+ frames: () => Array.from(window.document.querySelectorAll('iframe')).map((frame) => frame.getAttribute('src') || ''),
316
+ async look() {
317
+ window.document.dispatchEvent(new window.Event('visibilitychange'))
318
+ await settle(window)
319
+ }
320
+ }
321
+ }
322
+
323
+ const HOME_SHELL = { id: 'shell/home_dev.bash.plain?session=s1', path: '/home', start_time: 1 }
324
+ const APP_SHELL = { id: 'shell/demo_dev.bash.plain?session=s2', path: '/home/api/demo', start_time: 2 }
325
+ const ELSEWHERE_SHELL = { id: 'shell/tmp_dev.bash.plain?session=s3', path: '/somewhere/else', start_time: 3 }
326
+
327
+ test('a shell running in Pinokio\'s own folder is listed, under its own heading', async (t) => {
328
+ const p = await dock(t, { shells: [HOME_SHELL, APP_SHELL, ELSEWHERE_SHELL], homeDir: '/home' })
329
+ assert.equal(p.count(), 2)
330
+ assert.deepEqual((await p.titles()).sort(), ['Demo App', 'Pinokio home'])
331
+ })
332
+
333
+ test('where Pinokio\'s own folder is not known, only shells inside apps are listed', async (t) => {
334
+ const p = await dock(t, { shells: [HOME_SHELL, APP_SHELL, ELSEWHERE_SHELL] })
335
+ assert.equal(p.count(), 1)
336
+ })
337
+
338
+ test('a shell picked on home opens in the docked window, with a session of its own', async (t) => {
339
+ const p = await dock(t, { shells: [], homeDir: '/home' })
340
+ assert.equal(typeof p.bar.openShell, 'function')
341
+ assert.equal(p.bar.openShell({ href: '/shell/demo_dev.bash.plain?path=%2Fhome%2Fapi%2Fdemo', label: 'Shell' }, '/home/api/demo'), true)
342
+ await settle(p.window)
343
+ const shown = p.frames().filter((src) => src.startsWith('/shell/demo_dev.bash.plain?'))
344
+ assert.equal(shown.length, 1)
345
+ assert.match(shown[0], /[?&]session=[^&]+/)
346
+ // nothing to open it in a folder that was not given
347
+ assert.equal(p.bar.openShell({ href: '/shell/x' }, ''), false)
348
+ })
349
+
350
+ test('a shell opened in Pinokio\'s own folder stays on screen once it is running', async (t) => {
351
+ const p = await dock(t, { shells: [], homeDir: '/home' })
352
+ assert.equal(p.bar.openShell({ href: '/shell/home_dev.bash.plain?path=%2Fhome&session=s1', label: 'Shell' }, '/home'), true)
353
+ await settle(p.window)
354
+ p.running.list = [HOME_SHELL]
355
+ await p.look()
356
+ assert.equal(p.count(), 1)
357
+ assert.equal(p.frames().filter((src) => src.startsWith('/shell/home_dev.bash.plain?')).length, 1)
358
+ })
@@ -0,0 +1,166 @@
1
+ // The size a new shell starts at (kernel/shell_size.js): a script run's record
2
+ // of the page size is kept current by resizes, and each step's shell takes its
3
+ // size from it right before its console is created. The shells here are real
4
+ // Shell objects with a real terminal state, but no console process is started.
5
+ const assert = require('node:assert/strict')
6
+ const test = require('node:test')
7
+ const { Terminal } = require('@xterm/headless')
8
+
9
+ const ShellSize = require('../kernel/shell_size')
10
+ const Shell = require('../kernel/shell')
11
+ const Shells = require('../kernel/shells')
12
+
13
+ // the temporary log lines are not what is being tested
14
+ const quiet = (t) => t.mock.method(console, 'log', () => {})
15
+
16
+ // A shell as it is partway through starting: it has taken the size its step
17
+ // was given and has its id, and its console does not exist yet.
18
+ const startingShell = (id, params) => {
19
+ const shell = new Shell({ bracketedPasteSupport: {} })
20
+ shell.id = id
21
+ shell.params = params
22
+ shell.cols = params.cols
23
+ shell.rows = params.rows
24
+ shell.vt = new Terminal({ allowProposedApi: true, cols: shell.cols, rows: shell.rows })
25
+ ShellSize.track(shell, params)
26
+ return shell
27
+ }
28
+ // what a step hands its shell: the size copied when the step began, and its run
29
+ const step = (record, extra = {}) => Object.assign({ cols: record.cols, rows: record.rows, $parent: { client: record } }, extra)
30
+ const sizeOf = (shell) => ({ cols: shell.cols, rows: shell.rows })
31
+
32
+ test('a later step starts at the size of the last resize, not the launch size', (t) => {
33
+ quiet(t)
34
+ const record = { cols: 244, rows: 60 }
35
+ const first = startingShell('run-1', step(record))
36
+ ShellSize.beforeSpawn(first)
37
+ assert.deepEqual(sizeOf(first), { cols: 244, rows: 60 })
38
+
39
+ ShellSize.resized('run-1', { cols: 245, rows: 61 }, first)
40
+ assert.deepEqual(record, { cols: 245, rows: 61 })
41
+
42
+ // the step's other commands reuse the step's parameters, copied before the resize
43
+ const second = startingShell('run-1', { cols: 244, rows: 60, $parent: { client: record } })
44
+ ShellSize.beforeSpawn(second)
45
+ assert.deepEqual(sizeOf(second), { cols: 245, rows: 61 })
46
+ // the terminal state the server keeps for the shell follows too
47
+ assert.deepEqual({ cols: second.vt.cols, rows: second.vt.rows }, { cols: 245, rows: 61 })
48
+ })
49
+
50
+ test('a resize with no shell running reaches the next step', (t) => {
51
+ quiet(t)
52
+ const record = { cols: 120, rows: 30 }
53
+ ShellSize.beforeSpawn(startingShell('run-2', step(record)))
54
+
55
+ // between two steps: the shell is gone, only its id is known
56
+ ShellSize.resized('run-2', { cols: 90, rows: 25 }, null)
57
+
58
+ const next = startingShell('run-2', { cols: 120, rows: 30, $parent: { client: record } })
59
+ ShellSize.beforeSpawn(next)
60
+ assert.deepEqual(sizeOf(next), { cols: 90, rows: 25 })
61
+ })
62
+
63
+ test('a resize that arrives while a shell is still setting up is not lost', (t) => {
64
+ quiet(t)
65
+ const record = { cols: 100, rows: 30 }
66
+ // the shell has taken its size and its id, and is not findable yet
67
+ const shell = startingShell('run-3', step(record))
68
+
69
+ ShellSize.resized('run-3', { cols: 80, rows: 24 }, null)
70
+
71
+ ShellSize.beforeSpawn(shell)
72
+ assert.deepEqual(sizeOf(shell), { cols: 80, rows: 24 })
73
+ })
74
+
75
+ test('a step that sets its own size keeps it', (t) => {
76
+ quiet(t)
77
+ const record = { cols: 200, rows: 50 }
78
+ const params = step(record, { client: { cols: 80, rows: 24 } })
79
+ // the size the shell works out for itself when the step names one
80
+ const shell = startingShell('run-4', Object.assign({}, params, { cols: 80, rows: 24 }))
81
+
82
+ ShellSize.beforeSpawn(shell)
83
+ assert.deepEqual(sizeOf(shell), { cols: 80, rows: 24 })
84
+
85
+ // it still belongs to the run: a resize of it is what later steps start at
86
+ ShellSize.resized('run-4', { cols: 150, rows: 40 }, shell)
87
+ assert.deepEqual(record, { cols: 150, rows: 40 })
88
+ })
89
+
90
+ test('a resize updates the run of the shell it reached, also when two runs share an id', (t) => {
91
+ quiet(t)
92
+ const older = { cols: 100, rows: 30 }
93
+ const newer = { cols: 100, rows: 30 }
94
+ const live = startingShell('shared', step(older))
95
+ // a second run starts a shell under the same id
96
+ startingShell('shared', step(newer))
97
+
98
+ ShellSize.resized('shared', { cols: 70, rows: 20 }, live)
99
+
100
+ assert.deepEqual(older, { cols: 70, rows: 20 })
101
+ assert.deepEqual(newer, { cols: 100, rows: 30 })
102
+ })
103
+
104
+ test('a long-lived shell still updates its run after many other shells have started', (t) => {
105
+ quiet(t)
106
+ const record = { cols: 100, rows: 30 }
107
+ const live = startingShell('server', step(record))
108
+ const other = { cols: 100, rows: 30 }
109
+ for (let i = 0; i < 600; i++) {
110
+ ShellSize.track({ id: `bulk-${i}` }, step(other))
111
+ }
112
+
113
+ ShellSize.resized('server', { cols: 133, rows: 33 }, live)
114
+ assert.deepEqual(record, { cols: 133, rows: 33 })
115
+
116
+ // ids with no shell running are remembered up to a limit, newest kept
117
+ ShellSize.resized('bulk-0', { cols: 50, rows: 10 }, null)
118
+ assert.deepEqual(other, { cols: 100, rows: 30 })
119
+ ShellSize.resized('bulk-599', { cols: 50, rows: 10 }, null)
120
+ assert.deepEqual(other, { cols: 50, rows: 10 })
121
+ })
122
+
123
+ test('a shell outside a script run is left alone', (t) => {
124
+ quiet(t)
125
+ for (const params of [{ cols: 132, rows: 43 }, { cols: 132, rows: 43, $parent: { path: '/x' } }]) {
126
+ const shell = startingShell('plain', params)
127
+ ShellSize.resized('plain', { cols: 10, rows: 10 }, null)
128
+ ShellSize.beforeSpawn(shell)
129
+ assert.deepEqual(sizeOf(shell), { cols: 132, rows: 43 })
130
+ // and a resize of it while running has no record to touch
131
+ ShellSize.resized('plain', { cols: 20, rows: 20 }, shell)
132
+ }
133
+ })
134
+
135
+ test('a record without a usable size is not applied, and a bad resize is not stored', (t) => {
136
+ quiet(t)
137
+ const empty = { source: 'desktop' }
138
+ const shell = startingShell('run-5', { cols: 100, rows: 30, $parent: { client: empty } })
139
+ ShellSize.beforeSpawn(shell)
140
+ assert.deepEqual(sizeOf(shell), { cols: 100, rows: 30 })
141
+
142
+ const record = { cols: 100, rows: 30 }
143
+ const live = startingShell('run-6', step(record))
144
+ for (const bad of [undefined, {}, { cols: 0, rows: 30 }, { cols: '80', rows: 24 }, { cols: 80.5, rows: 24 }, { cols: 80 }]) {
145
+ ShellSize.resized('run-6', bad, live)
146
+ ShellSize.resized('run-6', bad, null)
147
+ }
148
+ assert.deepEqual(record, { cols: 100, rows: 30 })
149
+ })
150
+
151
+ test('a resize sent to the shells reaches the running shell and its run', (t) => {
152
+ quiet(t)
153
+ const shells = new Shells({})
154
+ const record = { cols: 100, rows: 30 }
155
+ const live = startingShell('live', step(record))
156
+ shells.shells.push(live)
157
+
158
+ shells.resize({ id: 'live', resize: { cols: 111, rows: 31 } })
159
+ assert.deepEqual(sizeOf(live), { cols: 111, rows: 31 })
160
+ assert.deepEqual(record, { cols: 111, rows: 31 })
161
+
162
+ // between two steps no shell is running: the record still follows
163
+ shells.shells.length = 0
164
+ shells.resize({ id: 'live', resize: { cols: 99, rows: 29 } })
165
+ assert.deepEqual(record, { cols: 99, rows: 29 })
166
+ })