Skip to content
Closed
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
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default [
window: 'readonly',
document: 'readonly',
navigator: 'readonly',
fetch: 'readonly',
},
},
plugins: {
Expand Down
31 changes: 24 additions & 7 deletions tests_integ/__fixtures__/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import { readFileSync } from 'node:fs'
import { join } from 'node:path'

/**
* Helper to load fixture files from Vite URL imports.
* Vite ?url imports return paths like '/tests_integ/__resources__/file.png' in test environment.
* Works in both Node.js and browser environments with a unified async API.
*
* @param url - The URL from a Vite ?url import
* @returns The file contents as a Uint8Array
* @returns Promise resolving to the file contents as a Uint8Array
*
* @remarks
* Implementation uses environment-specific approaches:
* - **Browser**: Uses fetch() with HTTP URLs from Vite dev server
* - **Node.js**: Uses fs.readFile since Node's fetch() doesn't support file:// protocol
*
* Note: Node.js's native fetch() (v18+) explicitly does not support file:// URLs.
* This limitation is documented in the Node.js fetch implementation and returns
* "not implemented... yet..." error. The hybrid approach is necessary until Node.js
* adds file:// protocol support to fetch().
*/
export const loadFixture = (url: string): Uint8Array => {
export const loadFixture = async (url: string): Promise<Uint8Array> => {
// Browser environment: Vite serves files over HTTP during development
if (url.startsWith('http')) {
const arrayBuffer = await fetch(url).then((b) => b.arrayBuffer())
return new Uint8Array(arrayBuffer)
}

// Node.js environment: Use file system since fetch() doesn't support file:// protocol
const { readFile } = await import('node:fs/promises')
const { join } = await import('node:path')
const relativePath = url.startsWith('/') ? url.slice(1) : url
const filePath = join(process.cwd(), relativePath)
return new Uint8Array(readFileSync(filePath))
const buffer = await readFile(filePath)
return new Uint8Array(buffer)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests_integ/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe.each(providers)('Agent with $name', ({ name, skip, createModel }) => {
})

// Create image block
const imageBytes = loadFixture(yellowPngUrl)
const imageBytes = await loadFixture(yellowPngUrl)
const imageBlock = new ImageBlock({
format: 'png',
source: { bytes: imageBytes },
Expand Down
2 changes: 1 addition & 1 deletion tests_integ/bedrock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ describe.skipIf(!(await shouldRunTests()))('BedrockModel Integration Tests', ()
})

// PDF document
const pdfBytes = loadFixture(letterPdfUrl)
const pdfBytes = await loadFixture(letterPdfUrl)
const pdfDocBlock = new DocumentBlock({
name: 'letter',
format: 'pdf',
Expand Down
21 changes: 1 addition & 20 deletions tests_integ/browser/agent.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,11 @@ import { httpRequest } from '@strands-agents/sdk/vended_tools/http_request'
import { z } from 'zod'

import { collectGenerator } from '../../src/__fixtures__/model-test-helpers.js'
import { loadFixture } from '../__fixtures__/test-helpers.js'

// Import fixtures
import yellowPngUrl from '../__resources__/yellow.png?url'

// Environment detection for browser vs Node.js
const isNode = typeof process !== 'undefined' && typeof process.versions !== 'undefined' && !!process.versions.node

// Browser-compatible fixture loader
const loadFixture = async (url: string): Promise<Uint8Array> => {
if (isNode) {
// In Node.js, use synchronous file reading
const { readFileSync } = await import('node:fs')
const { join } = await import('node:path')
const relativePath = url.startsWith('/') ? url.slice(1) : url
const filePath = join(process.cwd(), relativePath)
return new Uint8Array(readFileSync(filePath))
} else {
// In browser, use fetch API
const response = await globalThis.fetch(url)
const arrayBuffer = await response.arrayBuffer()
return new Uint8Array(arrayBuffer)
}
}

// Calculator tool for testing
const calculatorTool = tool({
name: 'calculator',
Expand Down
5 changes: 5 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const getOpenAIAPIKey: BrowserCommand<[], string | undefined> = async ({
}

export default defineConfig({
server: {
fs: {
allow: ['.'],
},
},
test: {
projects: [
{
Expand Down