Skip to content

The agent that grows with you

test(desktop): cover registered file fallback routing

Michael Nguyen631148755f58
diff --git a/apps/desktop/electron/gateway-file-download-transport.test.ts b/apps/desktop/electron/gateway-file-download-transport.test.ts
index 8ade7ec2a6b..fcb32a3ea0e 100644
--- a/apps/desktop/electron/gateway-file-download-transport.test.ts
+++ b/apps/desktop/electron/gateway-file-download-transport.test.ts
@@ -55,17 +55,3 @@ test('finalizeGatewayDownload prompts a save dialog then streams the response',
assert.match(fn, /error\.statusCode = statusCode/)
})
-test('saveGatewayFile falls back to the data-url route only on 404', () => {
- const fn = extract('async function saveGatewayFile', '\nasync function saveGatewayFileViaDataUrl')
-
- assert.match(fn, /\/api\/fs\/download\?path=/)
- assert.match(fn, /isNotFoundError\(error\)/)
- assert.match(fn, /saveGatewayFileViaDataUrl\(/)
-})
-
-test('data-url fallback reads the capped route and decodes it', () => {
- const fn = extract('async function saveGatewayFileViaDataUrl', '// Mint a single-use WS ticket')
-
- assert.match(fn, /\/api\/fs\/read-data-url\?path=/)
- assert.match(fn, /parseDataUrlToBuffer\(/)
-})
diff --git a/apps/desktop/electron/gateway-file-download.test.ts b/apps/desktop/electron/gateway-file-download.test.ts
index d67c763991f..fcadd46e8be 100644
--- a/apps/desktop/electron/gateway-file-download.test.ts
+++ b/apps/desktop/electron/gateway-file-download.test.ts
@@ -3,9 +3,11 @@ import { EventEmitter } from 'node:events'
import { test } from 'vitest'
+import { pathForRegistryBackendRequest } from './connection-config'
import {
filenameFromContentDisposition,
gatewayFilePath,
+ gatewayFileRequestPaths,
isNotFoundError,
parseDataUrlToBuffer,
pumpStreamToFile,
@@ -176,6 +178,17 @@ test('gatewayFilePath normalizes bare paths and file:// URLs', () => {
assert.equal(gatewayFilePath(null), '')
})
+test('gatewayFileRequestPaths keeps streaming and fallback requests on the same registered backend', () => {
+ const paths = gatewayFileRequestPaths('/srv/output/image one.png', requestPath =>
+ pathForRegistryBackendRequest(requestPath, 'research', { sharedRemote: true })
+ )
+
+ assert.deepEqual(paths, {
+ dataUrl: '/api/fs/read-data-url?path=%2Fsrv%2Foutput%2Fimage+one.png&profile=research',
+ download: '/api/fs/download?path=%2Fsrv%2Foutput%2Fimage+one.png&profile=research'
+ })
+})
+
test('isNotFoundError matches only HTTP 404', () => {
const notFound: any = new Error('404: missing')
diff --git a/apps/desktop/electron/gateway-file-download.ts b/apps/desktop/electron/gateway-file-download.ts
index 09a362fefe7..40fa98ced33 100644
--- a/apps/desktop/electron/gateway-file-download.ts
+++ b/apps/desktop/electron/gateway-file-download.ts
@@ -45,6 +45,22 @@ export interface GatewayFileBackendRoute<T> {
connectionId: null | string
profile: null | string
}
+export interface GatewayFileRequestPaths {
+ dataUrl: string
+ download: string
+}
+
+export function gatewayFileRequestPaths(
+ filePath: string,
+ scopePath: (requestPath: string) => string
+): GatewayFileRequestPaths {
+ const encodedPath = encodeURIComponent(filePath)
+
+ return {
+ dataUrl: scopePath(`/api/fs/read-data-url?path=${encodedPath}`),
+ download: scopePath(`/api/fs/download?path=${encodedPath}`)
+ }
+}
/**
* Resolve the backend that owns a renderer-requested gateway file. Registered
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index bf96d23515c..e0ba0815bf0 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -164,6 +164,7 @@ import { registerFsIpc } from './fs-ipc'
import {
filenameFromContentDisposition,
gatewayFilePath,
+ gatewayFileRequestPaths,
isNotFoundError,
parseDataUrlToBuffer,
pumpStreamToFile,
@@ -7571,14 +7572,11 @@ async function saveGatewayFile(payload: GatewayFileSavePayload = {}) {
const fallbackName = path.basename(filePath) || suggested || 'download'
const ctx = { suggested, fallbackName }
- const requestPath = gatewayFileRequestPath(
- connection,
- connectionId,
- profile,
- `/api/fs/download?path=${encodeURIComponent(filePath)}`
+ const requestPaths = gatewayFileRequestPaths(filePath, requestPath =>
+ gatewayFileRequestPath(connection, connectionId, profile, requestPath)
)
- const url = `${connection.baseUrl}${requestPath}`
+ const url = `${connection.baseUrl}${requestPaths.download}`
try {
const auth = await gatedFileAuth(connection)
@@ -7597,7 +7595,7 @@ async function saveGatewayFile(payload: GatewayFileSavePayload = {}) {
// /api/fs/download 404s here; fall back (ONLY on 404) to the older capped
// data-URL route so downloads keep working against older backends.
if (isNotFoundError(error)) {
- return await saveGatewayFileViaDataUrl(connection, connectionId, profile, filePath, ctx)
+ return await saveGatewayFileViaDataUrl(connection, requestPaths.dataUrl, ctx)
}
throw error
@@ -7610,18 +7608,9 @@ async function saveGatewayFile(payload: GatewayFileSavePayload = {}) {
// working until they gain the streaming route.
async function saveGatewayFileViaDataUrl(
connection: GatewayFileConnection,
- connectionId: null | string,
- profile: null | string,
- filePath: string,
+ requestPath: string,
ctx: GatewayFileSaveContext
) {
- const requestPath = gatewayFileRequestPath(
- connection,
- connectionId,
- profile,
- `/api/fs/read-data-url?path=${encodeURIComponent(filePath)}`
- )
-
const url = `${connection.baseUrl}${requestPath}`
const auth = await gatedFileAuth(connection)
let json: unknown