diff --git a/frontend/src/components/DeviceApps.tsx b/frontend/src/components/DeviceApps.tsx index a39b651a6..c969a276b 100644 --- a/frontend/src/components/DeviceApps.tsx +++ b/frontend/src/components/DeviceApps.tsx @@ -51,7 +51,7 @@ import { Icon } from './Icon' export const DeviceApps: React.FC<{ device: IDevice }> = ({ device }) => { const { t } = useTranslation() const history = useHistory() - const deviceApps = useDeviceApps(device.id) + const deviceApps = useDeviceApps(device.id, { accountId: device.accountId }) const { apps, available, switching } = deviceApps const [refusal, setRefusal] = useState() const { flip, consent } = useAppSwitch( diff --git a/frontend/src/helpers/appsHelper.test.ts b/frontend/src/helpers/appsHelper.test.ts index 1b8d7ab47..d7f9c4537 100644 --- a/frontend/src/helpers/appsHelper.test.ts +++ b/frontend/src/helpers/appsHelper.test.ts @@ -1,6 +1,7 @@ import { describe, it, expect } from 'vitest' import type { TFunction } from 'i18next' import { + addAsks, appHost, appIcon, capabilitiesLine, @@ -133,7 +134,7 @@ describe('an app’s icon and service, from what the API says — never its id', it('an app of several services: any of them is the app’s, and which of its services it is', () => { const stream: DeviceAppService = { - key: 'rtsp', name: 'Stream', protocol: 'tcp', multiPort: true, port: null, portSetting: null, listeners: [], serviceId: 'S6', ports: ['8554', '5004-5005'], + key: 'rtsp', name: 'Stream', protocol: 'tcp', multiPort: true, port: null, portSetting: null, listeners: [], serviceId: 'S6', } const camera = withServices(app('acme.camera', 'S5')) const both = { ...camera, services: [...camera.services, stream] } @@ -147,3 +148,51 @@ describe('an app’s icon and service, from what the API says — never its id', expect(appServiceOf(disabled, { id: 'S5', host: 'remoteit-app-acme-other' })).toBeUndefined() }) }) + +describe('addAsks: what adding an app gives, and so asks', () => { + const release = (version: string, more: any = {}) => ({ version, root: false, capabilities: [], withdrawn: null, ...more }) + const app = (more: any = {}) => + ({ + root: false, + capabilities: [], + accountChannel: null, + channels: [ + { channel: 'stable', current: '1.0.0', candidate: null, share: 0 }, + { channel: 'beta', current: '1.1.0', candidate: '1.2.0', share: 10 }, + ], + releases: [ + release('1.2.0', { root: true }), + release('1.1.0', { capabilities: ['ssh-exec'] }), + release('1.0.0', { capabilities: ['person-accounts'] }), + ], + ...more, + } as any) + + it('the account’s channel, else stable: its current, and its candidate where it has a share', () => { + expect(addAsks(app(), '')).toEqual({ versions: ['1.0.0'], root: false, capabilities: ['person-accounts'] }) + expect(addAsks(app({ accountChannel: 'beta' }), '')).toEqual({ + versions: ['1.2.0', '1.1.0'], + root: true, + capabilities: ['ssh-exec'], + }) + // The channel chosen over the account's. + expect(addAsks(app({ accountChannel: 'beta' }), 'stable').versions).toEqual(['1.0.0']) + // A candidate at no share goes to no device. + const none = app().channels.map((c: any) => ({ ...c, share: 0 })) + expect(addAsks(app({ channels: none }), 'beta')).toEqual({ versions: ['1.1.0'], root: false, capabilities: ['ssh-exec'] }) + }) + + it('a pin over the channel; a channel naming none gives stable’s; a withdrawn release none', () => { + expect(addAsks(app(), 'beta', '1.0.0').versions).toEqual(['1.0.0']) + const bare = [app().channels[0], { channel: 'beta', current: null, candidate: null, share: 0 }] + expect(addAsks(app({ channels: bare }), 'beta').versions).toEqual(['1.0.0']) + // Beta's current withdrawn, its candidate rolling out: the candidate alone is given. + const withdrawn = app({ releases: [release('1.2.0'), release('1.1.0', { withdrawn: '2026-11-01' })] }) + expect(addAsks(withdrawn, 'beta').versions).toEqual(['1.2.0']) + }) + + it('a release not among those read asks what the app’s own fields say', () => { + const own = app({ root: true, capabilities: ['person-certificates'], releases: [] }) + expect(addAsks(own, 'beta')).toEqual({ versions: ['1.2.0', '1.1.0'], root: true, capabilities: ['person-certificates'] }) + }) +}) diff --git a/frontend/src/helpers/appsHelper.ts b/frontend/src/helpers/appsHelper.ts index 8fd8e5159..fde709adc 100644 --- a/frontend/src/helpers/appsHelper.ts +++ b/frontend/src/helpers/appsHelper.ts @@ -8,6 +8,7 @@ import type { } from '../services/graphQLDeviceApps' import { needsNewer } from '../services/deviceAppsShared' import type { SettingLock } from '../services/productPolicy' +import type { CatalogApp } from '../services/graphQLAppCatalog' import { timeOptions } from '../components/Timestamp' /* The words the Apps pages share (presence-server docs/device-apps-design.md, "The Apps pages"): who publishes an app, @@ -137,6 +138,32 @@ export function releaseChannelName( : t('apps.channelCandidate', '{{channel}} candidate', { channel: channelName(t, channel) }) } +/* What adding an app gives the devices, and so what it asks (graphql's releaseFor and channelVersion): the release + pinned; else what the channel chosen gives — the account's where none is, else stable —: its candidate to the + devices in its share, its current to the rest, and a channel naming none stable's. A withdrawn release is given to + none. versions: those releases; root and capabilities: all they ask together, each release's as the catalogue lists + it — one not among the releases read (the newest only) as the app's own says, graphql refusing an add whose consent + does not cover what its release asks. */ +export type AddAsks = { versions: string[]; root: boolean; capabilities: string[] } +export function addAsks(app: CatalogApp, channel: string | null | undefined, pin?: string | null): AddAsks { + const gives = (name: string): string[] => { + const row = app.channels.find(c => c.channel === name) + const candidate = row?.candidate && row.share > 0 ? [row.candidate] : [] + if (row?.current) return [...candidate, row.current] + return name === 'stable' ? candidate : [...candidate, ...gives('stable')] + } + const versions = pin ? [pin] : [...new Set(gives(channel || app.accountChannel || 'stable'))] + const given = versions + .map(version => ({ version, release: app.releases.find(release => release.version === version) })) + .filter(({ release }) => !release?.withdrawn) + const asks = given.length ? given.map(({ release }) => release ?? app) : [app] + return { + versions: given.map(({ version }) => version), + root: asks.some(release => release.root), + capabilities: [...new Set(asks.flatMap(release => release.capabilities))], + } +} + /* What the machine's administrator refuses of an app here (DeviceApp.adminControl), as graphql refuses it: under local, enabling, disabling and removing it (its channel and pin are taken); under off, enabling it (an add too). */ export type AppAdminAction = 'enable' | 'disable' | 'remove' diff --git a/frontend/src/hooks/useDeviceApps.test.tsx b/frontend/src/hooks/useDeviceApps.test.tsx index 5916b69d2..975801898 100644 --- a/frontend/src/hooks/useDeviceApps.test.tsx +++ b/frontend/src/hooks/useDeviceApps.test.tsx @@ -2,7 +2,8 @@ import React, { act } from 'react' import { createRoot, Root } from 'react-dom/client' import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' -const { read, setApp, addApp, setRelease, removeApp, setSetting, available } = vi.hoisted(() => ({ +const { read, setApp, addApp, setRelease, removeApp, setSetting, available, fetchSingleFull } = vi.hoisted(() => ({ + fetchSingleFull: vi.fn(), available: vi.fn(), read: vi.fn(), setApp: vi.fn(), @@ -21,6 +22,8 @@ vi.mock('../services/graphQLDeviceApps', () => ({ graphQLSetDeviceAppSetting: setSetting, refused: (r: any) => !!r && typeof r === 'object' && 'reason' in r, })) +// The device in the store, read again where an app's services may have moved. +vi.mock('react-redux', () => ({ useDispatch: () => ({ devices: { fetchSingleFull } }) })) import { DeviceApps, ServiceApp, useDeviceApps, useServiceApp } from './useDeviceApps' import { withServices } from '../test/deviceApps' @@ -61,12 +64,12 @@ const app = (state: string, enabled?: boolean) => withServices(rawApp(state, ena let root: Root const seen: Record = {} const Apps = ({ deviceId, name, offers }: { deviceId?: string; name: string; offers?: boolean }) => { - seen[name] = useDeviceApps(deviceId, offers === undefined ? undefined : { offers }) + seen[name] = useDeviceApps(deviceId, offers === undefined ? { accountId: 'A1' } : { offers, accountId: 'A1' }) return null } const served: Record = {} const Serving = ({ service, name }: { service: Partial; name: string }) => { - served[name] = useServiceApp({ id: 'D1' } as IDevice, service as IService) + served[name] = useServiceApp({ id: 'D1', accountId: 'A1' } as IDevice, service as IService) return null } @@ -76,6 +79,7 @@ async function render(children: React.ReactNode) { } beforeEach(() => { + fetchSingleFull.mockReset().mockResolvedValue(undefined) read.mockReset().mockResolvedValue([app('disabled')]) available.mockReset().mockResolvedValue([]) setApp.mockReset().mockResolvedValue(app('running')) @@ -137,6 +141,9 @@ describe('useDeviceApps', () => { expect(seen.a.apps?.[0].state).toBe('running') expect(seen.b.apps?.[0].state).toBe('running') expect(seen.a.switching).toBeUndefined() + // Its service added: the device is read again, once, under its account. + expect(fetchSingleFull).toHaveBeenCalledTimes(1) + expect(fetchSingleFull).toHaveBeenCalledWith({ id: 'D1', accountId: 'A1', hidden: true, silent: true }) }) it('passes the person’s consent to a root app’s full control', async () => { @@ -179,6 +186,7 @@ describe('useDeviceApps', () => { }) expect(done).toEqual({ reason: 'Managed by its product Kiosks' }) expect(read).toHaveBeenCalledTimes(1) + expect(fetchSingleFull).not.toHaveBeenCalled() expect(seen.a.switching).toBeUndefined() }) @@ -231,22 +239,38 @@ describe('useDeviceApps', () => { expect(setSetting).toHaveBeenCalledWith('D1', 'remoteit.console', 'ai_commands', false) expect(seen.a.apps?.[0].settings[0].value).toBe(false) expect(seen.b.apps?.[0].settings[0].value).toBe(false) - // Not a port: the apps are not read again. + // Not a port: nothing read again. expect(read).toHaveBeenCalledTimes(1) + expect(fetchSingleFull).not.toHaveBeenCalled() }) - it('a port setting — a service’s or a listener’s — reads the apps again: their services’ ports moved with it', async () => { + it('a port setting — a service’s or a listener’s — reads the device again: its service’s port moved with it', async () => { const stream = { key: 'rtsp', name: 'Stream', protocol: 'udp', multiPort: true, port: null, portSetting: null, serviceId: 'S8', - listeners: [{ name: 'rtsp', type: 'udp', port: null, portSetting: 'rtsp_port', ports: null }], ports: ['8554'] } - const camera = withServices({ ...rawApp('running'), services: undefined } as any) + listeners: [{ name: 'rtsp', type: 'udp', port: null, portSetting: 'rtsp_port', ports: null }] } + const camera = withServices({ ...rawApp('running'), portSetting: 'port', services: undefined } as any) + const port = { ...aiCommands(true), name: 'rtsp_port', type: 'int', value: 8554 } read.mockResolvedValue([{ ...camera, services: [...camera.services, stream] }]) await render() - read.mockResolvedValue([{ ...camera, services: [...camera.services, { ...stream, ports: ['9554'] }] }]) + setSetting.mockResolvedValue({ ...port, value: 9554 }) + let done: unknown + await act(async () => { + done = await seen.a.setSetting(seen.a.apps![0], 'rtsp_port', 9554) + }) + expect(done).toBe(true) + expect(fetchSingleFull).toHaveBeenCalledWith({ id: 'D1', accountId: 'A1', hidden: true, silent: true }) + // The primary's own port setting too. + fetchSingleFull.mockClear() + await act(async () => { + await seen.a.setSetting(seen.a.apps![0], 'port', 2222) + }) + expect(fetchSingleFull).toHaveBeenCalledTimes(1) + // A setting graphql refused: nothing moved, nothing read. + fetchSingleFull.mockClear() + setSetting.mockResolvedValue(null) await act(async () => { - await seen.a.setSetting(seen.a.apps![0], 'rtsp_port', 9554) + await seen.a.setSetting(seen.a.apps![0], 'rtsp_port', 22) }) - expect(read).toHaveBeenCalledTimes(2) - expect(seen.a.apps?.[0].services[1].ports).toEqual(['9554']) + expect(fetchSingleFull).not.toHaveBeenCalled() }) }) diff --git a/frontend/src/hooks/useDeviceApps.ts b/frontend/src/hooks/useDeviceApps.ts index f16a7ac50..48efee822 100644 --- a/frontend/src/hooks/useDeviceApps.ts +++ b/frontend/src/hooks/useDeviceApps.ts @@ -1,4 +1,6 @@ import { useCallback, useEffect, useState } from 'react' +import { useDispatch } from 'react-redux' +import type { Dispatch } from '../store' import { AppChange, AppRelease, @@ -55,8 +57,17 @@ const waiting = (app?: DeviceApp) => app?.state === 'starting' || app?.state === /* A device's apps (services/graphQLDeviceApps): added, enabled or disabled, its channel and pin, removed, and its settings. The device acts within seconds: what it says is waited for, up to half a minute — while it starts the app, or until it says the app is gone. What it may add is read too, unless the part asking does not offer any (offers - false). */ -export function useDeviceApps(deviceId?: string, { offers = true }: { offers?: boolean } = {}): DeviceApps { + false). accountId: the account the device is read under (IDevice.accountId). + + An app's services are its device's services, and the device's services (its IDevice in the store) are where every + part of the portal finds them: their rows, connect panels, Open and Open terminal, and the app's page. A change that + went through, or a setting that is a port, may add, remove or move them, and nothing else says so while the service + stays online: the device is read again after one. */ +export function useDeviceApps( + deviceId?: string, + { offers = true, accountId }: { offers?: boolean; accountId?: string } = {} +): DeviceApps { + const dispatch = useDispatch() const [apps, setApps] = useState() const [switching, setSwitching] = useState() const [available, setAvailable] = useState() @@ -87,7 +98,11 @@ export function useDeviceApps(deviceId?: string, { offers = true }: { offers?: b } }, [deviceId]) - // A change, then what the device says of it, every part of the page told. + const readDevice = useCallback(async () => { + if (deviceId) await dispatch.devices.fetchSingleFull({ id: deviceId, accountId, hidden: true, silent: true }) + }, [dispatch, deviceId, accountId]) + + // A change, then what the device says of it, every part of the page told; then the device, its services as they are. const change = useCallback( async (app: DeviceApp, call: () => Promise>): Promise> => { if (!deviceId) return { reason: '' } @@ -100,11 +115,12 @@ export function useDeviceApps(deviceId?: string, { offers = true }: { offers?: b if (!waiting(now?.find(a => a.id === app.id))) break await new Promise(resolve => setTimeout(resolve, 2_000)) } + await readDevice() } setSwitching(undefined) return refused(answer) ? answer : true }, - [deviceId] + [deviceId, readDevice] ) const turn = useCallback( @@ -136,14 +152,11 @@ export function useDeviceApps(deviceId?: string, { offers = true }: { offers?: b a.id === app.id ? { ...a, settings: a.settings.map(s => (s.name === name ? setting : s)) } : a ) tell(deviceId, now) - // A port setting moves its service's ports, which graphql keeps in step: read them as it now has them. - if (isPortSetting(app, name)) { - const read = await graphQLDeviceApps(deviceId) - if (read) tell(deviceId, read) - } + // A port setting moves its service's port, which graphql keeps in step before it answers. + if (isPortSetting(app, name)) await readDevice() return true }, - [deviceId, apps] + [deviceId, apps, readDevice] ) return { @@ -167,7 +180,10 @@ const isPortSetting = (app: DeviceApp, name: string) => services ask nothing. */ export type ServiceApp = DeviceApps & { app?: DeviceApp; appService?: DeviceAppService } export function useServiceApp(device?: IDevice, service?: IService): ServiceApp { - const deviceApps = useDeviceApps(isAppService(service) ? device?.id : undefined, { offers: false }) + const deviceApps = useDeviceApps(isAppService(service) ? device?.id : undefined, { + offers: false, + accountId: device?.accountId, + }) const app = serviceApp(deviceApps.apps, service) return { ...deviceApps, app, appService: appServiceOf(app, service) } } diff --git a/frontend/src/pages/AppAddDialog.test.tsx b/frontend/src/pages/AppAddDialog.test.tsx index 94f2504d0..027cf90c1 100644 --- a/frontend/src/pages/AppAddDialog.test.tsx +++ b/frontend/src/pages/AppAddDialog.test.tsx @@ -187,9 +187,16 @@ describe('Add, to devices', () => { }) }) +// The release the account's channel gives (stable's current, 1.4.0) asking what is given, as the catalogue lists it and +// the app's own fields say it. +const accountRelease = (asks: { root?: boolean; capabilities?: string[] }) => { + const app = mockCatalogApp('acme.backup')! + return { ...app, ...asks, releases: app.releases.map(r => (r.version === '1.4.0' ? { ...r, ...asks } : r)) } +} + describe('the root consent', () => { it('a root app: Add only once the yes is ticked, in words, and consentRoot passed', async () => { - catalog.mockResolvedValue({ ...mockCatalogApp('acme.backup'), root: true }) + catalog.mockResolvedValue(accountRelease({ root: true })) await render() await tick('D1') expect($('[data-app-add-root]').textContent).toContain('Backup runs as root: full control of each device.') @@ -212,12 +219,9 @@ describe('the root consent', () => { }) it('what it asks of remote.it is part of the consent — the release pinned’s, where one is', async () => { - const asks = ['person-accounts', 'person-certificates'] - const app = mockCatalogApp('acme.backup')! + const app = accountRelease({ root: true, capabilities: ['person-accounts', 'person-certificates'] }) catalog.mockResolvedValue({ ...app, - root: true, - capabilities: asks, releases: [{ ...app.releases[0], root: true, capabilities: ['ssh-exec'] }, ...app.releases.slice(1)], }) await render() @@ -233,13 +237,69 @@ describe('the root consent', () => { }) it('a restricted app says what it asks of remote.it above Add, with no consent asked', async () => { - catalog.mockResolvedValue({ ...mockCatalogApp('acme.backup'), capabilities: ['person-certificates'] }) + catalog.mockResolvedValue(accountRelease({ capabilities: ['person-certificates'] })) await render() expect($('[data-app-add-root]')).toBeNull() expect($('[data-app-add-capabilities]').textContent).toBe( 'It asks of remote.it:· Signs in people by remote.it certificate' ) }) + + it('a channel chosen: what the releases it gives ask — its current’s, and its candidate’s for the devices in its share', async () => { + const app = mockCatalogApp('acme.backup')! + const beta = { ...app.releases[0], capabilities: ['ssh-exec'] } + catalog.mockResolvedValue({ ...app, releases: [beta, ...app.releases.slice(1)] }) + await render() + await tick('D1') + expect($('[data-app-add-capabilities]')).toBeNull() + await select($('[data-app-add-channel]'), 'beta') + expect($('[data-app-add-capabilities]').textContent).toBe('It asks of remote.it:· AI commands go through this app') + expect($('[data-app-add-root]')).toBeNull() + // Its candidate (1.5.0-rc.1, to 10% of its devices) runs as root: the yes is asked, for beta's releases. + act(() => root.unmount()) + container.remove() + const candidate = { ...beta, version: '1.5.0-rc.1', root: true, capabilities: ['person-accounts'], channels: ['beta candidate'] } + catalog.mockResolvedValue({ ...app, releases: [candidate, beta, ...app.releases.slice(1)] }) + await render() + await tick('D1') + expect($('[data-app-add-root]')).toBeNull() + await select($('[data-app-add-channel]'), 'beta') + expect([...document.querySelectorAll('[data-app-add-root] [data-app-capability]')].map(c => c.textContent)).toEqual([ + '· Keeps accounts for the people you share with', + '· AI commands go through this app', + ]) + expect(submit().disabled).toBe(true) + await click($('[data-app-add-consent]')) + await click(submit()) + expect(addMany).toHaveBeenCalledWith('acme.backup', { deviceIds: ['D1'], accountId: undefined, channel: 'beta', consentRoot: true }) + }) + + it('a yes is to the releases and the target it was given for: another channel, pin or target asks it again', async () => { + const app = accountRelease({ root: true }) + catalog.mockResolvedValue({ ...app, releases: [{ ...app.releases[0], root: true }, ...app.releases.slice(1)] }) + await render() + await tick('D1') + const consent = () => $('[data-app-add-consent]') as HTMLInputElement + await click(consent()) + expect(submit().disabled).toBe(false) + // Beta gives another release: the yes was not to it. + await select($('[data-app-add-channel]'), 'beta') + expect(consent().checked).toBe(false) + expect(submit().disabled).toBe(true) + await click(consent()) + // Stable named: the same release as the account's, asked again all the same — the yes was to beta's. + await select($('[data-app-add-channel]'), 'stable') + expect(consent().checked).toBe(false) + await click(consent()) + await click($('[data-app-add-pin]')) + await select($('[data-app-add-version]'), '1.5.0-beta.2') + expect(consent().checked).toBe(false) + await click(consent()) + // A product: the yes is for every device in it, now and later — not the one given for these devices. + await click($('[data-app-add-to="product"] input')) + await select($('[data-app-add-product-select]'), 'P1') + expect(consent().checked).toBe(false) + }) }) describe('Add, to a tag or a product', () => { @@ -280,7 +340,7 @@ describe('Add, to a tag or a product', () => { }) it('a product: set Enabled on it (state enabled), its consent worded for every device in it, now and later', async () => { - catalog.mockResolvedValue({ ...mockCatalogApp('acme.backup'), root: true }) + catalog.mockResolvedValue(accountRelease({ root: true })) await render() await click($('[data-app-add-to="product"] input')) await select($('[data-app-add-product-select]'), 'P1') diff --git a/frontend/src/pages/AppAddDialog.tsx b/frontend/src/pages/AppAddDialog.tsx index a4d29d144..96efaa7a2 100644 --- a/frontend/src/pages/AppAddDialog.tsx +++ b/frontend/src/pages/AppAddDialog.tsx @@ -33,7 +33,7 @@ import { AppChangeResults } from '../components/AppChangeResults' import { LoadingMessage } from '../components/LoadingMessage' import { Notice } from '../components/Notice' import { AppCapabilities } from '../components/AppRootConsent' -import { adminControlLine, channelName } from '../helpers/appsHelper' +import { addAsks, adminControlLine, channelName } from '../helpers/appsHelper' type Target = 'devices' | 'tag' | 'product' @@ -48,9 +48,11 @@ type Target = 'devices' | 'tag' | 'product' A product — its devices now and later: set Enabled on the product (setDeviceProductApp state enabled), with the product's root consent. Channel the account's, stable or beta; and a version to pin, where wanted. - A root app (or a root release pinned) asks the person's yes in words before Add is offered: full control of each - device — of every device in the product, now and later, for a product —, and what else it asks of remote.it (its - capabilities, the release's), said in the same place; a restricted app says them above Add. + What it asks is the releases the choice gives (helpers/appsHelper addAsks): the pin, else the channel's — not the + account's channel's where another is chosen. One that runs as root asks the person's yes in words before Add is + offered: full control of each device — of every device in the product, now and later, for a product —, and what + else it asks of remote.it (its capabilities), said in the same place; a restricted one says them above Add. The yes + is to those releases, for that target (and product): another choice asks it again. Devices and a tag call addDeviceApps (at most 500 devices a call: more, and it says to add it to a product); each device's result is listed after — Added · Enabled · Already there · Refused: . */ @@ -77,7 +79,7 @@ export const AppAddDialog: React.FC = () => { const [channel, setChannel] = useState('') const [pinning, setPinning] = useState(false) const [version, setVersion] = useState('') - const [consented, setConsented] = useState(false) + const [consentedTo, setConsentedTo] = useState() const [busy, setBusy] = useState(false) const [refusal, setRefusal] = useState() const [results, setResults] = useState() @@ -124,9 +126,11 @@ export const AppAddDialog: React.FC = () => { const ready = app && app !== 'ERROR' ? app : undefined const standing = ready?.releases.filter(r => !r.withdrawn) ?? [] - const pinned = pinning && version ? standing.find(r => r.version === version) : undefined - const root = pinned ? pinned.root : !!ready?.root - const capabilities = pinned ? pinned.capabilities : ready?.capabilities + const asks = ready ? addAsks(ready, channel, pinning ? version : undefined) : undefined + const root = !!asks?.root + const capabilities = asks?.capabilities + const consentFor = [target, target === 'product' ? productId : '', ...(asks?.versions ?? [])].join('/') + const consented = consentedTo === consentFor const product = products.find(p => p.id === productId) const count = target === 'devices' ? ticked.length : target === 'tag' ? tagCount ?? 0 : 0 const tooMany = target !== 'product' && count > MANY_DEVICES_MAX @@ -346,10 +350,7 @@ export const AppAddDialog: React.FC = () => { variant="standard" label={t('apps.pin', 'Pin')} value={version} - onChange={e => { - setVersion(e.target.value) - setConsented(false) - }} + onChange={e => setVersion(e.target.value)} SelectProps={{ native: true }} InputLabelProps={{ shrink: true }} inputProps={{ 'data-app-add-version': true }} @@ -390,7 +391,7 @@ export const AppAddDialog: React.FC = () => { setConsented(e.target.checked)} + onChange={e => setConsentedTo(e.target.checked ? consentFor : undefined)} inputProps={{ 'data-app-add-consent': true } as any} /> } diff --git a/frontend/src/pages/DeviceAppCatalogPage.tsx b/frontend/src/pages/DeviceAppCatalogPage.tsx index 4b2f79af9..964c5f3b5 100644 --- a/frontend/src/pages/DeviceAppCatalogPage.tsx +++ b/frontend/src/pages/DeviceAppCatalogPage.tsx @@ -29,7 +29,7 @@ export const DeviceAppCatalogPage: React.FC = () => { const { t } = useTranslation() const history = useHistory() const { device } = useContext(DeviceContext) - const deviceApps = useDeviceApps(device?.id) + const deviceApps = useDeviceApps(device?.id, { accountId: device?.accountId }) const { apps, available, switching } = deviceApps const [refusal, setRefusal] = useState() const { flip, consent } = useAppSwitch( diff --git a/frontend/src/pages/DeviceAppPage.test.tsx b/frontend/src/pages/DeviceAppPage.test.tsx index 9c398d815..3c26811d0 100644 --- a/frontend/src/pages/DeviceAppPage.test.tsx +++ b/frontend/src/pages/DeviceAppPage.test.tsx @@ -121,7 +121,10 @@ const device = (permissions = ['VIEW', 'CONNECT', 'MANAGE', 'SCRIPTING']) => name: 'pi-kitchen', accountId: 'A1', permissions, - services: [{ id: 'S7', name: 'Backup', port: 30001 }], + services: [ + { id: 'S7', name: 'Backup', port: 30001 }, + { id: 'S8', name: 'Stream', port: 8554, ports: ['8554', '5004-5005'] }, + ], } as unknown as IDevice) const turn = vi.fn() @@ -458,12 +461,12 @@ describe('its service, settings, root, delivery and problems', () => { settings: [...backup().settings, setting('web_port', 'Web port'), setting('rtsp_port', 'RTSP port')], services: [ { key: null, name: 'Camera', protocol: 'http', multiPort: false, port: null, portSetting: 'web_port', - listeners: [{ name: 'web', type: 'tcp', port: null, portSetting: null, ports: null }], serviceId: 'S7', ports: ['30001'] }, + listeners: [{ name: 'web', type: 'tcp', port: null, portSetting: null, ports: null }], serviceId: 'S7' }, { key: 'rtsp', name: 'Stream', protocol: 'udp', multiPort: true, port: null, portSetting: null, listeners: [ { name: 'rtsp', type: 'udp', port: null, portSetting: 'rtsp_port', ports: null }, { name: 'rtp', type: 'udp', port: null, portSetting: null, ports: '5004-5005' }, - ], serviceId: 'S8', ports: ['8554', '5004-5005'] }, + ], serviceId: 'S8' }, ], }) apps.value.apps = [camera] @@ -488,7 +491,7 @@ describe('its service, settings, root, delivery and problems', () => { act(() => root.unmount()) container.remove() // Disabled, its services stopped: its port settings are under Settings, by their own labels. - apps.value.apps = [{ ...camera, enabled: false, state: 'disabled', serviceId: null, services: camera.services.map(s => ({ ...s, serviceId: null, ports: [] })) }] + apps.value.apps = [{ ...camera, enabled: false, state: 'disabled', serviceId: null, services: camera.services.map(s => ({ ...s, serviceId: null })) }] const stopped = await render() expect(stopped.querySelectorAll('[data-app-service]').length).toBe(0) expect(row(stopped, 'settings')?.textContent).toContain('Web port') diff --git a/frontend/src/pages/DeviceAppPage.tsx b/frontend/src/pages/DeviceAppPage.tsx index 43cce8641..eb695e9f9 100644 --- a/frontend/src/pages/DeviceAppPage.tsx +++ b/frontend/src/pages/DeviceAppPage.tsx @@ -75,7 +75,7 @@ export const DeviceAppPage: React.FC = () => { const history = useHistory() const { appID } = useParams<{ appID: string }>() const { device } = useContext(DeviceContext) - const deviceApps = useDeviceApps(device?.id) + const deviceApps = useDeviceApps(device?.id, { accountId: device?.accountId }) const { apps, switching, setRelease, remove, setSetting } = deviceApps const [refusal, setRefusal] = useState() const [saving, setSaving] = useState() @@ -311,8 +311,9 @@ export const DeviceAppPage: React.FC = () => { )} {shown.map((served, index) => { const service = device.services?.find(s => s.id === served.serviceId) - // Its ports as graphql has them (a port setting's change re-reads them), else as the device's service says. - const list = served.ports.length ? served.ports : service?.ports ?? (service ? [String(service.port)] : []) + // Its ports as the device's service has them, as every connection to it reads them (a port setting's change + // reads the device again: hooks/useDeviceApps). + const list = service?.ports ?? (service ? [String(service.port)] : []) const several = served.multiPort || list.length > 1 || !!list[0]?.includes('-') const own = [...ports].filter(([, port]) => port.service === served) return ( diff --git a/frontend/src/services/graphQLDeviceApps.test.ts b/frontend/src/services/graphQLDeviceApps.test.ts index b1fe8debc..3bb8924ba 100644 --- a/frontend/src/services/graphQLDeviceApps.test.ts +++ b/frontend/src/services/graphQLDeviceApps.test.ts @@ -87,10 +87,10 @@ describe('Device.apps', () => { // A session as the app reports it: never a person. expect(query).toContain('sessions { session login via since }') expect(query).not.toMatch(/\bperson\b/) - // Its services, the primary first, each with its listeners and its ports on the device; where its named listeners + // Its services, the primary first, each with its listeners and its service on the device; where its named listeners // listen. An app's own port is never asked: a service's is. const services = - 'services { key name protocol multiPort port portSetting listeners { name type port portSetting ports } serviceId ports }' + 'services { key name protocol multiPort port portSetting listeners { name type port portSetting ports } serviceId }' expect(query).toContain(`${services} listeners { name listen }`) expect(query.replace(services, '')).not.toMatch(/\bport\b|\bon\b/) expect(query).toContain('heldBack { reason detail }') @@ -370,12 +370,12 @@ describe('build step 5: what a device may add, and a remote.it too old', () => { expect(await graphQLAvailableApps('D1')).toEqual([]) }) - it('its services, the primary first: each its listeners and, while served, its service on the device and its ports', async () => { + it('its services, the primary first: each its listeners and, while served, its service on the device', async () => { const services = [ { key: null, name: 'Camera', protocol: 'http', multiPort: false, port: null, portSetting: 'web_port', - listeners: [{ name: 'web', type: 'tcp', port: null, portSetting: 'web_port', ports: null }], serviceId: 'S1', ports: ['8080'] }, + listeners: [{ name: 'web', type: 'tcp', port: null, portSetting: 'web_port', ports: null }], serviceId: 'S1' }, { key: 'rtsp', name: 'Stream', protocol: 'tcp', multiPort: true, - listeners: [{ name: 'rtsp', type: 'tcp', port: 8554 }, { name: 'rtp', type: 'udp', ports: '5004-5005' }], serviceId: 'S2', ports: ['8554', '5004-5005'] }, + listeners: [{ name: 'rtsp', type: 'tcp', port: 8554 }, { name: 'rtp', type: 'udp', ports: '5004-5005' }], serviceId: 'S2' }, ] post.mockResolvedValue(answer({ login: { device: [{ apps: [{ ...consoleApp, services, listeners: [{ name: 'web', listen: 'tcp:8080' }] }] }] } })) const [app] = (await graphQLDeviceApps('D1'))! diff --git a/frontend/src/services/graphQLDeviceApps.ts b/frontend/src/services/graphQLDeviceApps.ts index 7bf420a3e..d7d51a1e1 100644 --- a/frontend/src/services/graphQLDeviceApps.ts +++ b/frontend/src/services/graphQLDeviceApps.ts @@ -61,8 +61,9 @@ export type AppListener = { /* One of an app's services (graphql's DeviceAppService), the primary first — the app's single-service fields (serviceId, protocol, portSetting) are the primary's. key: none for the primary, else its first listener's name. A multi-port service's ports are its listeners' own, in their order, the first its launch port; one of one port has a - port (or portSetting, the owner's). serviceId and ports: the service on the device and its ports and ranges there, - while the app is enabled and the service served. */ + port (or portSetting, the owner's). serviceId: the service on the device, while the app is enabled and the service + served — where it listens is that service's (IDevice.services: its port and ports), as every connection to it reads + it. */ export type DeviceAppService = { key: string | null name: string @@ -72,7 +73,6 @@ export type DeviceAppService = { portSetting: string | null listeners: AppListener[] serviceId: string | null - ports: string[] } // Where one of an app's named listeners listens, as the device reports it while the app runs: tcp:, @@ -206,10 +206,11 @@ const SETTING_FIELDS = 'name type label help default value at by onDevice control locked { product level } deprecated choices { value label } declaration' // The lifecycle's fields (enabled, added, latest, upToDate, heldBack, grantUntil) as the design pins them. const SESSION_FIELDS = 'session login via since' -// An app's services as its release declares them (AppService), and as each is on the device (DeviceAppService). +// An app's services as its release declares them (AppService), and which of the device's services each is +// (DeviceAppService). const DECLARED_SERVICE_FIELDS = 'key name protocol multiPort port portSetting listeners { name type port portSetting ports }' -const SERVICE_FIELDS = `${DECLARED_SERVICE_FIELDS} serviceId ports` +const SERVICE_FIELDS = `${DECLARED_SERVICE_FIELDS} serviceId` const FIELDS = `id name publisher publisherVerifiedDomain oldInstall conversion conversionDetail root enabled added channel pinned version release latest upToDate heldBack { reason detail } grantUntil adminControl state detail serviceId contract protocol capabilities hostKey sessions { ${SESSION_FIELDS} } sessionsOpen features portSetting services { ${SERVICE_FIELDS} } listeners { name listen } problems { name error detail } settings { ${SETTING_FIELDS} }` // A setting as read: its value the declared default where none was set, and its lock only where it names a product. @@ -239,7 +240,6 @@ const parsedService = (service: any): DeviceAppService => ({ ports: listener.ports ?? null, })), serviceId: service.serviceId ?? null, - ports: service.ports ?? [], }) export const parsedDeviceApp = (app: any): DeviceApp => ({ diff --git a/frontend/src/test/deviceApps.ts b/frontend/src/test/deviceApps.ts index b94102ac5..291d73810 100644 --- a/frontend/src/test/deviceApps.ts +++ b/frontend/src/test/deviceApps.ts @@ -14,7 +14,6 @@ export function withServices(app: Omit & { services?: Dev portSetting: app.portSetting ?? null, listeners: [], serviceId: app.serviceId, - ports: [], } return { ...app, services: serves ? [primary] : [] } }