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

12.7 KB
Raw Blame
from __future__ import annotations

import tempfile
from pathlib import Path

from playwright.sync_api import expect

from e2e_support import base_entry, extension_session


PNG_BASE64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="


def main() -> None:
    entry = base_entry(
        "detail-actions-case",
        "详情操作层级验收案例",
        "保留主体关系、材质层次和光线方向的完整提示词。",
        "content:prompt:image",
        8,
    )
    entry["metadataLabels"] = ["作者:验收作者", "事项:详情动作归位"]
    entry["mediaAssets"] = [{
        "id": "detail-actions-image",
        "kind": "image",
        "usage": "content",
        "storageMode": "managed",
        "mimeType": "image/png",
        "width": 1,
        "height": 1,
        "capturedAt": "2026-08-08T00:00:00.000Z",
        "reviewStatus": "verified",
    }]
    entry["primaryMediaId"] = "detail-actions-image"
    no_source_entry = base_entry(
        "detail-no-source",
        "无来源详情验收案例",
        "用于确认没有来源信息时仍能删除案例。",
        "content:prompt:text",
        7,
    )
    no_source_entry["url"] = ""
    no_source_entry["metadataLabels"] = []
    projects = [{
        "id": f"collection:detail-{index}",
        "name": f"详情项目 {index:02d} " + ("完整项目名称需要换行显示" if index == 20 else ""),
        "order": index,
        "entryIds": [entry["id"]],
        "visibility": "library",
    } for index in range(21)]
    screenshots = Path(tempfile.gettempdir())

    with extension_session("prompt-director-detail-actions-", viewport={"width": 1440, "height": 900}) as session:
        setup = session.open_page("collector.html")
        setup.evaluate(
            """async ({entry, noSourceEntry, projects, png}) => {
              const {saveMediaBlob} = await import(chrome.runtime.getURL('media-store.js'));
              const bytes = Uint8Array.from(atob(png), value => value.charCodeAt(0));
              await chrome.storage.local.clear();
              await chrome.storage.local.set({
                schemaVersion: 25,
                entries: [entry, noSourceEntry],
                organizerState: {version: 5, collections: projects},
                uiPreferences: {locale: 'zh-CN', theme: 'dark', motion: 'reduced'}
              });
              await saveMediaBlob(entry.primaryMediaId, new Blob([bytes], {type: 'image/png'}), {checkCapacity: false});
            }""",
            {"entry": entry, "noSourceEntry": no_source_entry, "projects": projects, "png": PNG_BASE64},
        )

        library = session.open_page("library.html", wait_until="networkidle")
        library.locator(f'.case-card[data-entry-id="{entry["id"]}"]').click()
        expect(library.locator(".detail-header-section .entry-editor-inline > summary")).to_be_visible()
        expect(library.locator(".media-prompt-section > .original-prompt-panel > .prompt-section-heading")).to_be_visible()
        expect(library.locator(".detail-core-actions > button")).to_have_count(1)
        expect(library.get_by_role("button", name="复制提示词")).to_be_enabled()
        expect(library.get_by_role("button", name="以此创作")).to_be_enabled()
        expect(library.get_by_role("button", name="编辑共享提示词", exact=True)).to_have_count(0)
        project_menu = library.locator(".detail-project-menu")
        expect(project_menu.locator(":scope > summary")).to_have_text("详情项目 00")
        closed_height = project_menu.locator(":scope > summary").evaluate("node => node.getBoundingClientRect().height")
        assert closed_height <= 40, closed_height
        summary = project_menu.locator(":scope > summary")
        summary.click(position={"x": summary.bounding_box()["width"] - 8, "y": 12})
        expect(project_menu.locator(".detail-project-option")).to_have_count(21)
        checkbox_widths = project_menu.locator('.detail-project-option input[type="checkbox"]').evaluate_all(
            "nodes => nodes.map(node => node.getBoundingClientRect().width)"
        )
        assert checkbox_widths and max(checkbox_widths) <= 18, checkbox_widths
        long_project = project_menu.locator(".detail-project-option").nth(20)
        expect(long_project).to_contain_text("完整项目名称需要换行显示")
        long_project.locator("input").click()
        expect(project_menu.locator(":scope > summary")).to_have_text("详情项目 20 完整项目名称需要换行显示")
        project_menu.locator('input[aria-label="新项目名称"]').fill("详情新建项目")
        project_menu.get_by_role("button", name="新建并加入", exact=True).click()
        expect(library.locator(".detail-project-menu > summary")).to_have_text("详情新建项目")
        expect(library.locator(".detail-quick-organization")).not_to_contain_text("快捷整理")
        detail_order = library.evaluate(
            """() => {
              const prompt = document.querySelector('.prompt-section');
              const organizer = document.querySelector('.detail-quick-organization');
              return Boolean(prompt && organizer && (prompt.compareDocumentPosition(organizer) & Node.DOCUMENT_POSITION_FOLLOWING));
            }"""
        )
        assert detail_order is True
        tag_input = library.locator(".detail-quick-organization .tag-editor input")
        library.locator('.detail-quick-organization').get_by_role('button',name='添加标签',exact=True).click()
        tag_input.fill("客户喜欢,待复刻")
        library.locator(".detail-quick-organization .tag-editor").get_by_role("button", name="保存标签", exact=True).click()
        expect(library.locator(".detail-quick-organization .tag-editor-chip")).to_have_count(2)
        expect(library.locator(".detail-quick-organization .tag-editor-chip")).to_contain_text(["客户喜欢", "待复刻"])
        expect(library.locator(".detail-quick-organization")).not_to_contain_text("可选")
        expect(library.locator(".detail-quick-organization")).not_to_contain_text("不用预先创建")
        expect(library.locator(".detail-quick-organization")).not_to_contain_text("任意输入")
        expect(library.locator(".detail-quick-organization .detail-delete-action")).to_have_attribute('title','移入回收站')
        expect(library.locator(".metadata-section .detail-delete-action")).to_have_count(0)
        expect(library.locator(".metadata-actions > *")).to_have_count(2)
        expect(library.locator(".detail-content > .detail-footer-actions")).to_have_count(0)

        core_geometry = library.locator(".detail-core-actions > button").evaluate_all(
            "buttons => buttons.map(button => ({width: button.getBoundingClientRect().width, height: button.getBoundingClientRect().height}))"
        )
        assert len(core_geometry) == 1 and core_geometry[0]["height"] == 36, core_geometry
        expect(library.locator(".prompt-toolbar").get_by_role("button", name="复制提示词", exact=True)).to_be_visible()

        edit_geometry = library.evaluate(
            """() => {
              const title = document.querySelector('.detail-title').getBoundingClientRect();
              const edit = document.querySelector('.entry-editor-inline > summary').getBoundingClientRect();
              const toolbarButtons = [...document.querySelectorAll('.drawer-toolbar > .icon-button')]
                .filter(button => !button.hidden && getComputedStyle(button).display !== 'none')
                .map(button => button.getBoundingClientRect());
              const overlapsToolbar = toolbarButtons.some(button => !(
                edit.right <= button.left || edit.left >= button.right ||
                edit.bottom <= button.top || edit.top >= button.bottom
              ));
              return {titleTop: title.top, editTop: edit.top, delta: Math.abs(title.top - edit.top), overlapsToolbar};
            }"""
        )
        assert edit_geometry["delta"] < 8, edit_geometry
        assert edit_geometry["overlapsToolbar"] is False, edit_geometry
        library.locator(".entry-editor-inline > summary").click()
        expect(library.locator(".entry-editor-inline .entry-editor-body")).to_be_visible()
        expect(library.locator(".entry-editor-inline h4", has_text="案例标题")).to_have_count(1)
        expect(library.locator(".entry-editor-inline .entry-edit-row").first).to_be_visible()
        expect(library.locator(".entry-editor-inline .entry-edit-row").get_by_role("button", name="保存", exact=True)).to_be_visible()
        title_row = library.locator(".entry-editor-inline .entry-edit-row").first.evaluate(
            """node => ({display: getComputedStyle(node).display, columns: node.children.length})"""
        )
        assert title_row == {"display": "grid", "columns": 2}, title_row
        library.locator(".entry-editor-inline > summary").click()

        library.get_by_role("button", name="编辑原始提示词", exact=True).click()
        expect(library.get_by_role("textbox", name="编辑原始提示词")).to_be_visible()
        library.get_by_role("button", name="取消", exact=True).click()

        analysis = library.locator(".prompt-toolbar")
        expect(library.locator(".detail-analysis-menu")).to_have_count(0)
        expect(library.locator(".detail-analysis-actions")).to_have_count(0)
        expect(library.locator(".prompt-toolbar").get_by_role("button", name="分析图片")).to_be_visible()
        expect(analysis.get_by_role("button", name="分析文字标签")).to_be_visible()

        source = library.get_by_role("link", name="打开来源")
        expect(source).to_have_attribute("href", entry["url"])
        expect(source).to_have_attribute("target", "_blank")
        expect(source).to_have_attribute("rel", "noopener noreferrer")
        expect(library.locator(".metadata-actions .detail-delete-action")).to_be_visible()
        library.screenshot(path=str(screenshots / "promptdirector-step3-detail-desktop.png"), full_page=True)

        library.set_viewport_size({"width": 390, "height": 844})
        mobile_project_menu = library.locator(".detail-project-menu")
        mobile_project_popover = mobile_project_menu.locator(".detail-project-popover")
        for _ in range(5):
            if mobile_project_menu.get_attribute("open") is not None and mobile_project_popover.is_visible():
                break
            summary = mobile_project_menu.locator(":scope > summary")
            summary.click(position={"x": summary.bounding_box()["width"] - 8, "y": 12})
            library.wait_for_timeout(150)
        expect(mobile_project_menu).to_have_attribute("open", "")
        expect(mobile_project_popover).to_be_visible()
        library.locator(".prompt-section").scroll_into_view_if_needed()
        mobile = library.evaluate(
            """() => {
              const actions = document.querySelector('.detail-core-actions').getBoundingClientRect();
              return {
                actionsLeft: actions.left,
                actionsRight: actions.right,
                viewport: innerWidth,
                documentOverflow: document.documentElement.scrollWidth > document.documentElement.clientWidth,
                detailOverflow: document.querySelector('#detail-content').scrollWidth > document.querySelector('#detail-content').clientWidth
              };
            }"""
        )
        assert mobile["actionsLeft"] >= -1 and mobile["actionsRight"] <= mobile["viewport"] + 1, mobile
        assert mobile["documentOverflow"] is False and mobile["detailOverflow"] is False, mobile
        expect(library.get_by_role("button", name="复制提示词")).to_be_visible()
        expect(library.get_by_role("button", name="以此创作")).to_be_visible()
        library.screenshot(path=str(screenshots / "promptdirector-step3-detail-mobile.png"), full_page=True)

        library.locator("#detail-close").click()
        library.locator(f'.case-card[data-entry-id="{no_source_entry["id"]}"]').click()
        expect(library.locator(".metadata-section")).to_have_count(0)
        expect(library.locator('.detail-quick-organization .detail-delete-action')).to_be_visible()
        expect(library.locator('.detail-quick-organization').get_by_role('link',name='打开来源',exact=True)).to_have_count(0)
        expect(library.locator(".detail-body > .detail-footer-actions")).to_have_count(0)

        print({
            "core_actions": core_geometry,
            "title_edit_alignment": edit_geometry,
            "analysis_choices": 2,
            "source_href": entry["url"],
            "mobile": mobile,
            "screenshots": [
                str(screenshots / "promptdirector-step3-detail-desktop.png"),
                str(screenshots / "promptdirector-step3-detail-mobile.png"),
            ],
        })


if __name__ == "__main__":
    main()