This commit is contained in:
2026-11-08 20:08:53 +08:00
parent 7526a5fe4d
commit 9b924747c5
2692 changed files with 394867 additions and 0 deletions

View File

@@ -0,0 +1,172 @@
import { click, describe, expect, test, microTick, queryFirst } from "@odoo/hoot";
import { defineWebsiteModels } from "@website/../tests/builder/website_helpers";
import { defineAIModels } from "@ai/../tests/ai_test_helpers";
import { loadBundle } from "@web/core/assets";
import { setupAiWebsiteBuilder } from "./ai_website_test_helpers";
defineAIModels();
defineWebsiteModels({
includeMailModels: false,
});
async function setupScriptsManager(websiteContent) {
await setupAiWebsiteBuilder(websiteContent, { openEditor: false });
await loadBundle("website.website_builder_assets");
const doc = queryFirst(":iframe");
const manager = doc.defaultView.__aiScriptsManager__.manager;
return {
doc,
manager,
addScript(scriptId) {
const scriptEl = doc.createElement("script");
scriptEl.dataset.aiScriptId = scriptId;
doc.querySelector("#wrap").appendChild(scriptEl);
return scriptEl;
},
aiRun(fn) {
manager.protectMutations(fn)();
},
};
}
describe("AI scripts manager", () => {
// TODO SERU: find a way to make this work functionally.
test.todo("stopping one of two scripts should keep the other script's changes", async () => {
const { manager, addScript, aiRun } = await setupScriptsManager(
`<section><div class="target" style="color: blue;">content</div></section>`
);
const target = queryFirst(":iframe .target");
const scriptA = addScript("script_a");
const scriptB = addScript("script_b");
aiRun(() => {
manager.trackElement(target, scriptA);
target.style.color = "red";
});
aiRun(() => {
manager.trackElement(target, scriptB);
target.classList.add("from-b");
});
manager.disposeScript(scriptB);
expect(target).not.toHaveClass("from-b");
expect(target.style.color).toBe("red");
});
test("stopping a script should leave the element usable by the script still running", async () => {
const { manager, addScript, aiRun } = await setupScriptsManager(
`<section><div class="target" style="color: blue;">content</div></section>`
);
const target = queryFirst(":iframe .target");
const original = target.outerHTML;
const scriptA = addScript("script_a");
const scriptB = addScript("script_b");
aiRun(() => {
manager.trackElement(target, scriptA);
target.style.color = "red";
});
aiRun(() => {
manager.trackElement(target, scriptB);
target.classList.add("from-b");
});
manager.disposeScript(scriptA);
expect(target.outerHTML).toBe(original);
aiRun(() => {
manager.trackElement(target, scriptB);
target.style.opacity = "0.5";
});
expect(target.outerHTML).not.toBe(original);
manager.disposeScript(scriptB);
expect(target.outerHTML).toBe(original);
});
test("a user edit on an AI-owned property should save this change", async () => {
const { manager, addScript, aiRun } = await setupScriptsManager(
`<section><div class="target" style="color: blue;">content</div></section>`
);
const target = queryFirst(":iframe .target");
const scriptEl = addScript("script_a");
aiRun(() => {
manager.trackElement(target, scriptEl);
target.style.color = "red";
});
await microTick();
// The user overrides the property the AI made.
target.style.color = "green";
await microTick();
// The script then writes it again: the value to restore is now the
// user's one, not the original one.
aiRun(() => {
target.style.color = "purple";
});
await microTick();
manager.disposeScript(scriptEl);
expect(target.style.color).toBe("green");
});
test("disposeAll should leave the page clean and trackable again", async () => {
const { manager, addScript, aiRun } = await setupScriptsManager(
`<section><div class="target" style="color: blue;">content</div></section>`
);
const target = queryFirst(":iframe .target");
const original = target.outerHTML;
const scriptA = addScript("script_a");
aiRun(() => {
manager.trackElement(target, scriptA);
target.style.color = "red";
});
manager.disposeAll();
expect(target.outerHTML).toBe(original);
expect(":iframe [data-ai-tracked]").toHaveCount(0);
// The scripts are restarted (e.g. when leaving the editor): tracking
// must work again on the same element.
const scriptB = addScript("script_b");
aiRun(() => {
manager.trackElement(target, scriptB);
target.style.color = "green";
});
manager.disposeScript(scriptB);
expect(target.outerHTML).toBe(original);
});
test("should correctly handle ai and non-ai mutations done together", async () => {
const { manager, addScript } = await setupScriptsManager(
`<section><button class="target" style="color: blue; font-size: 10px;">Click</button></section>`
);
const target = queryFirst(":iframe .target");
const scriptEl = addScript("script_a");
// Both listeners answer the same click, but the AI's one should be
// revertable.
target.addEventListener(
"click",
manager.protectMutations(() => {
manager.trackElement(target, scriptEl);
target.style.color = "red";
})
);
target.addEventListener("click", () => {
target.style.fontSize = "20px";
});
await click(target);
await microTick();
expect(target.style.color).toBe("red");
expect(target.style.fontSize).toBe("20px");
// Should revert ai script's changes, while keeping the normal ones.
manager.disposeScript(scriptEl);
expect(target.style.color).toBe("blue");
expect(target.style.fontSize).toBe("20px");
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,15 @@
import { queryOne } from "@odoo/hoot-dom";
import { loadBundle } from "@web/core/assets";
import { setupWebsiteBuilder } from "@website/../tests/builder/website_helpers";
export async function setupAiWebsiteBuilder(websiteContent, options = {}) {
const { openEditor = true, ...rest } = options;
const result = await setupWebsiteBuilder(websiteContent, { ...rest, openEditor: false });
const targetDoc = queryOne(":iframe");
await loadBundle("ai_website.ai_assets", { targetDoc });
await loadBundle("ai_website.ai_editor_assets", { targetDoc });
if (openEditor) {
await result.openBuilderSidebar();
}
return result;
}

View File

@@ -0,0 +1,49 @@
import { expect, test } from "@odoo/hoot";
import { animationFrame, click, waitFor } from "@odoo/hoot-dom";
import { registry } from "@web/core/registry";
import { contains, patchWithCleanup } from "@web/../tests/web_test_helpers";
import { getDragHelper, waitForEndOfOperation } from "@html_builder/../tests/helpers";
import {
defineWebsiteModels,
setupWebsiteBuilder,
} from "@website/../tests/builder/website_helpers";
defineWebsiteModels();
test("dropping image snippet and clicking AI button keeps placeholder", async () => {
// Mock aiChatLauncher service to avoid RPCs and complex setup
const aiChatLauncher = registry.category("services").get("aiChatLauncher");
patchWithCleanup(aiChatLauncher, {
start() {
return {
launchAIChat: async () => ({ id: "test_thread", channel: {} }),
};
},
});
// The text is added to introduce a drop zone for snippets.
await setupWebsiteBuilder(`<div><p>Text</p></div>`);
// Drag the snippet from the sidebar to the content
const { moveTo, drop } = await contains(
".o-website-builder_sidebar [name='Image'] .o_snippet_thumbnail"
).drag();
await moveTo(":iframe .oe_drop_zone");
await drop(getDragHelper());
// Verify media dialog opens
await animationFrame();
await waitFor(".o_select_media_dialog");
expect(".o_select_media_dialog").toHaveCount(1);
// Click the AI button
await click("button[title='Generate Image with AI']");
await animationFrame();
// Verify media dialog is closed
expect(".o_select_media_dialog").toHaveCount(0);
// Verify snippet is dropped (not discarded)
await waitForEndOfOperation();
expect("[data-snippet='s_image']").toHaveCount(1);
});

View File

@@ -0,0 +1,3 @@
import { registerRoute } from "@mail/../tests/mock_server/mail_mock_server";
registerRoute("/ai_website/ready_scraper_results", () => []);

View File

@@ -0,0 +1,112 @@
/** @odoo-module **/
import { registerWebsitePreviewTour, clickOnSave } from "@website/js/tours/tour_utils";
import { deliverAIResults, setupAIResults } from "@ai/../tests/tours/ai_tour_helpers";
registerWebsitePreviewTour(
"replace_website_image_with_ai_tour",
{
edition: true,
},
() => [
{
content:
"Double-click the image on the website to open the Media Dialog to replace the image",
trigger: ":iframe #test_image",
run: "dblclick",
},
{
content: "Wait for Media Dialog to open and click on the AI button",
trigger: ".o_select_media_dialog button[title='Generate Image with AI']",
run: "click",
},
setupAIResults(),
{
content:
"Click on the prompt button 'Help me generate an image' in the AI chat channel",
trigger: ".ai-chat-prompt-button:contains('Help me generate an image')",
run: "click",
},
{
content: "Verify the message 'Help me generate an image' is posted in the chat channel",
trigger: ".o-mail-Message:contains('Help me generate an image')",
},
deliverAIResults(),
{
content: "Click on the 'Use This' button",
trigger: "button[name='media_dialog_use_this']:not(:visible)",
run: "click",
},
{
content: "Verify the image on the website is replaced by the first AI generated image",
trigger: ":iframe img[data-original-src*='AI%20Generated%201']",
},
{
content: "Hover over the first AI generated image again",
trigger: ".o-mail-Message:contains('AI Generated 1') .o-mail-AttachmentList img",
run: "hover",
},
{
content: "Verify that 'Try again' button is visible and click on it",
trigger: "button[name='regenerate_image']:visible",
run: "click",
},
deliverAIResults(),
{
content: "Click the 'Use This' button for the second AI generated image",
trigger:
".o-mail-Message:contains('AI Generated 2') button[name='media_dialog_use_this']:not(:visible)",
run: "click",
},
{
content: "Verify the image on the website is replaced by the second AI generated image",
trigger: ":iframe img[data-original-src*='AI%20Generated%202']",
},
{
content: "Focus the image",
trigger: ":iframe img[data-original-src*='AI%20Generated%202']",
run: "click",
},
{
content: "First Undo (Ctrl + z)",
trigger: "body",
run: "press ctrl+z",
},
{
content: "Verify that the image on the website is the first AI generated image",
trigger: ":iframe img[data-original-src*='AI%20Generated%201']",
},
{
content: "Second Undo (Ctrl + z)",
trigger: "body",
run: "press ctrl+z",
},
{
content: "Verify the image on the website is the initial image",
trigger: ":iframe #test_image",
},
{
content: "First Redo (Ctrl + y)",
trigger: "body",
run: "press ctrl+y",
},
{
content: "Verify the image on the website is the first AI generated image",
trigger: ":iframe img[data-original-src*='AI%20Generated%201']",
},
{
content: "Second Redo (Ctrl + y)",
trigger: "body",
run: "press ctrl+y",
},
{
content: "Verify the image on the website is the second AI generated image",
trigger: ":iframe img[data-original-src*='AI%20Generated%202']",
},
...clickOnSave(),
{
content:
"Verify the final image on the website after save is the second AI generated image",
trigger: ":iframe img[data-original-src*='AI%20Generated%202']",
},
],
);