Repository navigation
tron pwa search / install: pwamart.com listings as TronBrowser app windows #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // `tron pwa install` resolves a pwamart.com listing to its start URL and opens it as | ||
| // a TronBrowser app window. Pinned here: it only ever launches https URLs, it goes | ||
| // through the launcher (never the engine), and a slug is looked up on the store. | ||
|
|
||
| import { spawn, spawnSync } from 'node:child_process'; | ||
| import { createServer } from 'node:http'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| const HERE = dirname(fileURLToPath(import.meta.url)); | ||
| const TRON_PWA = join(HERE, '..', 'launcher', 'tron-pwa'); | ||
| const CLI = '/opt/tron/bin/tron'; | ||
|
|
||
| const env = (extra: Record<string, string> = {}) => ({ PATH: process.env.PATH ?? '/usr/bin:/bin', HOME: '/nonexistent', TRONBROWSER_CLI: CLI, ...extra }); | ||
|
|
||
| function runAsync(args: string[], extra: Record<string, string>): Promise<{ stdout: string; status: number }> { | ||
| return new Promise((resolve) => { | ||
| const child = spawn('python3', [TRON_PWA, ...args], { env: env(extra) }); | ||
| let stdout = ''; | ||
| child.stdout.on('data', (d) => (stdout += d)); | ||
| child.on('close', (status) => resolve({ stdout, status: status ?? -1 })); | ||
| }); | ||
| } | ||
|
|
||
| describe('tron pwa install', () => { | ||
| it('opens an https URL as an app window through the launcher', () => { | ||
| const r = spawnSync('python3', [TRON_PWA, 'install', 'https://app.example/', '--dry-run'], { encoding: 'utf8', env: env() }); | ||
| expect(r.status).toBe(0); | ||
| expect(r.stdout.trim()).toBe(`${CLI} --app=https://app.example/`); | ||
| }); | ||
|
|
||
| it('refuses anything that is not a slug or an https URL', () => { | ||
| for (const bad of ['javascript:alert(1)', 'http://app.example/', '../etc/passwd', '']) { | ||
| const r = spawnSync('python3', [TRON_PWA, 'install', bad, '--dry-run'], { encoding: 'utf8', env: env() }); | ||
| expect(r.status).not.toBe(0); | ||
| expect(r.stdout).toBe(''); | ||
| } | ||
| }); | ||
|
|
||
| it('ignores a PWAMART_URL that is not https (or loopback http)', () => { | ||
| for (const bad of ['http://169.254.169.254', 'file:///etc', 'https://user:pw@store.example']) { | ||
| const r = spawnSync('python3', [TRON_PWA, 'install', 'https://app.example/', '--dry-run'], { encoding: 'utf8', env: env({ PWAMART_URL: bad }) }); | ||
| expect(r.stderr).toContain('ignoring PWAMART_URL'); | ||
| expect(r.stdout.trim()).toBe(`${CLI} --app=https://app.example/`); | ||
| } | ||
| }); | ||
|
|
||
| it('looks a slug up on pwamart and launches its start_url', async () => { | ||
| const seen: string[] = []; | ||
| const server = createServer((req, res) => { | ||
| seen.push(`${req.method} ${req.url}`); | ||
| res.setHeader('content-type', 'application/json'); | ||
| if (req.url === '/api/v1/apps/notes') res.end(JSON.stringify({ app: { slug: 'notes', url: 'https://notes.example/', start_url: 'https://notes.example/?pwa' } })); | ||
| else if (req.url === '/api/v1/apps/evil') res.end(JSON.stringify({ app: { slug: 'evil', url: 'http://evil.example/', start_url: 'http://evil.example/' } })); | ||
| else res.writeHead(404).end('{}'); | ||
| }); | ||
| await new Promise<void>((r) => server.listen(0, '127.0.0.1', r)); | ||
| const port = (server.address() as { port: number }).port; | ||
| try { | ||
| const ok = await runAsync(['install', 'notes', '--dry-run'], { PWAMART_URL: `http://127.0.0.1:${port}` }); | ||
| expect(ok.status).toBe(0); | ||
| expect(ok.stdout.trim()).toBe(`${CLI} --app=https://notes.example/?pwa`); | ||
| const evil = await runAsync(['install', 'evil', '--dry-run'], { PWAMART_URL: `http://127.0.0.1:${port}` }); | ||
| expect(evil.status).not.toBe(0); | ||
| const missing = await runAsync(['install', 'nope', '--dry-run'], { PWAMART_URL: `http://127.0.0.1:${port}` }); | ||
| expect(missing.status).not.toBe(0); | ||
| // A dry run never counts an install. | ||
| expect(seen.some((s) => s.startsWith('POST'))).toBe(false); | ||
| } finally { | ||
| server.close(); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.