Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"bench:gate": "bun apps/bench/src/gate/main.ts",
"test": "bun test --parallel=4 --timeout 60000 ./apps ./packages ./plugins",
"test:upgrade": "bun test --timeout 1200000 ./plugins/host/daemon/test/upgrade/upgrade.ts",
"test:phone": "bun test --parallel --timeout 30000 ./plugins/ui/look/kit/test/gallery/gallery.ts ./plugins/ui/shell/shell/test/phone/window.ts ./plugins/ui/shell/shell/test/phone/drawers.ts ./plugins/ui/shell/shell/test/phone/keyboard.ts ./plugins/core/features/test/web/matrix.ts ./plugins/ui/chat/chat/test/web/follow.ts ./plugins/ui/chat/chat/test/web/catch-up.ts ./plugins/ui/chat/chat/test/web/draft.ts ./plugins/ui/chat/chat/test/web/early.ts ./plugins/ui/chat/chat/test/web/reply.ts ./plugins/ui/chat/chat/test/web/upload.ts ./plugins/bots/views/test/web/history.ts ./plugins/bots/views/test/web/tabs.ts ./plugins/core/server/test/web/hello.ts ./plugins/ui/shell/shell/test/web/parts.ts ./plugins/ui/chat/html/test/web/zoom.ts"
"test:phone": "bun test --parallel --timeout 30000 ./plugins/ui/look/kit/test/gallery/gallery.ts ./plugins/ui/shell/shell/test/phone/window.ts ./plugins/ui/shell/shell/test/phone/drawers.ts ./plugins/ui/shell/shell/test/phone/keyboard.ts ./plugins/core/features/test/web/matrix.ts ./plugins/ui/chat/chat/test/web/follow.ts ./plugins/ui/chat/chat/test/web/catch-up.ts ./plugins/ui/chat/chat/test/web/draft.ts ./plugins/ui/chat/chat/test/web/early.ts ./plugins/ui/chat/chat/test/web/reply.ts ./plugins/ui/chat/chat/test/web/upload.ts ./plugins/ui/chat/chat/test/web/app.ts ./plugins/bots/views/test/web/history.ts ./plugins/bots/views/test/web/tabs.ts ./plugins/core/server/test/web/hello.ts ./plugins/ui/shell/shell/test/web/parts.ts ./plugins/ui/chat/html/test/web/zoom.ts"
},
"devDependencies": {
"@sand/boot": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion plugins/agent/dev-servers/src/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default definePlugin({
const notices = toastsOf(ctx)
const card = serversCard(ctx.kit, {
servers: shownIn,
open: server => void notices.attempt(`Couldn't open localhost:${server.port}`, () => openServer(ctx.wire, server)),
open: server => void notices.attempt(`Couldn't open localhost:${server.port}`, () => openServer(ctx, server)),
stop: server => void notices.attempt(`Couldn't stop localhost:${server.port}`, () => ctx.wire.call(devServerStop, { port: server.port, session: server.session }, server.device)),
})
ctx.effect(() => ctx.inspector.row({ id: 'dev-servers', group: 'helpers', order: 70, shows: subject => !subject.draft && shownIn(subject).length > 0, render: card }))
Expand Down
33 changes: 25 additions & 8 deletions plugins/agent/dev-servers/src/web/open.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { Wire } from '@sand/devices-contract'
import { devicePaths } from '@sand/devices-contract'
import type { Pcs, Wire } from '@sand/devices-contract'
import { devServerOpen, devServerRelay } from '../contract'
import type { Shown } from './card'

interface Reach {
wire: Wire
pcs: Pcs
}

const LOOPBACK = new Set(['localhost', '127.0.0.1', '[::1]'])

const hostnameOf = (link: string) => new URL(link).hostname
Expand All @@ -23,18 +27,31 @@ const show = (tab: Window | null, link: string) => {
else window.open(link, '_blank', 'noopener')
}

const relayed = (wire: Wire, pc: string) => new URL(wire.url('/', pc)).pathname.startsWith(`${devicePaths.via}/`)
const routeTo = (pcs: Pcs, pc: string) => {
const status = pcs.get(pc)?.status
if (status?.phase !== 'connected') throw new Error(`${pcs.name(pc)} isn't connected`)
return status.route
}

const addressOf = (pcs: Pcs, pc: string) => {
const { url } = routeTo(pcs, pc)
if (!url) throw new Error(`${pcs.name(pc)} isn't connected to this page directly`)
return url
}

const reachableLinks = async (wire: Wire, server: Shown) => {
const reachableLinks = async ({ wire, pcs }: Reach, server: Shown) => {
const links = await wire.call(devServerOpen, { port: server.port, session: server.session }, server.device)
return relayed(wire, server.device) ? { links: await wire.call(devServerRelay, { pc: server.device, links }), pc: undefined } : { links, pc: server.device }
const { url, via } = routeTo(pcs, server.device)
if (url) return { links, address: url }
if (!via) throw new Error(`${pcs.name(server.device)} has no route this page can link to`)
return { links: await wire.call(devServerRelay, { pc: server.device, links }, via), address: addressOf(pcs, via) }
}

export const openServer = async (wire: Wire, server: Shown) => {
export const openServer = async (reach: Reach, server: Shown) => {
const tab = blankTab()
try {
const { links, pc } = await reachableLinks(wire, server)
const link = linkFor(links, hostnameOf(wire.url('/', pc)))
const { links, address } = await reachableLinks(reach, server)
const link = linkFor(links, hostnameOf(address))
if (!link) throw new Error("sand on that PC doesn't listen on an address this device reaches")
show(tab, link)
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions plugins/devices/pcs/src/page/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export const thisApp = lazy((): AndroidApp | undefined => {
return window.sandApp && start ? appOver(window.sandApp, start) : undefined
})

export const appProxyOrigin = () => (thisApp() ? location.origin : undefined)

const reachableUrls = (connection: PcConnection) => {
const status = connection.status()
const connected = status.phase === 'connected' ? [status.route.url] : []
Expand Down
18 changes: 9 additions & 9 deletions plugins/devices/pcs/src/wire/url.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { PcUnavailable, viaPath } from '@sand/devices-contract'
import type { Fleet } from '../fleet/fleet'
import { appProxyOrigin } from '../page/app'

const routeTo = (fleet: Fleet, pc: string) => {
const status = fleet.connection(pc).status()
if (status.phase !== 'connected') throw new PcUnavailable({ pc, name: fleet.nameOf(pc), status, reason: 'disconnected' })
return status.route
}

const directBase = (fleet: Fleet, pc: string) => {
const { url } = routeTo(fleet, pc)
if (!url) throw new Error(`${fleet.nameOf(pc)} isn't connected to this page directly`)
return url
}
const directBase = (fleet: Fleet, pc: string) => (pc === fleet.home ? appProxyOrigin() : undefined) ?? routeTo(fleet, pc).url

export const pcUrl = (fleet: Fleet, path: string, pc: string) => {
const route = routeTo(fleet, pc)
if (route.url) return new URL(path, route.url).href
if (!route.via) throw new Error(`${fleet.nameOf(pc)} has no route this page can link to`)
return new URL(viaPath(pc, path), directBase(fleet, route.via)).href
const direct = directBase(fleet, pc)
if (direct) return new URL(path, direct).href
const { via } = routeTo(fleet, pc)
if (!via) throw new Error(`${fleet.nameOf(pc)} has no route this page can link to`)
const relay = directBase(fleet, via)
if (!relay) throw new Error(`${fleet.nameOf(via)} isn't connected to this page directly`)
return new URL(viaPath(pc, path), relay).href
}
83 changes: 83 additions & 0 deletions plugins/ui/chat/chat/test/web/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { afterAll, beforeAll, expect, test } from 'bun:test'
import { listen } from '@vaakx-dev/vrui'
import { chromium, type Browser, type BrowserContext, type Page, type Route, type WebSocketRoute } from 'playwright'
import { startSandbox } from './sandbox'

const APP_ORIGIN = 'https://sand.localhost'
const LAN_ORIGIN = 'https://pc.test'

let sand: Awaited<ReturnType<typeof startSandbox>>
let pc: URL
let browser: Browser

beforeAll(async () => {
sand = await startSandbox([])
pc = new URL(sand.url)
browser = await chromium.launch()
}, 60_000)

afterAll(async () => {
await browser?.close()
await sand?.stop()
})

const onPc = (url: string) => {
const { pathname, search } = new URL(url)
return `${pc.origin}${pathname}${search}`
}

const proxyToPc = async (route: Route) => {
await route.fulfill({ response: await route.fetch({ url: onPc(route.request().url()) }) })
}

const refuseImages = async (route: Route) => (route.request().resourceType() === 'image' ? route.abort() : proxyToPc(route))

const pipeToPc = (page: WebSocketRoute) => {
const socket = new WebSocket(onPc(page.url()).replace(/^http/, 'ws'))
socket.binaryType = 'arraybuffer'
const opened = new Promise(resolve => listen(socket, 'open', resolve))
const stops = [
listen<MessageEvent>(socket, 'message', ({ data }) => page.send(typeof data === 'string' ? data : Buffer.from(data))),
listen<CloseEvent>(socket, 'close', ({ code, reason }) => page.close({ code, reason })),
]
page.onMessage(message => void opened.then(() => socket.send(typeof message === 'string' ? message : new Uint8Array(message))))
page.onClose(() => {
stops.forEach(stop => stop())
socket.close()
})
}

const androidApp = async (): Promise<BrowserContext> => {
const context = await browser.newContext({ serviceWorkers: 'block', viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true })
await context.addInitScript(routes => {
Object.assign(window, { sandApp: Object.assign(new EventTarget(), { postMessage: () => {} }), sandAppStart: { routes, push: false, versionCode: 1 } })
}, [LAN_ORIGIN])
await context.route(`${APP_ORIGIN}/**`, proxyToPc)
await context.route(`${LAN_ORIGIN}/**`, refuseImages)
await context.routeWebSocket(`${LAN_ORIGIN.replace(/^https/, 'wss')}/**`, pipeToPc)
return context
}

const pictureOutcome = async (page: Page, name: string) => {
const outcome = await page.waitForFunction(alt => {
const shown = [...document.images].some(image => image.alt === alt && image.complete && image.naturalWidth > 0)
if (shown) return 'shown'
return document.body.innerText.includes('Image unavailable') && 'unavailable'
}, name)
return outcome.jsonValue()
}

test('in the Android app, a chat picture loads through the page the app serves, since the WebView loads no pictures straight from the PC', async () => {
const context = await androidApp()
const page = await context.newPage()
await page.goto(`${APP_ORIGIN}/${pc.hash}`)
const box = page.getByRole('textbox').first()
await box.waitFor()
await page.locator('input[type=file]').first().setInputFiles({ name: 'tide.png', mimeType: 'image/png', buffer: await page.screenshot() })
await box.fill('look at this')
await page.getByRole('button', { name: 'Send' }).click()
await page.waitForURL(/view=thread/)
await page.reload()
expect(await pictureOutcome(page, 'tide.png')).toBe('shown')
await context.close()
}, 60_000)
Loading