pinokiod 8.3.26 → 8.3.29

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.
Files changed (39) hide show
  1. package/kernel/shell.js +3 -0
  2. package/kernel/shell_size.js +99 -0
  3. package/kernel/shells.js +2 -0
  4. package/package.json +1 -1
  5. package/server/index.js +7 -3
  6. package/server/lib/app_run_tabs.js +29 -17
  7. package/server/public/agent-bar-about.css +67 -0
  8. package/server/public/agent-bar-about.js +205 -0
  9. package/server/public/agent-bar-compose.js +4 -1
  10. package/server/public/agent-bar-modes.css +78 -0
  11. package/server/public/agent-bar-modes.js +97 -0
  12. package/server/public/agent-bar-panel.js +41 -13
  13. package/server/public/agent-bar.css +7 -5
  14. package/server/public/agent-bar.js +37 -9
  15. package/server/public/app-run-tabs.js +0 -10
  16. package/server/public/files-app/open-folder.js +58 -41
  17. package/server/public/files-app/toolbar.css +27 -5
  18. package/server/public/git-page.css +0 -1
  19. package/server/public/git-page.js +269 -149
  20. package/server/public/home-actions-menu.css +188 -0
  21. package/server/public/home-actions-menu.js +135 -0
  22. package/server/public/home-shell.css +121 -0
  23. package/server/public/home-shell.js +290 -0
  24. package/server/routes/git_page.js +4 -6
  25. package/server/views/app.ejs +19 -5
  26. package/server/views/file_browser.ejs +3 -4
  27. package/server/views/index.ejs +42 -3
  28. package/server/views/partials/home_action_modal.ejs +1 -1
  29. package/server/views/partials/home_ask_button.ejs +15 -0
  30. package/server/views/partials/home_shell_button.ejs +21 -0
  31. package/server/views/partials/main_sidebar.ejs +5 -1
  32. package/server/views/pre.ejs +1 -1
  33. package/test/agent-bar-about.test.js +238 -0
  34. package/test/agent-bar-modes.test.js +191 -0
  35. package/test/agent-bar-new-tab.test.js +60 -0
  36. package/test/app-git-files-tabs.test.js +84 -0
  37. package/test/home-actions-menu.test.js +187 -0
  38. package/test/home-shell.test.js +358 -0
  39. package/test/shell-size.test.js +166 -0
@@ -0,0 +1,187 @@
1
+ // Home: an app row's actions as a dropdown (server/public/home-actions-menu.js).
2
+ // On a wide window the page opens the row's actions dialog "docked"; the script
3
+ // puts it under the row's button and closes it like a menu. The dialog and its
4
+ // buttons are the page's own.
5
+ //
6
+ // The real script runs here in a page that has only what it looks for. The
7
+ // page's part (opening the dialog) is done by hand. Layout is not covered.
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 = fs.readFileSync(path.resolve(__dirname, '../server/public/home-actions-menu.js'), 'utf8')
16
+ const MODAL = path.resolve(__dirname, '../server/views/partials/home_action_modal.ejs')
17
+
18
+ const PAGE = '<!doctype html><body class="is-home">'
19
+ + '<div class="home-app-line line" id="row">'
20
+ + '<div class="menu-btns"><button class="btn open-actions-modal" id="trigger" data-dialog-id="actions-1" aria-expanded="false"></button></div>'
21
+ + '<dialog id="actions-1" class="home-actions-dialog">'
22
+ + '<div class="home-actions-header"><button class="home-actions-close"></button></div>'
23
+ + '<div class="home-actions-body">'
24
+ + '<details class="home-run-menu-group" open><summary class="home-run-menu-summary" id="group">Group</summary>'
25
+ + '<div class="home-run-menu-row"><a class="home-run-command" id="run" href="#">Open Web UI</a><button class="home-run-stop" id="stop"></button></div></details>'
26
+ + '<button class="home-mode-command" id="settings"><i></i><span>Settings</span></button>'
27
+ + '</div></dialog>'
28
+ + '</div>'
29
+ + '<div id="elsewhere"></div>'
30
+ + '<div class="swal2-container"><button id="confirm"></button></div>'
31
+ + '</body>'
32
+
33
+ async function page(t) {
34
+ const dom = new JSDOM(PAGE, {
35
+ url: 'http://127.0.0.1:42000/home',
36
+ runScripts: 'outside-only',
37
+ pretendToBeVisual: true,
38
+ beforeParse(window) {
39
+ // the row's button, and a menu 280 by 300
40
+ window.Element.prototype.getBoundingClientRect = function () {
41
+ return { left: 200, top: 100, right: 228, bottom: 122, width: 28, height: 22, x: 200, y: 100 }
42
+ }
43
+ Object.defineProperty(window.HTMLElement.prototype, 'offsetWidth', { get() { return 280 } })
44
+ Object.defineProperty(window.HTMLElement.prototype, 'offsetHeight', { get() { return 300 } })
45
+ }
46
+ })
47
+ t.after(() => dom.window.close())
48
+ const { window } = dom
49
+ const { document } = window
50
+ window.eval(script)
51
+ const dialog = document.getElementById('actions-1')
52
+ const settle = async () => { for (let i = 0; i < 4; i += 1) await new Promise((resolve) => window.setTimeout(resolve, 0)) }
53
+ return {
54
+ window,
55
+ document,
56
+ dialog,
57
+ settle,
58
+ isOpen: () => dialog.hasAttribute('open'),
59
+ // what the page does on a wide window, and on a narrow one
60
+ async open(docked = true) {
61
+ dialog.classList.toggle('is-docked', docked)
62
+ dialog.setAttribute('open', '')
63
+ await settle()
64
+ },
65
+ async press(id) {
66
+ document.getElementById(id).dispatchEvent(new window.Event('pointerdown', { bubbles: true }))
67
+ await settle()
68
+ },
69
+ async click(id) {
70
+ document.getElementById(id).dispatchEvent(new window.MouseEvent('click', { bubbles: true, cancelable: true }))
71
+ await settle()
72
+ }
73
+ }
74
+ }
75
+
76
+ test('the page is marked, so the opened dialog is drawn as a menu', async (t) => {
77
+ const p = await page(t)
78
+ assert.ok(p.document.body.classList.contains('home-actions-menu'))
79
+ })
80
+
81
+ test('the opened menu is put under the row\'s button', async (t) => {
82
+ const p = await page(t)
83
+ await p.open()
84
+ assert.equal(p.dialog.style.left, '200px')
85
+ assert.equal(p.dialog.style.top, '128px')
86
+ })
87
+
88
+ test('with no room below, the menu goes above the button', async (t) => {
89
+ const p = await page(t)
90
+ p.window.innerHeight = 400
91
+ p.window.Element.prototype.getBoundingClientRect = function () {
92
+ return { left: 200, top: 340, right: 228, bottom: 362, width: 28, height: 22, x: 200, y: 340 }
93
+ }
94
+ await p.open()
95
+ assert.equal(p.dialog.style.top, '34px')
96
+ })
97
+
98
+ test('a press elsewhere closes the menu; inside it, on its button, or on a question over it does not', async (t) => {
99
+ const p = await page(t)
100
+ await p.open()
101
+ await p.press('settings')
102
+ await p.press('trigger')
103
+ await p.press('confirm')
104
+ assert.equal(p.isOpen(), true)
105
+ await p.press('elsewhere')
106
+ assert.equal(p.isOpen(), false)
107
+ })
108
+
109
+ test('the press that closes the menu does not also open the row under it', async (t) => {
110
+ const p = await page(t)
111
+ const opened = []
112
+ // the page's own rule: a click on a row opens its app, unless it was cancelled or is on a control
113
+ p.document.addEventListener('click', (event) => {
114
+ if (event.defaultPrevented || event.target.closest('button, a, dialog, summary')) return
115
+ if (event.target.closest('.home-app-line')) opened.push('row')
116
+ })
117
+ await p.open()
118
+ await p.press('row')
119
+ assert.equal(p.isOpen(), false)
120
+ await p.click('row')
121
+ assert.deepEqual(opened, [])
122
+ // the click after that is an ordinary one
123
+ await p.press('row')
124
+ await p.click('row')
125
+ assert.deepEqual(opened, ['row'])
126
+ // a button pressed to close the menu still gets its click
127
+ await p.open()
128
+ let clicked = 0
129
+ p.document.getElementById('trigger').addEventListener('click', () => { clicked += 1 })
130
+ await p.press('elsewhere')
131
+ await p.click('trigger')
132
+ assert.equal(clicked, 1)
133
+ })
134
+
135
+ test('picking a command closes the menu, after the page has handled the click', async (t) => {
136
+ for (const id of ['settings', 'run', 'stop']) {
137
+ const p = await page(t)
138
+ await p.open()
139
+ let openWhenHandled = null
140
+ p.document.addEventListener('click', () => { openWhenHandled = p.isOpen() })
141
+ await p.click(id)
142
+ assert.equal(openWhenHandled, true, id)
143
+ assert.equal(p.isOpen(), false, id)
144
+ }
145
+ })
146
+
147
+ test('opening or closing a group of launcher items leaves the menu open', async (t) => {
148
+ const p = await page(t)
149
+ await p.open()
150
+ await p.click('group')
151
+ assert.equal(p.isOpen(), true)
152
+ })
153
+
154
+ test('the page scrolling or the window resizing closes the menu', async (t) => {
155
+ const p = await page(t)
156
+ await p.open()
157
+ p.window.dispatchEvent(new p.window.Event('scroll'))
158
+ assert.equal(p.isOpen(), false)
159
+ await p.open()
160
+ p.window.dispatchEvent(new p.window.Event('resize'))
161
+ assert.equal(p.isOpen(), false)
162
+ })
163
+
164
+ test('a narrow window\'s dialog is left as the page shows it', async (t) => {
165
+ const p = await page(t)
166
+ // a place left over from a wide window is taken off
167
+ await p.open()
168
+ p.dialog.removeAttribute('open')
169
+ await p.open(false)
170
+ assert.equal(p.dialog.style.left, '')
171
+ assert.equal(p.dialog.style.top, '')
172
+ await p.press('elsewhere')
173
+ await p.click('settings')
174
+ assert.equal(p.isOpen(), true)
175
+ })
176
+
177
+ test('the dialog\'s Share section is marked, so the menu can leave it out', async () => {
178
+ const html = await ejs.renderFile(MODAL, {
179
+ id: 'actions-1',
180
+ item: { name: 'Demo', filepath: '/home/api/demo', running: true, menu: [], uri: 'demo', view_url: '/v/demo', url: '/demo' }
181
+ })
182
+ const document = new JSDOM(html).window.document
183
+ const share = document.querySelector('.home-actions-section--share')
184
+ assert.ok(share)
185
+ assert.ok(share.querySelector('.home-share-app'))
186
+ assert.equal(document.querySelectorAll('.home-actions-section--share').length, 1)
187
+ })
@@ -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
+ })