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

5.6 KB
Raw Blame
"""A failed original stays pending; explicitly choosing online-only completes the same case without duplicate text or media."""
import re
from playwright.sync_api import expect
from e2e_support import extension_session
from page_capture_e2e import fixture_png


def main():
    with extension_session('capture-partial-save-', viewport={'width':390,'height':844}) as run:
        run.context.route('https://pbs.twimg.com/**', lambda route: route.fulfill(body=fixture_png(route.request.url), content_type='image/png'))
        page=run.open_page('collector.html')
        run.seed_storage(page, {'entries':[], 'capturePermissionOnboarding':{'version':1,'acknowledgedAt':'2026-09-07T00:00:00Z','clipboardIncluded':True}})
        page.evaluate("""async () => {
          const send=chrome.runtime.sendMessage.bind(chrome.runtime);
          const {createCaptureDraft}=await import('./capture-draft.js');
          await send({type:'UPDATE_CAPTURE_DRAFT',draft:createCaptureDraft({fragments:[{id:'clipboard-fragment',text:'用户补充的剪贴板文字,已入库后应从侧栏清除。',sourceUrl:'https://x.com/director/status/123'}]})});
          chrome.permissions.contains=async()=>true;chrome.permissions.request=async()=>true;
          chrome.tabs.query=async()=>[{id:999,url:'https://x.com/director/status/123'}];
          chrome.runtime.sendMessage=async m=>{
            // Saves now run through the background save-task pipeline: the collector sends
            // START_CAPTURE_SAVE{input:COMMIT_PAGE_CAPTURE} and reads the finished task back.
            if(m.type==='START_CAPTURE_SAVE' && m.input?.type==='COMMIT_PAGE_CAPTURE' && window.forceSaveFailure)return {ok:false,message:'模拟保存失败'};
            if(m.type==='START_PAGE_CAPTURE') return {ok:true,batch:{sourceUrl:'https://x.com/director/status/123',candidates:[{
              id:'partial-video',title:'Video prompt fixture',canonicalUrl:'https://x.com/director/status/123',pageType:'post',
              contentText:'Prompt: a cinematic martial arts scene.',textBlocks:[{id:'text',kind:'section',text:'Prompt: a cinematic martial arts scene.'}],
              media:[{id:'video',kind:'video',url:'',posterUrl:'https://pbs.twimg.com/video-poster.png',placement:'inline',sourceKind:'video-element'}],
              articleDocument:{version:1,blocks:[{id:'text',kind:'paragraph',text:'Prompt: a cinematic martial arts scene.'},{id:'video-block',kind:'video',assetId:'video'}]},
              sourceFacts:{provider:'x',pageType:'post'}
            }]}};
            if(['PREVIEW_PAGE_CAPTURE_REGION','CLEAR_PAGE_CAPTURE_MARKERS'].includes(m.type)) return {ok:true};
            const result=await send(m);
            const task=m.type==='GET_CAPTURE_SAVE_TASK'?result?.task:null;
            if(task?.input?.type==='COMMIT_PAGE_CAPTURE' && ['completed','failed','confirmation'].includes(task.status))window.saveResult=task.result;
            return result;
          };
        }""")
        page.evaluate("()=>window.dispatchEvent(new Event('focus'))")
        page.locator('#add-page-capture').click()
        page.locator('.page-capture-confirm').click()
        page.locator('#page-capture-save').click()
        page.wait_for_function('()=>Boolean(window.saveResult)')
        result=page.evaluate('window.saveResult')
        assert result['ok'] and result['results'][0]['status']=='partial', result
        entries=page.evaluate("async()=> (await import(chrome.runtime.getURL('library-storage.js')).then(({getLibraryStorage}) => getLibraryStorage().get('entries'))).entries")
        assert len(entries)==1 and not any(asset['kind']=='video' for asset in entries[0]['mediaAssets']),entries
        assert 'captureWarnings' not in entries[0]['sourceFacts'], entries
        assert result['results'][0]['warnings'], result
        expect(page.locator('#page-capture')).to_be_visible()
        # The failure may save the original text, but cannot invent a player or
        # clear the failed video. Only the user's explicit storage choice does so.
        page.evaluate("async()=>chrome.runtime.sendMessage({type:'UPDATE_UI_PREFERENCES',preferences:{videoOnlineOnly:true}})")
        page.locator('#page-capture-save').click()
        expect(page.locator('#page-capture')).to_be_hidden()
        saved=page.evaluate("async()=>(await chrome.runtime.sendMessage({type:'GET_STATE'})).entries")
        assert len(saved)==1 and any(a['kind']=='video' and a['storageMode']=='reference' for a in saved[0]['mediaAssets']),saved
        expect(page.locator('#feedback')).to_contain_text('已保存')
        assert len(page.evaluate("async()=> (await import(chrome.runtime.getURL('library-storage.js')).then(({getLibraryStorage}) => getLibraryStorage().get('entries'))).entries"))==1
        expect(page.locator('#feedback')).not_to_have_class(re.compile(r'\berror\b'))
        workspace=page.evaluate("async()=>chrome.runtime.sendMessage({type:'GET_CAPTURE_WORKSPACE'})")
        assert workspace['draft']['fragments']==[],workspace
        page.evaluate('()=>window.forceSaveFailure=true')
        page.locator('#start-page-capture').click()
        page.locator('.page-capture-confirm').click()
        page.locator('#page-capture-save').click()
        expect(page.locator('#feedback')).to_contain_text('模拟保存失败')
        expect(page.locator('#page-capture')).to_be_visible()
        assert len(page.evaluate("async()=> (await import(chrome.runtime.getURL('library-storage.js')).then(({getLibraryStorage}) => getLibraryStorage().get('entries'))).entries"))==1
        print({'case_saved':True,'diagnostics_not_in_case':True,'saved_sidebar_cleared':True,'unsaved_content_preserved':True})


if __name__=='__main__':main()