import { describe, expect, test } from 'bun:test'; import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import type { InvocationResource } from '../../../src/plugin-sdk.js'; import { bindResourceInputs, materializeResources, } from '../src/resourceInputs.js'; const imageDef = { name: 'source', type: 'resource' as const, required: true, accepts: ['image/*'], allowFile: true, allowDirectory: true, multiple: true, attachmentFallback: true, }; describe('workflow resource inputs', () => { test('binds compatible Desktop attachments and materializes multiple files', async () => { const root = await mkdtemp(join(tmpdir(), 'workflow-resource-')); try { const a = join(root, 'a.jpg'); const b = join(root, 'b.png'); await writeFile(a, 'a'); await writeFile(b, 'b'); const attachments: InvocationResource[] = [ { id: 'a', origin: 'attachment', name: 'a.jpg', mime: 'image/jpeg', size: 1, sha256: 'sha-a', localPath: a, kind: 'file', }, { id: 'b', origin: 'attachment', name: 'b.png', mime: 'image/png', size: 1, sha256: 'sha-b', localPath: b, kind: 'file', }, ]; const bound = await bindResourceInputs({ workflowName: 'wrong-question-docx', sessionId: 's1', rawInputs: {}, defs: [imageDef], attachments, }); expect(typeof bound.inputs.source).toBe('string'); expect(bound.resourceMap.source).toHaveLength(2); expect(bound.resourceIdentity).toEqual(['source:sha-a', 'source:sha-b']); const staged = String(bound.inputs.source); expect(await readFile(join(staged, '001-a.jpg'), 'utf8')).toBe('a'); expect(await readFile(join(staged, '002-b.png'), 'utf8')).toBe('b'); } finally { await rm(root, { recursive: true, force: true }); } }); test('explicit path wins over attachments', async () => { const root = await mkdtemp(join(tmpdir(), 'workflow-resource-')); try { const path = join(root, 'paper.jpg'); await writeFile(path, 'paper'); const bound = await bindResourceInputs({ workflowName: 'wrong-question-docx', sessionId: 's1', rawInputs: { source: path }, defs: [imageDef], attachments: [ { id: 'other', origin: 'attachment', name: 'other.jpg', mime: 'image/jpeg', localPath: path, kind: 'file', }, ], }); expect(bound.inputs.source).toBe(path); expect(bound.resourceMap.source?.[0]?.origin).toBe('path'); expect(bound.warnings).toHaveLength(0); } finally { await rm(root, { recursive: true, force: true }); } }); test('relative explicit paths resolve from the workflow cwd', async () => { const root = await mkdtemp(join(tmpdir(), 'workflow-resource-')); try { await writeFile(join(root, 'paper.jpg'), 'paper'); const bound = await bindResourceInputs({ workflowName: 'wrong-question-docx', sessionId: 's1', cwd: root, rawInputs: { source: 'paper.jpg' }, defs: [imageDef], }); expect(bound.inputs.source).toBe(join(root, 'paper.jpg')); } finally { await rm(root, { recursive: true, force: true }); } }); test('directory materialization stages even one Desktop file', async () => { const root = await mkdtemp(join(tmpdir(), 'workflow-resource-')); try { const file = join(root, 'one.jpg'); await writeFile(file, 'one'); const bound = await bindResourceInputs({ workflowName: 'e2e-vision-schema', sessionId: 's1', rawInputs: {}, defs: [ { ...imageDef, name: 'dir', allowFile: false, materialize: 'directory', }, ], attachments: [ { id: 'one', origin: 'attachment', name: 'one.jpg', mime: 'image/jpeg', sha256: 'sha-one', localPath: file, kind: 'file', }, ], }); expect( await readFile(join(String(bound.inputs.dir), '001-one.jpg'), 'utf8'), ).toBe('one'); } finally { await rm(root, { recursive: true, force: true }); } }); test('single Desktop file also uses a stable Runner copy', async () => { const root = await mkdtemp(join(tmpdir(), 'workflow-resource-')); try { const file = join(root, 'one.jpg'); await writeFile(file, 'one'); const bound = await bindResourceInputs({ workflowName: 'wrong-question-docx', sessionId: 's1', rawInputs: {}, defs: [{ ...imageDef, multiple: false }], attachments: [ { id: 'one', origin: 'attachment', name: 'one.jpg', mime: 'image/jpeg', sha256: 'sha-one', localPath: file, kind: 'file', }, ], }); expect(bound.inputs.source).not.toBe(file); expect(await readFile(String(bound.inputs.source), 'utf8')).toBe('one'); } finally { await rm(root, { recursive: true, force: true }); } }); test('reference identity uses resource ids instead of content hashes', async () => { const root = await mkdtemp(join(tmpdir(), 'workflow-resource-')); try { const path = join(root, 'paper.jpg'); await writeFile(path, 'paper'); const bound = await bindResourceInputs({ workflowName: 'demo', sessionId: 's1', rawInputs: { source: path }, defs: [{ ...imageDef, identity: 'reference' }], }); expect(bound.resourceIdentity).toHaveLength(1); expect(bound.resourceIdentity[0]).toMatch(/^source:path-[a-f0-9]{24}$/); } finally { await rm(root, { recursive: true, force: true }); } }); test('content identity notices changes inside an explicit directory', async () => { const root = await mkdtemp(join(tmpdir(), 'workflow-resource-')); try { const source = join(root, 'papers'); await mkdir(source); await writeFile(join(source, 'page.jpg'), 'page-a'); const options = { workflowName: 'wrong-question-docx', sessionId: 's1', rawInputs: { source }, defs: [imageDef], }; const first = await bindResourceInputs(options); await writeFile(join(source, 'page.jpg'), 'page-b'); const second = await bindResourceInputs(options); expect(first.resourceIdentity).not.toEqual(second.resourceIdentity); } finally { await rm(root, { recursive: true, force: true }); } }); test('rejects incompatible attachments instead of guessing', async () => { const bound = await bindResourceInputs({ workflowName: 'wrong-question-docx', sessionId: 's1', rawInputs: {}, defs: [imageDef], attachments: [ { id: 'pdf', origin: 'attachment', name: 'paper.pdf', mime: 'application/pdf', localPath: '/tmp/paper.pdf', kind: 'file', }, ], }); expect(bound.inputs.source).toBeUndefined(); expect(bound.resourceMap.source).toBeUndefined(); }); test('rejects attachment batches above maxItems', async () => { await expect( bindResourceInputs({ workflowName: 'wrong-question-docx', sessionId: 's1', rawInputs: {}, defs: [{ ...imageDef, maxItems: 1 }], attachments: [ { id: 'a', origin: 'attachment', name: 'a.jpg', mime: 'image/jpeg', localPath: '/tmp/a.jpg', kind: 'file', }, { id: 'b', origin: 'attachment', name: 'b.jpg', mime: 'image/jpeg', localPath: '/tmp/b.jpg', kind: 'file', }, ], }), ).rejects.toThrow('最多允许 1 个附件'); }); test('staging path is stable for the same resources', async () => { const root = await mkdtemp(join(tmpdir(), 'workflow-resource-')); try { const file = join(root, 'x.jpg'); await writeFile(file, 'x'); const resource: InvocationResource = { id: 'x', origin: 'attachment', name: 'x.jpg', mime: 'image/jpeg', size: 1, sha256: 'sha-x', localPath: file, kind: 'file', }; expect(await materializeResources('wf', 's', 'source', [resource])).toBe( await materializeResources('wf', 's', 'source', [resource]), ); } finally { await rm(root, { recursive: true, force: true }); } }); });