Skip to content

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. 提示词导演

3.9 KB
Raw Blame
import test from 'node:test';
import assert from 'node:assert/strict';
import { setPdReferenceDragData, parsePdReference, pdReferenceLink } from '../extension/pd-reference.js';

function transfer() {
  const data = new Map([['text/plain', 'stale thumbnail']]);
  return { data, clearData: () => data.clear(), setData: (key, value) => data.set(key, value) };
}
test('a card names the whole case and carries its library identity even when two cases have the same title', () => {
  const a = transfer(), b = transfer();
  const reference = setPdReferenceDragData(a, { libraryId: 'library', caseId: 'multi', name: '同名案例' });
  setPdReferenceDragData(b, { libraryId: 'library', caseId: 'compound', name: '同名案例' });
  assert.equal(a.data.get('text/plain'), `[同名案例](${reference})`);
  assert.equal(a.data.get('text/html'), `<a href="${reference.replaceAll('&', '&amp;')}">同名案例</a>`);
  assert.notEqual(a.data.get('text/plain'), b.data.get('text/plain'));
  assert.deepEqual(parsePdReference(reference), { libraryId: 'library', caseId: 'multi', assetId: '' });
  assert(!a.data.has('DownloadURL'), 'a video card must not export its poster as the video');
});
test('specific image drag retains original canvas/file payload while exposing the exact media name and reference', () => {
  const value = transfer(), url = 'blob:original-image';
  const reference = setPdReferenceDragData(value, { libraryId: 'library', caseId: 'multi', assetId: 'image-2', name: '角色 [修订].png',
    file: { kind: 'image', url, mimeType: 'image/png', name: '角色 [修订].png' } });
  assert.equal(parsePdReference(reference).assetId, 'image-2');
  assert.equal(value.data.get('text/plain'), url, 'file receivers must retain the original URI flavor');
  assert.equal(value.data.get('text/uri-list'), url);
  assert.equal(value.data.get('DownloadURL'), `image/png:角色 [修订].png:${url}`);
  assert.deepEqual(JSON.parse(value.data.get('application/x-promptdirector-file')), { url, mimeType: 'image/png', name: '角色 [修订].png' });
  assert(value.data.get('text/html').includes('<img src="blob:original-image"'), 'canvas receivers need the original image HTML flavor');
  assert(!value.data.get('text/html').includes('hidden'), 'image receivers must receive visible original media');
  assert(!value.data.get('text/html').includes('promptdirector://'), 'media drags must not insert Agent reference links');
  assert(!value.data.get('text/plain').includes('对应画面'));
});
test('text-only case name links still survive protocol filtering', () => {
  const value = transfer();
  const reference = setPdReferenceDragData(value, { libraryId: 'library', caseId: 'article', name: '文章',
    linkBase: 'https://example.org/source(draft)?original=1#old-position' });
  const href = value.data.get('text/html').match(/href="([^"]+)"/)[1].replaceAll('&amp;', '&');
  assert.equal(new URL(href).pathname, '/source%28draft%29');
  assert.deepEqual(parsePdReference(href), parsePdReference(reference));
  assert(!value.data.has('DownloadURL'));
});
test('name-link wrappers reject ambiguous or unsafe references', () => {
  const ref = 'promptdirector://reference?v=1&library=library&case=case';
  for (const base of ['javascript:bad', 'https://user:[email protected]']) assert.throws(() => pdReferenceLink(ref, base));
  for (const link of ['https://example.org', 'https://example.org#pd-reference=javascript:bad',
      `https://example.org#pd-reference=${encodeURIComponent(ref)}&pd-reference=${encodeURIComponent(ref)}`,
      `https://example.org#pd-reference=${encodeURIComponent(ref)}&other=1`]) assert.throws(() => parsePdReference(link));
});
test('untrusted case names cannot inject markup into the external drag', () => {
  const value = transfer();
  setPdReferenceDragData(value, { libraryId: 'library', caseId: 'case', name: '<img onerror="bad">\n' });
  assert(!value.data.get('text/html').includes('<img'));
  assert(value.data.get('text/html').includes('&lt;img onerror=&quot;bad&quot;&gt;'));
});