pinokiod 8.3.15 → 8.3.16
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.
- package/package.json +1 -1
- package/server/index.js +2 -0
- package/server/lib/session_files.js +248 -0
- package/server/public/agent-bar-app.js +28 -6
- package/server/public/agent-bar-compose.js +18 -9
- package/server/public/agent-bar-files.css +391 -0
- package/server/public/agent-bar-files.js +394 -0
- package/server/public/agent-bar-panel.js +104 -14
- package/server/public/style.css +114 -0
- package/server/public/terminal-settings-sheet.js +66 -0
- package/server/public/terminal-settings.js +6 -1
- package/server/views/app.ejs +2 -0
- package/server/views/bootstrap.ejs +1 -0
- package/server/views/editor.ejs +1 -0
- package/server/views/init/index.ejs +1 -0
- package/server/views/install.ejs +1 -0
- package/server/views/layout.ejs +13 -0
- package/server/views/partials/main_sidebar.ejs +2 -0
- package/server/views/pro.ejs +1 -0
- package/server/views/prototype/index.ejs +1 -0
- package/server/views/shell.ejs +1 -0
- package/server/views/terminal.ejs +1 -0
- package/test/agent-bar-compose.test.js +45 -0
- package/test/agent-bar-files.test.js +276 -0
- package/test/agent-bar-keyboard.test.js +84 -0
- package/test/agent-bar-running.test.js +148 -0
- package/test/agent-bar-step-aside.test.js +72 -0
- package/test/layout-soft-keyboard.test.js +12 -0
- package/test/session-files.test.js +214 -0
- package/test/terminal-settings-font-size.test.js +47 -0
- package/test/terminal-settings-sheet.test.js +58 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const assert = require('node:assert/strict')
|
|
2
|
+
const fs = require('node:fs')
|
|
3
|
+
const path = require('node:path')
|
|
4
|
+
const test = require('node:test')
|
|
5
|
+
const { JSDOM, ResourceLoader } = require('jsdom')
|
|
6
|
+
|
|
7
|
+
const script = (name) => fs.readFileSync(path.resolve(__dirname, '../server/public', name), 'utf8')
|
|
8
|
+
const keyboardInset = '--agent-dock-keyboard'
|
|
9
|
+
|
|
10
|
+
class TerminalPage extends ResourceLoader {
|
|
11
|
+
fetch() {
|
|
12
|
+
return Promise.resolve(Buffer.from('<!doctype html><div id="terminal"><textarea></textarea></div>'))
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
test('the mobile Agent Bar panel stays under a terminal tap until Safari delivers its click', async (t) => {
|
|
17
|
+
const dom = new JSDOM('<!doctype html><body class="app-page"></body>', {
|
|
18
|
+
url: 'https://pinokio.example/p/demo',
|
|
19
|
+
resources: new TerminalPage(),
|
|
20
|
+
runScripts: 'outside-only',
|
|
21
|
+
pretendToBeVisual: true,
|
|
22
|
+
})
|
|
23
|
+
t.after(() => dom.window.close())
|
|
24
|
+
const { window } = dom
|
|
25
|
+
const { document } = window
|
|
26
|
+
const viewport = Object.assign(new window.EventTarget(), { height: 695, offsetTop: 0 })
|
|
27
|
+
window.innerWidth = 390
|
|
28
|
+
window.innerHeight = 695
|
|
29
|
+
window.visualViewport = viewport
|
|
30
|
+
window.fetch = async () => ({ ok: true, json: async () => [] })
|
|
31
|
+
window.eval(script('agent-bar.js'))
|
|
32
|
+
window.eval(script('agent-bar-panel.js'))
|
|
33
|
+
await window.PinokioAgentBar.ready
|
|
34
|
+
assert.equal(window.PinokioAgentDock.openWithAgent({
|
|
35
|
+
agentHref: '/pinokio/run/plugin/codex/pinokio.js?cwd=/home/api/demo&session=test',
|
|
36
|
+
}), true)
|
|
37
|
+
const panel = document.querySelector('.agent-bar-panel')
|
|
38
|
+
const frame = panel.querySelector('iframe')
|
|
39
|
+
await new Promise((resolve) => frame.addEventListener('load', resolve, { once: true }))
|
|
40
|
+
|
|
41
|
+
const terminal = frame.contentDocument.querySelector('#terminal')
|
|
42
|
+
const pointer = (type, pointerType = 'touch') => terminal.dispatchEvent(Object.assign(
|
|
43
|
+
new frame.contentWindow.Event(type, { bubbles: true }), { pointerType, isPrimary: true },
|
|
44
|
+
))
|
|
45
|
+
const resize = (height) => {
|
|
46
|
+
viewport.height = height
|
|
47
|
+
viewport.dispatchEvent(new window.Event('resize'))
|
|
48
|
+
}
|
|
49
|
+
const panelInset = () => panel.style.getPropertyValue(keyboardInset)
|
|
50
|
+
const pageInset = () => document.documentElement.style.getPropertyValue(keyboardInset)
|
|
51
|
+
const nextFrame = () => new Promise((resolve) => window.requestAnimationFrame(resolve))
|
|
52
|
+
|
|
53
|
+
pointer('pointerdown')
|
|
54
|
+
terminal.querySelector('textarea').focus()
|
|
55
|
+
pointer('pointerup')
|
|
56
|
+
resize(392)
|
|
57
|
+
assert.equal(pageInset(), '303px', 'the latest keyboard measurement is available')
|
|
58
|
+
assert.equal(panelInset(), '0px', 'the panel still covers the original tap target')
|
|
59
|
+
|
|
60
|
+
terminal.click()
|
|
61
|
+
assert.equal(panelInset(), '0px', 'the tap target stays in place through click dispatch')
|
|
62
|
+
await nextFrame()
|
|
63
|
+
assert.equal(panelInset(), '', 'the panel now uses the measured keyboard inset')
|
|
64
|
+
|
|
65
|
+
pointer('pointerdown')
|
|
66
|
+
assert.equal(panelInset(), '303px')
|
|
67
|
+
pointer('pointercancel')
|
|
68
|
+
assert.equal(panelInset(), '', 'a canceled tap releases the inset even without a click')
|
|
69
|
+
|
|
70
|
+
pointer('pointerdown', 'mouse')
|
|
71
|
+
assert.equal(panelInset(), '', 'mouse interactions keep the normal viewport updates')
|
|
72
|
+
|
|
73
|
+
pointer('pointerdown')
|
|
74
|
+
frame.contentWindow.dispatchEvent(new frame.contentWindow.Event('pagehide'))
|
|
75
|
+
assert.equal(panelInset(), '', 'leaving the frame releases a pending tap')
|
|
76
|
+
|
|
77
|
+
pointer('pointerdown')
|
|
78
|
+
await new Promise((resolve) => window.setTimeout(resolve, 1100))
|
|
79
|
+
assert.equal(panelInset(), '', 'a missing click cannot leave the panel pinned')
|
|
80
|
+
|
|
81
|
+
window.innerWidth = 1024
|
|
82
|
+
pointer('pointerdown')
|
|
83
|
+
assert.equal(panelInset(), '', 'wide floating panels keep their normal positioning')
|
|
84
|
+
})
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
// "N running" in the agent bar is the same number on every page: every running
|
|
2
|
+
// agent session, wherever it runs, and every shell opened inside an app. On an
|
|
3
|
+
// app's page the list starts with that app's own sessions.
|
|
4
|
+
//
|
|
5
|
+
// The real agent-bar.js, agent-bar-app.js and agent-bar-panel.js run here in a
|
|
6
|
+
// page that has only what they look for.
|
|
7
|
+
const assert = require('node:assert/strict')
|
|
8
|
+
const fs = require('node:fs')
|
|
9
|
+
const path = require('node:path')
|
|
10
|
+
const test = require('node:test')
|
|
11
|
+
const { JSDOM } = require('jsdom')
|
|
12
|
+
|
|
13
|
+
const script = (name) => fs.readFileSync(path.resolve(__dirname, '../server/public', name), 'utf8')
|
|
14
|
+
|
|
15
|
+
const HOME_PAGE = '<!doctype html><body class="main-sidebar-page"><header class="navheader"><h1><div class="flexible"></div></h1></header><main id="page"></main></body>'
|
|
16
|
+
// an app's page in Run: the app's name, the Ask AI button and panel (which carry
|
|
17
|
+
// the app's folder) and the sidebar the app's card moves to
|
|
18
|
+
const APP_PAGE = '<!doctype html><body class="app-page"><header class="navheader"><h1>'
|
|
19
|
+
+ '<div class="app-header-identity"><span class="app-header-identity-title">Demo App</span></div>'
|
|
20
|
+
+ '<button id="ask-ai-tab" data-workspace-cwd="/home/api/demo"></button><div class="flexible"></div></h1></header>'
|
|
21
|
+
+ '<div class="appcanvas"><aside id="app-sidebar"><div class="menu-scroller"></div></aside><main id="page"></main></div>'
|
|
22
|
+
+ '<div id="ask-ai-drawer" data-workspace-cwd="/home/api/demo"></div></body>'
|
|
23
|
+
|
|
24
|
+
const agent = (id, cwd, session, start) => ({ id, group: `/home/plugin/codex/pinokio.js?cwd=${cwd}&session=${session}`, start_time: start })
|
|
25
|
+
const OWN = agent('a', '/home/api/demo', 's1', 1)
|
|
26
|
+
const OTHER_APP = agent('b', '/home/api/other', 's2', 2)
|
|
27
|
+
const WORKSPACE = agent('c', '/home/workspaces/notes', 's3', 3)
|
|
28
|
+
// a shell opened inside this app, and one of this app's own running scripts
|
|
29
|
+
const OWN_SHELL = { id: 'shell/demo_bash?session=s4', path: '/home/api/demo', start_time: 4 }
|
|
30
|
+
const OWN_SCRIPT = { id: '/home/api/demo/start.js', path: '/home/api/demo', start_time: 5 }
|
|
31
|
+
|
|
32
|
+
async function settle(window) {
|
|
33
|
+
for (let i = 0; i < 6; i += 1) await new Promise((resolve) => window.setTimeout(resolve, 0))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function page(t, { app, shells }) {
|
|
37
|
+
const running = { list: shells }
|
|
38
|
+
const dom = new JSDOM(app ? APP_PAGE : HOME_PAGE, {
|
|
39
|
+
url: `http://127.0.0.1:42000/${app ? 'p/demo' : 'home'}`,
|
|
40
|
+
runScripts: 'outside-only',
|
|
41
|
+
pretendToBeVisual: true,
|
|
42
|
+
beforeParse(window) {
|
|
43
|
+
window.matchMedia = () => ({ matches: false, addEventListener() {}, removeEventListener() {} })
|
|
44
|
+
window.Element.prototype.getBoundingClientRect = function () {
|
|
45
|
+
return { left: 300, top: 8, right: 900, bottom: 40, width: 600, height: 32, x: 300, y: 8 }
|
|
46
|
+
}
|
|
47
|
+
window.fetch = async (url) => {
|
|
48
|
+
const address = String(url)
|
|
49
|
+
if (address === '/info/shells') return { ok: true, json: async () => running.list }
|
|
50
|
+
if (address.split('?')[0] === '/api/plugin/menu') {
|
|
51
|
+
return {
|
|
52
|
+
ok: true,
|
|
53
|
+
json: async () => ({
|
|
54
|
+
menu: [{ href: '/run/plugin/codex/pinokio.js', title: 'Codex', default: true }],
|
|
55
|
+
projectShell: { items: [{ href: '/shell/demo_bash', title: 'Bash' }] },
|
|
56
|
+
}),
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (address === '/info/apps') return { ok: true, json: async () => ({ apps: [{ name: 'other', title: 'Other App' }] }) }
|
|
60
|
+
return { ok: false, json: async () => ({}) }
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
t.after(() => dom.window.close())
|
|
65
|
+
const { window } = dom
|
|
66
|
+
window.eval(script('agent-bar.js'))
|
|
67
|
+
if (app) window.eval(script('agent-bar-app.js'))
|
|
68
|
+
window.eval(script('agent-bar-panel.js'))
|
|
69
|
+
await window.PinokioAgentBar.ready
|
|
70
|
+
await settle(window)
|
|
71
|
+
const root = window.document.querySelector('.agent-bar')
|
|
72
|
+
const chip = () => root.querySelector('.agent-bar__running')
|
|
73
|
+
const list = () => root.querySelector(app ? '.agent-bar__menu[data-menu="running"]' : '.agent-bar__menu--running')
|
|
74
|
+
return {
|
|
75
|
+
window,
|
|
76
|
+
root,
|
|
77
|
+
chip,
|
|
78
|
+
list,
|
|
79
|
+
running,
|
|
80
|
+
count: () => (chip() && !chip().hidden ? parseInt(chip().textContent, 10) : 0),
|
|
81
|
+
titles: () => Array.from(list().querySelectorAll('.agent-bar__menu-title')).map((node) => node.textContent),
|
|
82
|
+
async open() {
|
|
83
|
+
chip().click()
|
|
84
|
+
await settle(window)
|
|
85
|
+
},
|
|
86
|
+
// the dock looks at what is running again, as it does every few seconds
|
|
87
|
+
async look() {
|
|
88
|
+
if (app) await window.PinokioAgentDock.look()
|
|
89
|
+
else window.document.dispatchEvent(new window.Event('visibilitychange'))
|
|
90
|
+
await settle(window)
|
|
91
|
+
},
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const EVERYTHING = [OWN, OTHER_APP, WORKSPACE, OWN_SHELL, OWN_SCRIPT]
|
|
96
|
+
|
|
97
|
+
test('the count is the same on the home page and on an app\'s page', async (t) => {
|
|
98
|
+
const home = await page(t, { shells: EVERYTHING })
|
|
99
|
+
const app = await page(t, { app: true, shells: EVERYTHING })
|
|
100
|
+
// three agents and the shell; the app's own script is not a session
|
|
101
|
+
assert.equal(home.count(), 4)
|
|
102
|
+
assert.equal(app.count(), 4)
|
|
103
|
+
assert.match(app.chip().textContent, /^4 running$/)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('app page: sessions that run only in other places are counted and listed', async (t) => {
|
|
107
|
+
const p = await page(t, { app: true, shells: [OTHER_APP, WORKSPACE] })
|
|
108
|
+
assert.equal(p.count(), 2)
|
|
109
|
+
await p.open()
|
|
110
|
+
assert.deepEqual(p.titles(), ['Workspaces', 'Other App'])
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('app page: the list starts with this app\'s sessions, then the other places', async (t) => {
|
|
114
|
+
const p = await page(t, { app: true, shells: EVERYTHING })
|
|
115
|
+
await p.open()
|
|
116
|
+
assert.deepEqual(p.titles(), ['This app', 'Workspaces', 'Other App'])
|
|
117
|
+
const rows = Array.from(p.list().children)
|
|
118
|
+
const firstOther = rows.findIndex((node) => node.textContent === 'Workspaces')
|
|
119
|
+
// this app's shell and its agent, before any other place's heading
|
|
120
|
+
assert.equal(rows.slice(1, firstOther).length, 2)
|
|
121
|
+
assert.equal(p.list().querySelectorAll('.agent-dock__liststop').length, 4, 'every row has Stop')
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('home page: no "This app" heading, an app\'s sessions stay under the app', async (t) => {
|
|
125
|
+
const p = await page(t, { shells: EVERYTHING })
|
|
126
|
+
await p.open()
|
|
127
|
+
assert.deepEqual(p.titles(), ['Workspaces', 'demo', 'Other App'])
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test('app page: the count follows a session that ends in another place', async (t) => {
|
|
131
|
+
const p = await page(t, { app: true, shells: [OWN, OTHER_APP] })
|
|
132
|
+
assert.equal(p.count(), 2)
|
|
133
|
+
p.running.list = [OWN]
|
|
134
|
+
await p.look()
|
|
135
|
+
assert.equal(p.count(), 1)
|
|
136
|
+
p.running.list = []
|
|
137
|
+
await p.look()
|
|
138
|
+
assert.equal(p.chip().hidden, true)
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
test('app page: choosing a row closes the list', async (t) => {
|
|
142
|
+
const p = await page(t, { app: true, shells: [OWN, OTHER_APP] })
|
|
143
|
+
await p.open()
|
|
144
|
+
assert.equal(p.list().hidden, false)
|
|
145
|
+
p.list().querySelector('.agent-bar__row').click()
|
|
146
|
+
await settle(p.window)
|
|
147
|
+
assert.equal(p.root.querySelector('.agent-bar__menu').hidden, true)
|
|
148
|
+
})
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// On a phone a session's window covers the whole page. When the page changes,
|
|
2
|
+
// the window steps aside to its tab so the new page can be seen.
|
|
3
|
+
const assert = require('node:assert/strict')
|
|
4
|
+
const fs = require('node:fs')
|
|
5
|
+
const path = require('node:path')
|
|
6
|
+
const test = require('node:test')
|
|
7
|
+
const { JSDOM } = require('jsdom')
|
|
8
|
+
|
|
9
|
+
const script = (name) => fs.readFileSync(path.resolve(__dirname, '../server/public', name), 'utf8')
|
|
10
|
+
const TABS = 'pinokio.agentBar.dock.tabs'
|
|
11
|
+
const PAGE = 'pinokio.agentBar.dock.page'
|
|
12
|
+
|
|
13
|
+
// Loads `to` with one window left open on the page `from`; returns the saved tabs.
|
|
14
|
+
const arrive = (t, { from, to, width }) => {
|
|
15
|
+
const dom = new JSDOM('<!doctype html><body class="app-page"></body>', {
|
|
16
|
+
url: `https://pinokio.example${to}`,
|
|
17
|
+
runScripts: 'outside-only',
|
|
18
|
+
pretendToBeVisual: true,
|
|
19
|
+
})
|
|
20
|
+
t.after(() => dom.window.close())
|
|
21
|
+
const { window } = dom
|
|
22
|
+
window.innerWidth = width
|
|
23
|
+
window.innerHeight = 695
|
|
24
|
+
window.fetch = () => new Promise(() => {})
|
|
25
|
+
window.sessionStorage.setItem(TABS, JSON.stringify([
|
|
26
|
+
{ key: 'one', kind: 'agent', label: 'Agent', open: true, z: 2 },
|
|
27
|
+
{ key: 'two', kind: 'agent', label: 'Agent', open: false, z: 1 },
|
|
28
|
+
]))
|
|
29
|
+
if (from) window.sessionStorage.setItem(PAGE, JSON.stringify(from))
|
|
30
|
+
window.eval(script('agent-bar.js'))
|
|
31
|
+
window.eval(script('agent-bar-panel.js'))
|
|
32
|
+
return {
|
|
33
|
+
window,
|
|
34
|
+
tabs: JSON.parse(window.sessionStorage.getItem(TABS)),
|
|
35
|
+
page: JSON.parse(window.sessionStorage.getItem(PAGE)),
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test('on a phone an open window steps aside to its tab when the page changes', (t) => {
|
|
40
|
+
const { tabs, page } = arrive(t, { from: '/p/demo', to: '/p/demo/dev', width: 390 })
|
|
41
|
+
assert.deepEqual(tabs.map((tab) => tab.key), ['one', 'two'], 'both tabs are kept')
|
|
42
|
+
assert.deepEqual(tabs.map((tab) => tab.open), [false, false], 'no window covers the new page')
|
|
43
|
+
assert.equal(page, '/p/demo/dev', 'the new page is remembered for the next change')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('on a phone the window comes back when the same page loads again', (t) => {
|
|
47
|
+
const { tabs } = arrive(t, { from: '/p/demo?tab=1', to: '/p/demo?tab=1', width: 390 })
|
|
48
|
+
assert.deepEqual(tabs.map((tab) => tab.open), [true, false])
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('on a phone a different address on the same path is a page change', (t) => {
|
|
52
|
+
const { tabs } = arrive(t, { from: '/p/demo?tab=1', to: '/p/demo?tab=2', width: 390 })
|
|
53
|
+
assert.deepEqual(tabs.map((tab) => tab.open), [false, false])
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
test('on a wide screen the window follows you to the new page', (t) => {
|
|
57
|
+
const { tabs } = arrive(t, { from: '/p/demo', to: '/p/demo/dev', width: 1280 })
|
|
58
|
+
assert.deepEqual(tabs.map((tab) => tab.open), [true, false])
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test('with no page remembered the windows are left as they were saved', (t) => {
|
|
62
|
+
const { tabs, page } = arrive(t, { from: '', to: '/p/demo', width: 390 })
|
|
63
|
+
assert.deepEqual(tabs.map((tab) => tab.open), [true, false])
|
|
64
|
+
assert.equal(page, '/p/demo')
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
test('a page that rewrites its own address is remembered by the address it was left at', (t) => {
|
|
68
|
+
const { window } = arrive(t, { from: '', to: '/home', width: 390 })
|
|
69
|
+
window.history.replaceState(null, '', '/home?sort=most_used')
|
|
70
|
+
window.dispatchEvent(new window.Event('pagehide'))
|
|
71
|
+
assert.equal(JSON.parse(window.sessionStorage.getItem(PAGE)), '/home?sort=most_used')
|
|
72
|
+
})
|
|
@@ -85,6 +85,18 @@ test('with the keyboard up, the visible part is what is left above it', (t) => {
|
|
|
85
85
|
assert.deepEqual(page.measured(), { height: '812px', top: '0px' })
|
|
86
86
|
})
|
|
87
87
|
|
|
88
|
+
test('the outer layout defers its keyboard resize until a terminal tap finishes', (t) => {
|
|
89
|
+
const viewport = fullWindow()
|
|
90
|
+
const page = shell(t, viewport)
|
|
91
|
+
const finishTap = page.window.PinokioLayoutViewport.hold()
|
|
92
|
+
Object.assign(viewport, { height: 470, pageTop: 40 })
|
|
93
|
+
page.resize()
|
|
94
|
+
assert.deepEqual(page.measured(), { height: '812px', top: '0px' })
|
|
95
|
+
|
|
96
|
+
finishTap()
|
|
97
|
+
assert.deepEqual(page.measured(), { height: '470px', top: '40px' })
|
|
98
|
+
})
|
|
99
|
+
|
|
88
100
|
test('when the browser slides the page up under the keyboard, the panes follow', (t) => {
|
|
89
101
|
const viewport = fullWindow()
|
|
90
102
|
const page = shell(t, viewport)
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
// The files an agent made for a session, as a page reads them: only that
|
|
4
|
+
// session's output folder, nothing outside it, and markdown made safe.
|
|
5
|
+
|
|
6
|
+
const test = require("node:test")
|
|
7
|
+
const assert = require("node:assert/strict")
|
|
8
|
+
const fs = require("fs")
|
|
9
|
+
const os = require("os")
|
|
10
|
+
const path = require("path")
|
|
11
|
+
const http = require("http")
|
|
12
|
+
const express = require("express")
|
|
13
|
+
const SessionFiles = require("../server/lib/session_files")
|
|
14
|
+
|
|
15
|
+
const ID = "20261118-211145-a46df201"
|
|
16
|
+
|
|
17
|
+
// A Pinokio home with one session in the brain and one in an app, and the
|
|
18
|
+
// addresses served from it.
|
|
19
|
+
const withHome = async (run) => {
|
|
20
|
+
const home = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "pinokio-session-files-")))
|
|
21
|
+
const brain = path.join(home, "brain")
|
|
22
|
+
const appDir = path.join(home, "api", "Speech")
|
|
23
|
+
const brainOut = path.join(brain, "sessions", ID, "output")
|
|
24
|
+
const appOut = path.join(appDir, ".pinokio-sessions", ID, "output")
|
|
25
|
+
fs.mkdirSync(brainOut, { recursive: true })
|
|
26
|
+
fs.mkdirSync(appOut, { recursive: true })
|
|
27
|
+
fs.writeFileSync(path.join(home, "secret.txt"), "not for a page")
|
|
28
|
+
fs.writeFileSync(path.join(brain, "sessions", ID, "request.md"), "the request")
|
|
29
|
+
const app = express()
|
|
30
|
+
SessionFiles.registerSessionFiles(app, { kernel: { homedir: home } })
|
|
31
|
+
const server = http.createServer(app)
|
|
32
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve))
|
|
33
|
+
const base = `http://127.0.0.1:${server.address().port}`
|
|
34
|
+
const address = (tail, cwd, more = {}) => `${base}/pinokio/session-files${tail}?${new URLSearchParams({ cwd, session: ID, ...more })}`
|
|
35
|
+
try {
|
|
36
|
+
await run({ home, brain, appDir, brainOut, appOut, address })
|
|
37
|
+
} finally {
|
|
38
|
+
await new Promise((resolve) => server.close(resolve))
|
|
39
|
+
fs.rmSync(home, { recursive: true, force: true })
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const write = (dir, relative, text) => {
|
|
43
|
+
const file = path.join(dir, ...relative.split("/"))
|
|
44
|
+
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
45
|
+
fs.writeFileSync(file, text)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
test("a session's output folder is in the brain's sessions or in the app's own folder", () => {
|
|
49
|
+
const home = path.resolve("/home/pinokio")
|
|
50
|
+
assert.equal(SessionFiles.outputDir(home, path.join(home, "brain"), ID), path.join(home, "brain", "sessions", ID, "output"))
|
|
51
|
+
assert.equal(SessionFiles.outputDir(home, `${path.join(home, "brain")}${path.sep}`, ID), path.join(home, "brain", "sessions", ID, "output"), "a trailing separator is the same folder")
|
|
52
|
+
assert.equal(SessionFiles.outputDir(home, path.join(home, "api", "Speech"), ID), path.join(home, "api", "Speech", ".pinokio-sessions", ID, "output"))
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
test("no other folder has session files", () => {
|
|
56
|
+
const home = path.resolve("/home/pinokio")
|
|
57
|
+
const refused = (cwd, id, status) => assert.throws(() => SessionFiles.outputDir(home, cwd, id), (error) => error.status === status)
|
|
58
|
+
refused(home, ID, 404)
|
|
59
|
+
refused(path.join(home, "api"), ID, 404)
|
|
60
|
+
refused(path.join(home, "api", "Speech", "app"), ID, 404)
|
|
61
|
+
refused(path.join(home, "workspaces", "notes"), ID, 404)
|
|
62
|
+
refused(path.join(home, "brain", "..", ".."), ID, 404)
|
|
63
|
+
refused(path.resolve("/somewhere/else"), ID, 404)
|
|
64
|
+
refused("brain", ID, 400)
|
|
65
|
+
refused("", ID, 400)
|
|
66
|
+
refused(path.join(home, "brain"), "../../etc", 400)
|
|
67
|
+
refused(path.join(home, "brain"), "20261118-211145-a46df201/..", 400)
|
|
68
|
+
refused(path.join(home, "brain"), undefined, 400)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
test("a session that made nothing has an empty list", async () => {
|
|
72
|
+
await withHome(async ({ brain, address }) => {
|
|
73
|
+
const body = await fetch(address("", brain)).then((response) => response.json())
|
|
74
|
+
assert.deepEqual(body, { ok: true, files: [], more: false })
|
|
75
|
+
// a session whose folder was never made (an empty request) is the same
|
|
76
|
+
const none = await fetch(address("", brain, { session: "20200101-000000-00000000" })).then((response) => response.json())
|
|
77
|
+
assert.deepEqual(none, { ok: true, files: [], more: false })
|
|
78
|
+
})
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test("every file in the output folder is listed by its path, folders included", async () => {
|
|
82
|
+
await withHome(async ({ brain, brainOut, appDir, appOut, address }) => {
|
|
83
|
+
write(brainOut, "report/README.md", "# hi")
|
|
84
|
+
write(brainOut, "report/assets/b10.png", "png")
|
|
85
|
+
write(brainOut, "report/assets/b2.png", "png")
|
|
86
|
+
write(brainOut, "summary.txt", "12345")
|
|
87
|
+
write(appOut, "voice.wav", "wav")
|
|
88
|
+
const body = await fetch(address("", brain)).then((response) => response.json())
|
|
89
|
+
assert.deepEqual(body.files.map((file) => file.path), ["report/assets/b2.png", "report/assets/b10.png", "report/README.md", "summary.txt"])
|
|
90
|
+
assert.equal(body.files[3].size, 5)
|
|
91
|
+
assert.equal(typeof body.files[3].modified, "number")
|
|
92
|
+
const inApp = await fetch(address("", appDir)).then((response) => response.json())
|
|
93
|
+
assert.deepEqual(inApp.files.map((file) => file.path), ["voice.wav"], "an app's session lists its own folder only")
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test("a link out of the output folder is neither listed nor served", async (t) => {
|
|
98
|
+
await withHome(async ({ home, brain, brainOut, address }) => {
|
|
99
|
+
write(brainOut, "real.txt", "real")
|
|
100
|
+
try {
|
|
101
|
+
fs.symlinkSync(path.join(home, "secret.txt"), path.join(brainOut, "link.txt"))
|
|
102
|
+
fs.symlinkSync(home, path.join(brainOut, "up"))
|
|
103
|
+
} catch (error) {
|
|
104
|
+
t.skip("links cannot be made here")
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
const body = await fetch(address("", brain)).then((response) => response.json())
|
|
108
|
+
assert.deepEqual(body.files.map((file) => file.path), ["real.txt"])
|
|
109
|
+
assert.equal((await fetch(address("/file/link.txt", brain))).status, 404)
|
|
110
|
+
assert.equal((await fetch(address("/file/up/secret.txt", brain))).status, 404)
|
|
111
|
+
})
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
test("a file is served as itself, can be saved, and nothing outside the folder is", async () => {
|
|
115
|
+
await withHome(async ({ brain, brainOut, address }) => {
|
|
116
|
+
write(brainOut, "report/notes one.txt", "hello there")
|
|
117
|
+
const shown = await fetch(address("/file/report/notes%20one.txt", brain))
|
|
118
|
+
assert.equal(shown.status, 200)
|
|
119
|
+
assert.equal(await shown.text(), "hello there")
|
|
120
|
+
assert.match(shown.headers.get("content-type"), /^text\/plain/)
|
|
121
|
+
assert.match(shown.headers.get("content-security-policy"), /^sandbox;/, "opened by itself, a file an agent made runs nothing")
|
|
122
|
+
assert.equal(shown.headers.get("x-content-type-options"), "nosniff")
|
|
123
|
+
assert.equal(shown.headers.get("content-disposition"), null, "shown in place")
|
|
124
|
+
const saved = await fetch(address("/file/report/notes%20one.txt", brain, { download: "1" }))
|
|
125
|
+
assert.match(saved.headers.get("content-disposition"), /^attachment; filename="notes one.txt"/)
|
|
126
|
+
const part = await fetch(address("/file/report/notes%20one.txt", brain), { headers: { Range: "bytes=0-4" } })
|
|
127
|
+
assert.equal(part.status, 206, "a video can be read in pieces")
|
|
128
|
+
assert.equal(await part.text(), "hello")
|
|
129
|
+
|
|
130
|
+
for (const escape of ["/file/..%2F..%2Frequest.md", "/file/%2E%2E/request.md", "/file/report/..%2F..%2F..%2Frequest.md", "/file/..%5C..%5Crequest.md"]) {
|
|
131
|
+
const response = await fetch(address(escape, brain))
|
|
132
|
+
assert.ok([400, 404].includes(response.status), `${escape} is refused (${response.status})`)
|
|
133
|
+
assert.notEqual(await response.text(), "the request")
|
|
134
|
+
}
|
|
135
|
+
assert.equal((await fetch(address("/file/missing.txt", brain))).status, 404)
|
|
136
|
+
assert.equal((await fetch(address("/file/report", brain))).status, 404, "a folder is not a file")
|
|
137
|
+
})
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
test("a file in a hidden folder of the session is served", async () => {
|
|
141
|
+
await withHome(async ({ appDir, appOut, address }) => {
|
|
142
|
+
write(appOut, ".cache/a.txt", "x")
|
|
143
|
+
const response = await fetch(address("/file/.cache/a.txt", appDir))
|
|
144
|
+
assert.equal(response.status, 200)
|
|
145
|
+
assert.equal(await response.text(), "x")
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
test("a link in a file leads to another file of the session, or nowhere", () => {
|
|
150
|
+
const at = (href) => SessionFiles.linkedPath("report/README.md", href)
|
|
151
|
+
assert.equal(at("assets/pic.png"), "report/assets/pic.png")
|
|
152
|
+
assert.equal(at("./assets/pic%20one.png?raw=1#top"), "report/assets/pic one.png")
|
|
153
|
+
assert.equal(at("../summary.wav"), "summary.wav")
|
|
154
|
+
assert.equal(at("/summary.wav"), "summary.wav", "from the top of the output folder")
|
|
155
|
+
assert.equal(at("../../outside.txt"), null, "it never climbs out")
|
|
156
|
+
assert.equal(at("#section"), null)
|
|
157
|
+
assert.equal(at("https://example.com/a.png"), null)
|
|
158
|
+
assert.equal(at("//example.com/a.png"), null)
|
|
159
|
+
assert.equal(at("javascript:alert(1)"), null)
|
|
160
|
+
assert.equal(at("data:text/html,x"), null)
|
|
161
|
+
assert.equal(at("%E0%A4%A"), null, "an address that cannot be read")
|
|
162
|
+
assert.equal(at(""), null)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
test("markdown becomes a page with nothing of the file's own HTML in it", () => {
|
|
166
|
+
const cwd = "/home/pinokio/brain"
|
|
167
|
+
const html = SessionFiles.renderMarkdown([
|
|
168
|
+
"# Title",
|
|
169
|
+
"",
|
|
170
|
+
"<script>alert(1)</script>",
|
|
171
|
+
"",
|
|
172
|
+
"Text <img src=x onerror=alert(2)> and <b onclick=\"alert(3)\">bold</b>.",
|
|
173
|
+
"",
|
|
174
|
+
"",
|
|
175
|
+
"",
|
|
176
|
+
")",
|
|
177
|
+
"",
|
|
178
|
+
"[other file](notes/prompt.txt) [site](https://example.com/?a=1&b=\"2\") [bad](javascript:alert(5)) [worse](JaVaScRiPt:alert(6)) [data](data:text/html,<script>alert(7)</script>) [climb](../../../etc/passwd)"
|
|
179
|
+
].join("\n"), { from: "report/README.md", cwd, id: ID })
|
|
180
|
+
assert.match(html, /<h1>Title<\/h1>/)
|
|
181
|
+
assert.doesNotMatch(html, /<script/i)
|
|
182
|
+
assert.doesNotMatch(html, /onerror|onclick/i)
|
|
183
|
+
assert.doesNotMatch(html, /href="(?!#"|https?:\/\/)/i, "a link is a web address or a file of the session, nothing else")
|
|
184
|
+
assert.doesNotMatch(html, /src="(?!\/pinokio\/session-files\/file\/)/i, "a picture is a file of the session, nothing else")
|
|
185
|
+
assert.match(html, /<a href="#" data-session-file="report\/notes\/prompt.txt">other file<\/a>/)
|
|
186
|
+
assert.match(html, /<a href="https:\/\/example.com\/\?a=1&b=%222%22" target="_blank" rel="noopener noreferrer">site<\/a>|<a href="https:\/\/example.com\/\?a=1&b="2"" target="_blank" rel="noopener noreferrer">site<\/a>/)
|
|
187
|
+
assert.match(html, /<a href="https:\/\/example.com\/track.png" target="_blank" rel="noopener noreferrer">Remote<\/a>/, "a picture from another site is a link, not loaded")
|
|
188
|
+
const picture = /<img src="([^"]+)" alt="The picture"/.exec(html)
|
|
189
|
+
assert.ok(picture, "the session's own picture is shown")
|
|
190
|
+
const address = new URL(picture[1].replace(/&/g, "&"), "http://x")
|
|
191
|
+
assert.equal(decodeURIComponent(address.pathname), "/pinokio/session-files/file/report/assets/pic one.png")
|
|
192
|
+
assert.equal(address.searchParams.get("cwd"), cwd)
|
|
193
|
+
assert.equal(address.searchParams.get("session"), ID)
|
|
194
|
+
for (const word of ["bad", "worse", "data", "climb"]) {
|
|
195
|
+
assert.doesNotMatch(html, new RegExp(`<a[^>]*>${word}</a>`), `"${word}" is plain words`)
|
|
196
|
+
assert.match(html, new RegExp(word))
|
|
197
|
+
}
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
test("a markdown file is served as that page", async () => {
|
|
201
|
+
await withHome(async ({ brain, brainOut, address }) => {
|
|
202
|
+
write(brainOut, "report/README.md", "# Hello\n\n\n\n<script>bad()</script>\n")
|
|
203
|
+
write(brainOut, "report/assets/p.png", "png")
|
|
204
|
+
const body = await fetch(address("/markdown/report/README.md", brain)).then((response) => response.json())
|
|
205
|
+
assert.equal(body.ok, true)
|
|
206
|
+
assert.match(body.html, /<h1>Hello<\/h1>/)
|
|
207
|
+
assert.doesNotMatch(body.html, /<script/)
|
|
208
|
+
const src = /<img src="([^"]+)"/.exec(body.html)[1].replace(/&/g, "&")
|
|
209
|
+
const picture = await fetch(new URL(src, address("", brain)))
|
|
210
|
+
assert.equal(picture.status, 200, "the page's picture is found at the address it was given")
|
|
211
|
+
assert.equal(await picture.text(), "png")
|
|
212
|
+
assert.equal((await fetch(address("/markdown/report/none.md", brain))).status, 404)
|
|
213
|
+
})
|
|
214
|
+
})
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// The font size field of the terminal appearance menu
|
|
2
|
+
// (server/public/terminal-settings.js): a size is typed one digit at a time, so
|
|
3
|
+
// the field is not corrected under the fingers.
|
|
4
|
+
const assert = require('node:assert/strict')
|
|
5
|
+
const fs = require('node:fs')
|
|
6
|
+
const path = require('node:path')
|
|
7
|
+
const test = require('node:test')
|
|
8
|
+
const { JSDOM } = require('jsdom')
|
|
9
|
+
|
|
10
|
+
const script = fs.readFileSync(path.resolve(__dirname, '../server/public/terminal-settings.js'), 'utf8')
|
|
11
|
+
|
|
12
|
+
test('a font size can be typed digit by digit, and is settled on leaving the field', (t) => {
|
|
13
|
+
const dom = new JSDOM('<!doctype html><body><div id="runner"></div></body>', {
|
|
14
|
+
url: 'http://127.0.0.1:42000/',
|
|
15
|
+
runScripts: 'outside-only',
|
|
16
|
+
pretendToBeVisual: true,
|
|
17
|
+
})
|
|
18
|
+
t.after(() => dom.window.close())
|
|
19
|
+
const { window } = dom
|
|
20
|
+
window.eval(script)
|
|
21
|
+
const settings = window.PinokioTerminalSettings
|
|
22
|
+
const menu = settings.createMenu(window.document.getElementById('runner'))
|
|
23
|
+
// (as initRunnerMenus does for a terminal page's menu)
|
|
24
|
+
settings.menus.add(menu)
|
|
25
|
+
const { sizeInput } = menu
|
|
26
|
+
const type = (value) => {
|
|
27
|
+
sizeInput.value = value
|
|
28
|
+
sizeInput.dispatchEvent(new window.Event('input', { bubbles: true }))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type('1')
|
|
32
|
+
assert.equal(sizeInput.value, '1', 'the first digit of 14 stays as typed')
|
|
33
|
+
assert.equal(settings.preferences.fontSize, undefined)
|
|
34
|
+
type('14')
|
|
35
|
+
assert.equal(sizeInput.value, '14')
|
|
36
|
+
assert.equal(settings.preferences.fontSize, 14)
|
|
37
|
+
|
|
38
|
+
type('140')
|
|
39
|
+
assert.equal(sizeInput.value, '140')
|
|
40
|
+
assert.equal(settings.preferences.fontSize, 14, 'too large a number changes nothing while typing')
|
|
41
|
+
sizeInput.dispatchEvent(new window.Event('blur'))
|
|
42
|
+
assert.equal(sizeInput.value, '72', 'leaving the field brings it into range')
|
|
43
|
+
assert.equal(settings.preferences.fontSize, 72)
|
|
44
|
+
|
|
45
|
+
type('')
|
|
46
|
+
assert.equal(settings.preferences.fontSize, undefined, 'an emptied field goes back to the default size')
|
|
47
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// What server/public/terminal-settings-sheet.js adds to the terminal appearance
|
|
2
|
+
// menu for a phone: Done, a − and a + for the font size, and the fold of the
|
|
3
|
+
// theme colors. Which screens show them is the stylesheet's part and is not
|
|
4
|
+
// covered: this page has no layout.
|
|
5
|
+
const assert = require('node:assert/strict')
|
|
6
|
+
const fs = require('node:fs')
|
|
7
|
+
const path = require('node:path')
|
|
8
|
+
const test = require('node:test')
|
|
9
|
+
const { JSDOM } = require('jsdom')
|
|
10
|
+
|
|
11
|
+
const script = (name) => fs.readFileSync(path.resolve(__dirname, '../server/public', name), 'utf8')
|
|
12
|
+
|
|
13
|
+
test('the terminal appearance menu gets its phone parts as it is opened', (t) => {
|
|
14
|
+
const dom = new JSDOM('<!doctype html><body><div id="runner"></div></body>', {
|
|
15
|
+
url: 'http://127.0.0.1:42000/',
|
|
16
|
+
runScripts: 'outside-only',
|
|
17
|
+
pretendToBeVisual: true,
|
|
18
|
+
})
|
|
19
|
+
t.after(() => dom.window.close())
|
|
20
|
+
const { window } = dom
|
|
21
|
+
window.eval(script('terminal-settings.js'))
|
|
22
|
+
window.eval(script('terminal-settings-sheet.js'))
|
|
23
|
+
const settings = window.PinokioTerminalSettings
|
|
24
|
+
const record = settings.createMenu(window.document.getElementById('runner'))
|
|
25
|
+
// (as initRunnerMenus does for a terminal page's menu)
|
|
26
|
+
settings.menus.add(record)
|
|
27
|
+
const { menu, button, sizeInput, themeSection } = record
|
|
28
|
+
|
|
29
|
+
button.click()
|
|
30
|
+
assert.equal(menu.hidden, false)
|
|
31
|
+
assert.equal(menu.firstChild.className, 'terminal-config-sheet-head')
|
|
32
|
+
|
|
33
|
+
// the steps start from the size in use
|
|
34
|
+
sizeInput.placeholder = '12'
|
|
35
|
+
const [smaller, larger] = menu.querySelectorAll('.terminal-config-step')
|
|
36
|
+
assert.equal(smaller.nextSibling, sizeInput)
|
|
37
|
+
larger.click()
|
|
38
|
+
assert.equal(settings.preferences.fontSize, 13)
|
|
39
|
+
assert.equal(sizeInput.value, '13')
|
|
40
|
+
smaller.click()
|
|
41
|
+
smaller.click()
|
|
42
|
+
assert.equal(settings.preferences.fontSize, 11)
|
|
43
|
+
assert.equal(menu.hidden, false, 'a step leaves the menu open')
|
|
44
|
+
|
|
45
|
+
const fold = themeSection.querySelector('.terminal-config-fold')
|
|
46
|
+
assert.equal(fold.getAttribute('aria-expanded'), 'false')
|
|
47
|
+
fold.click()
|
|
48
|
+
assert.equal(fold.getAttribute('aria-expanded'), 'true')
|
|
49
|
+
assert.equal(themeSection.classList.contains('terminal-config-theme--open'), true)
|
|
50
|
+
|
|
51
|
+
menu.querySelector('.terminal-config-sheet-done').click()
|
|
52
|
+
assert.equal(menu.hidden, true)
|
|
53
|
+
|
|
54
|
+
// opened again, it is not dressed twice
|
|
55
|
+
button.click()
|
|
56
|
+
assert.equal(menu.querySelectorAll('.terminal-config-sheet-head, .terminal-config-fold').length, 2)
|
|
57
|
+
assert.equal(menu.querySelectorAll('.terminal-config-step').length, 2)
|
|
58
|
+
})
|