Spot it. Save it. Free, local-first Chrome extension for images, videos and prompts. Explore 2,000+ curated cases and connect your library to AI Agents via MCP. 提示词导演
JavaScript ai-artai-videochrome-extensionclaude-codecodexhiggsfieldjimenglocal-firstmcpmidjourneymoodboardprompt-engineeringprompt-libraryprompt-managerreference-libraryseedancevisual-bookmarksweb-clipper
import test from 'node:test';
import assert from 'node:assert/strict';
import { captureChatAssetIds, readCaptureChatAttachment, resolveCaptureLocalMedia } from '../extension/capture-chat-attachments.js';
import { runCaptureAssistant } from '../extension/capture-assistant.js';
import { executeComposerTurnWithService } from '../extension/composer-service.js';
const file = { transferId: 'user-file', name: 'target.png', assetId: 'original', kind: 'image' };
const record = { id: file.transferId, name: file.name, state: 'ready', assetId: file.assetId,
prepared: { asset: { id: file.assetId, kind: 'image', mimeType: 'image/png' }, contentText: '' } };
const dependencies = { transfers: { get: async id => { assert.equal(id, file.transferId); return record; } }, readBlob: async () => new Blob(['actual original'], { type: 'image/png' }) };
const runtime = { aiTaskAssignments: { creativePlanning: { providerId: 'deepseek', model: 'deepseek-flash' } },
aiSettings: { apiKey: 'fixture', consent: true }, visionSettings: { consent: true, openai: { apiKey: 'fixture', model: 'gpt-4.1' } } };
const input = { instruction: '截图中这块才是我要保存的网页内容,先告诉我是否看见', sourceUrl: 'https://example.test/', tabId: 1, attachments: [file] };
test('a screenshot reaches the actual shared provider request as image bytes, while no library write occurs', async () => {
let requests = 0;
const result = await runCaptureAssistant({ ...input, aiProfile: { serviceId: 'openai', model: 'gpt-4.1' } }, {
runtime, signal: new AbortController().signal, readAttachment: id => readCaptureChatAttachment(id, dependencies),
call: () => assert.fail('attaching an image does not select or save it'),
execute: (request, settings, images, options) => executeComposerTurnWithService(request, settings, images, { ...options, stream: false,
fetchImpl: async (_url, init) => {
requests++; const body = JSON.parse(init.body);
assert.equal(body.model, 'gpt-4.1');
assert(JSON.stringify(body).includes('input_image'));
assert(JSON.stringify(body).includes(Buffer.from('actual original').toString('base64')));
return new Response(JSON.stringify({ status: 'completed', output: [{ type: 'message', role: 'assistant', content: [{ type: 'output_text', text: '收到真实截图' }] }] }));
} })
});
assert.equal(result.ok, true); assert.equal(requests, 1);
});
test('a text-only model rejects image input before any paid request and preserves its explicit attachment identity', async () => {
const textRuntime = { ...runtime, visionSettings: { providerProfiles: { 'custom-text': { id: 'custom-text', apiKey: 'fixture', consent: true,
endpoint: 'https://example.test/chat/completions', protocol: 'chat_completions', capabilities: ['creativePlanning'], models: { creativePlanning: 'text-only' },
discoveredModels: [{ id: 'text-only', tasks: ['creativePlanning'], inputModalities: ['text'], supportedParameters: ['tools'] }] } } } };
const result = await runCaptureAssistant({ ...input, aiProfile: { serviceId: 'custom-text', model: 'text-only' } }, { runtime: textRuntime, signal: new AbortController().signal,
readAttachment: id => readCaptureChatAttachment(id, dependencies), call: () => assert.fail('no capture'), execute: () => assert.fail('no paid request') });
assert.equal(result.ok, false); assert.match(result.message, /不能接收图片/); assert.equal(file.transferId, 'user-file');
});
test('historical attachments remain scoped to the human conversation and are re-read only when requested', async () => {
const result = await runCaptureAssistant({ ...input, attachments: [], messages: [{ role: 'user', content: '看这张截图', attachments: [file] }], aiProfile: { serviceId: 'openai', model: 'gpt-4.1' } }, {
runtime, signal: new AbortController().signal, readAttachment: id => readCaptureChatAttachment(id, dependencies), call: () => assert.fail('no capture'),
execute: async (_request, _settings, images, { toolRuntime }) => {
assert.equal(images.length, 0);
assert.equal((await toolRuntime.execute('read_chat_attachment', { transferId: 'other-session-file' })).data.code, 'attachment_unavailable');
assert.equal((await toolRuntime.execute('update_capture', { captureId: 'preview', candidates: [{ media: [{ localTransferId: 'other-session-file' }] }] })).data.code, 'capture_scope_changed');
assert.equal((await toolRuntime.execute('read_chat_attachment', { transferId: file.transferId })).images.length, 1);
return { text: '按需重读' };
} });
assert.equal(result.ok, true);
});
test('saving an attachment uses its staged original identity and protects attachments across draft, history and page changes', async () => {
const media = await resolveCaptureLocalMedia({ id: 'picked', kind: 'image', localTransferId: file.transferId }, dependencies.transfers);
assert.equal(media.localAssetId, file.assetId); assert.equal(media.filename, file.name); assert.equal(media.url, '');
await assert.rejects(resolveCaptureLocalMedia({ kind: 'video', localTransferId: file.transferId }, dependencies.transfers), { code: 'invalid_input' });
assert.deepEqual(captureChatAssetIds({ history: [{ messages: [{ attachments: [file] }] }], pages: { current: { attachments: [file] } } }), [file.assetId]);
});
for (const kind of ['image', 'document', 'video']) test(`a committed ${kind} attachment reads and reuses the current library original after temporary preparation is cleared`, async () => {
const asset = { id: file.assetId, kind, mimeType: kind === 'image' ? 'image/png' : kind === 'video' ? 'video/mp4' : 'text/plain', posterAssetId: kind === 'video' ? 'poster' : undefined };
const committed = { ...record, state: 'committed', prepared: null, entryId: 'saved-case' };
let entry = { id: 'saved-case', text: 'case commentary is not the uploaded original', mediaAssets: [asset, ...(kind === 'video' ? [{ id: 'poster', kind: 'image', derivedFromAssetId: asset.id }] : [])] };
const deps = { ...dependencies, transfers: { get: async () => committed }, loadState: async () => ({ entries: entry ? [entry] : [] }),
readDerived: async id => { assert.equal(id, asset.id); return { searchText: 'Complete uploaded document' }; } };
const read = await readCaptureChatAttachment(file.transferId, deps);
assert.equal(read.data.localAssetId, asset.id);
assert.equal(read.data.text, 'Complete uploaded document');
assert.equal(read.images?.length || 0, kind === 'image' ? 1 : 0);
const reuse = await resolveCaptureLocalMedia({ kind, localTransferId: file.transferId }, deps.transfers, deps);
assert.equal(reuse.localAssetId, asset.id);
// Deleting the saved case or detaching the original must not resurrect it
// from a still-retained chat reference or transfer record.
entry.mediaAssets = [];
await assert.rejects(readCaptureChatAttachment(file.transferId, deps), { code: 'attachment_unavailable' });
entry = null;
await assert.rejects(resolveCaptureLocalMedia({ kind, localTransferId: file.transferId }, deps.transfers, deps), { code: 'attachment_unavailable' });
});