Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions desktop/agent_preset_icon_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"bytes"
"encoding/base64"
"image"
"image/png"
"testing"

"deepzora/internal/agentpreset"
)

func TestAgentPresetCustomIconAuthoring(t *testing.T) {
a := newManualSessionTestApp(t)
var data bytes.Buffer
if err := png.Encode(&data, image.NewNRGBA(image.Rect(0, 0, 128, 128))); err != nil {
t.Fatal(err)
}
preset := agentpreset.Default()
preset.IconImage = "data:image/png;base64," + base64.StdEncoding.EncodeToString(data.Bytes())
saved, err := a.SaveAgentPreset("", "user", "avatar", agentpreset.Render(preset))
if err != nil || saved.IconImage != preset.IconImage {
t.Fatalf("save did not return the custom image: %v", err)
}
list, err := a.ListAgentPresets("")
if err != nil || len(list.Presets) != 2 || list.Presets[1].IconImage != preset.IconImage {
t.Fatalf("list did not return the persisted custom image: %v", err)
}
if _, err := a.SaveAgentPreset("", "user", "avatar-copy", agentpreset.Render(saved)); err != nil {
t.Fatal(err)
}
if err := a.DeleteAgentPreset("", "user", "avatar"); err != nil {
t.Fatal(err)
}
list, err = a.ListAgentPresets("")
if err != nil || len(list.Presets) != 2 || list.Presets[1].IconImage != preset.IconImage {
t.Fatalf("copy lost its image when the original was deleted: %v", err)
}
}
14 changes: 7 additions & 7 deletions desktop/electron/scripts/agent-presets-smoke.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ try {
assert.equal(await editor().getByLabel("ID", { exact: true }).inputValue(), "default-copy");
await editor().getByLabel("Name", { exact: true }).fill("Native reviewer");
await editor().getByLabel("ID", { exact: true }).fill("native-reviewer");
await editor().getByLabel("Description", { exact: true }).fill("Native preset acceptance");
await editor().getByLabel("Persona body", { exact: true }).fill("NATIVE_PERSONA_FIRST");
await editor().getByLabel("Complete system prompt", { exact: true }).check();
await editor().getByLabel("Include runtime context", { exact: true }).uncheck();
assert.equal(await editor().getByRole("button", { name: "Keep", exact: true }).isDisabled(), true);
await editor().getByLabel("Restrict available tools", { exact: true }).check();
await editor().getByLabel("Summary (optional)", { exact: true }).fill("Native preset acceptance");
await editor().getByRole("radio", { name: "Custom complete system prompt", exact: true }).check();
await editor().getByLabel("System prompt (required)", { exact: true }).fill("NATIVE_PERSONA_FIRST");
await editor().locator(".agent-preset-advanced summary").click();
await editor().getByRole("switch", { name: "Include runtime context", exact: true }).uncheck();
await editor().getByRole("switch", { name: "Restrict available tools", exact: true }).check();
await page.screenshot({ path: join(home, "preset-editor.png") });
await editor().getByRole("button", { name: "Save", exact: true }).click();
await row("Native reviewer").waitFor();
Expand Down Expand Up @@ -170,7 +170,7 @@ try {
check("first native turn sends only the complete body, required cwd and admitted schemas, then locks the picker");
await settings();
await row("Native reviewer").getByRole("button", { name: "Edit", exact: true }).click();
await editor().getByLabel("Persona body", { exact: true }).fill("NATIVE_PERSONA_LATEST");
await editor().getByLabel("System prompt (required)", { exact: true }).fill("NATIVE_PERSONA_LATEST");
await editor().getByRole("button", { name: "Save", exact: true }).click();
await row("Native reviewer").waitFor();
await composer();
Expand Down
168 changes: 168 additions & 0 deletions desktop/frontend/bench/agent-presets.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import assert from "node:assert/strict";
import { mkdir } from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { createServer } from "vite";
import { newSessionButton } from "./app-page-actions.mjs";

const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
process.env.PLAYWRIGHT_BROWSERS_PATH ??= path.join(root, ".pw-browsers");
const { chromium } = await import("playwright");
const server = await createServer({ root, logLevel: "error", server: { host: "127.0.0.1", port: 0, hmr: false } });
await server.listen();
let browser;
try {
browser = await chromium.launch({ headless: true, executablePath: process.env.CHROME_EXECUTABLE });
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 }, locale: "en-US" });
const errors = [];
page.on("pageerror", error => errors.push(error.message));
await page.goto(`http://127.0.0.1:${server.httpServer.address().port}/?mock=demo&bench=1`);
await page.locator("#composer-input").waitFor();
await newSessionButton(page).click();
const picker = page.getByRole("button", { name: "Agent preset: Default", exact: true });
await picker.focus();
await picker.press("ArrowDown");
await page.getByRole("menu", { name: "Agent preset: Default", exact: true }).waitFor();
await page.waitForFunction(() => document.activeElement?.getAttribute("role") === "menuitemradio");
await page.keyboard.press("End");
assert.equal(await page.evaluate(() => document.activeElement.textContent), "New agent preset…");
await page.keyboard.press("Enter");
const editor = page.locator(".agent-preset-editor");
await editor.waitFor();
assert.equal(await editor.getAttribute("aria-label"), "New preset");
await page.waitForFunction(() => document.activeElement?.id.endsWith("-name"));
console.log("PASS picker action opens the creation form and focuses its name");
await editor.getByLabel("Name", { exact: true }).fill("Custom avatar");

const modes = editor.getByRole("radiogroup", { name: "Prompt mode", exact: true });
assert.equal(await modes.getByRole("radio").count(), 3);
assert.equal(await editor.getByRole("switch", { name: "Include runtime context", exact: true }).isVisible(), false);
await modes.getByRole("radio", { name: "Custom complete system prompt", exact: true }).check();
assert.equal(await editor.getByRole("button", { name: "Save", exact: true }).isDisabled(), true, "empty complete prompt blocks save");
await editor.getByLabel("System prompt (required)", { exact: true }).fill("Retain this draft across modes.");
await modes.getByRole("radio", { name: "Replace coding instructions", exact: true }).check();
assert.equal(await editor.getByLabel("Coding instructions (required)").inputValue(), "Retain this draft across modes.");
await editor.locator(".agent-preset-advanced summary").press("Enter");
const context = editor.getByRole("switch", { name: "Include runtime context", exact: true });
await context.press("Space");
assert.equal(await context.isChecked(), false, "context switch is keyboard accessible");
await modes.getByRole("radio", { name: "Extend default instructions (recommended)", exact: true }).check();
assert.equal(await context.isChecked(), false, "mode changes retain runtime setting");
await editor.getByLabel("Role instructions (optional)").fill("");
assert.equal(await editor.getByRole("button", { name: "Save", exact: true }).isEnabled(), true);
await context.check();
await editor.locator(".agent-preset-advanced summary").press("Enter");
const restrict = editor.getByRole("switch", { name: "Restrict available tools", exact: true });
await restrict.press("Space");
await editor.getByRole("button", { name: "Choose tools (0 selected)", exact: true }).waitFor();
await restrict.uncheck();
assert.equal(await editor.getByRole("button", { name: "Choose tools (0 selected)", exact: true }).count(), 0);
console.log("PASS three prompt modes, dynamic required body, advanced disclosure and keyboard switches");
assert.equal(await editor.getByLabel("ID", { exact: true }).inputValue(), "custom-avatar");
await editor.getByLabel("ID", { exact: true }).fill("avatar-manual");
await editor.getByLabel("Name", { exact: true }).fill("Custom avatar renamed");
assert.equal(await editor.getByLabel("ID", { exact: true }).inputValue(), "avatar-manual", "manual ID survives name changes");
await editor.getByLabel("Name", { exact: true }).fill("Custom avatar");

const png = await page.evaluate(() => {
const canvas = document.createElement("canvas"); canvas.width = 400; canvas.height = 200;
const context = canvas.getContext("2d"); context.fillStyle = "red"; context.fillRect(0, 0, 200, 200);
context.fillStyle = "blue"; context.fillRect(200, 0, 200, 200);
return canvas.toDataURL("image/png").split(",")[1];
});
const upload = page.locator('.agent-preset-icon-picker input[type="file"]');
const file = { name: "avatar.png", mimeType: "image/png", buffer: Buffer.from(png, "base64") };
await upload.setInputFiles(file);
await page.getByRole("group", { name: "Crop icon", exact: true }).waitFor();
assert.equal(await editor.getByRole("button", { name: "Save", exact: true }).isDisabled(), true, "save waits for a crop decision");
await editor.getByLabel("Zoom", { exact: true }).fill("2");
await editor.getByLabel("Horizontal position").fill("0");
await page.waitForFunction(() => document.querySelector('.agent-preset-icon-crop canvas')?.getContext("2d").getImageData(96, 96, 1, 1).data[0] === 255);
await page.getByRole("button", { name: "Use icon", exact: true }).click();
console.log("PASS square crop applied");
await page.waitForFunction(() => document.querySelector('.agent-preset-editor [aria-label="Custom icon"] img')?.naturalWidth === 128);
await editor.getByRole("button", { name: "Save", exact: true }).click();
const row = page.getByRole("article", { name: "Custom avatar", exact: true });
await row.waitFor();
console.log("PASS image saved and listed");
const stored = await row.locator("img").getAttribute("src");
assert.match(stored, /^data:image\/png;base64,/u);
assert.ok(stored.length < 128 * 1024);
await row.getByRole("button", { name: "Copy", exact: true }).click();
assert.equal(await page.locator('[aria-label="Custom icon"] img').getAttribute("src"), stored, "copy retains embedded image");
await page.getByRole("button", { name: "Cancel", exact: true }).click();
await page.locator("#management-back").click();
await picker.click();
const option = page.getByRole("menuitemradio", { name: /Custom avatar/u });
assert.equal(await option.locator("img").getAttribute("src"), stored, "picker and list use the same persisted image");
await option.click();
await page.getByRole("button", { name: "Agent preset: Custom avatar", exact: true }).waitFor();
console.log("PASS saved image renders in the composer picker");
assert.equal(await page.locator(".agent-preset-choice .composer-choice img").getAttribute("src"), stored);
await page.getByRole("button", { name: "Agent preset: Custom avatar", exact: true }).click();
await page.getByRole("menuitem", { name: "New agent preset…", exact: true }).click();
await editor.waitFor();
await editor.getByRole("button", { name: "Cancel", exact: true }).click();
assert.equal(await editor.count(), 0, "consumed create request does not reopen after cancel");
await row.getByRole("button", { name: "Edit", exact: true }).click();

for (const [badFile, message] of [
[{ name: "vector.svg", mimeType: "image/svg+xml", buffer: Buffer.from("<svg/>") }, "Choose a PNG, JPEG or WebP image."],
[{ name: "huge.png", mimeType: "image/png", buffer: Buffer.alloc(5 * 1024 * 1024 + 1) }, "Choose an image smaller than 5 MB."],
[{ name: "broken.png", mimeType: "image/png", buffer: Buffer.from("broken") }, "Could not read this image. Try another file."],
]) {
await upload.setInputFiles(badFile);
await page.getByRole("alert").filter({ hasText: message }).waitFor();
assert.equal(await page.locator('[aria-label="Custom icon"] img').getAttribute("src"), stored, "invalid upload retains the saved image");
}
await upload.setInputFiles(file);
await page.getByRole("button", { name: "Use icon", exact: true }).waitFor();
await page.locator(".agent-preset-icon-crop").getByRole("button", { name: "Cancel", exact: true }).click();
assert.equal(await page.locator('[aria-label="Custom icon"] img').getAttribute("src"), stored, "cancelling crop keeps previous image");
await upload.setInputFiles(file);
await editor.getByLabel("Zoom", { exact: true }).fill("2");
await editor.getByLabel("Horizontal position").fill("100");
await page.getByRole("button", { name: "Use icon", exact: true }).click();
assert.notEqual(await page.locator('[aria-label="Custom icon"] img').getAttribute("src"), stored, "replacement applies the new crop");
const evidence = process.env.DEEPZORA_PRESET_EVIDENCE;
if (evidence) {
await mkdir(evidence, { recursive: true });
await page.screenshot({ path: path.join(evidence, "custom-icon-editor.png"), fullPage: true });
}
await page.getByRole("button", { name: "Restore default icon", exact: true }).click();
assert.equal(await page.locator('[aria-label="Custom icon"]').count(), 0);
assert.equal(await page.getByRole("radio", { name: "bot", exact: true }).getAttribute("aria-checked"), "true");

// A late decode must not undo a subsequent built-in icon choice.
await page.evaluate(() => {
const decode = window.createImageBitmap.bind(window);
window.createImageBitmap = file => new Promise(resolve => { window.releasePresetImage = async () => resolve(await decode(file)); });
});
await upload.setInputFiles(file);
await page.getByText("Reading image…", { exact: true }).waitFor();
await page.getByRole("radio", { name: "search", exact: true }).click();
await page.evaluate(() => window.releasePresetImage());
assert.equal(await page.locator(".agent-preset-icon-crop").count(), 0, "late decode cannot reopen crop");
assert.equal(await page.getByRole("radio", { name: "search", exact: true }).getAttribute("aria-checked"), "true");
await editor.getByRole("radiogroup", { name: "Icon color", exact: true }).getByRole("radio", { name: "Blue", exact: true }).click();
await page.getByRole("form").getByRole("button", { name: "Save", exact: true }).click();
await row.waitFor();
assert.equal(await row.locator("img").count(), 0, "reset and built-in choice persist");
assert.equal(await row.locator(".agent-preset-row__icon svg").evaluate(node => getComputedStyle(node).color), "rgb(96, 165, 250)", "chosen color persists in list");
await row.getByRole("button", { name: "Copy", exact: true }).click();
assert.equal(await editor.getByRole("radiogroup", { name: "Icon color", exact: true }).getByRole("radio", { name: "Blue", exact: true }).getAttribute("aria-checked"), "true", "copy retains color");
await editor.getByRole("button", { name: "Random", exact: true }).click();
assert.equal(await editor.locator('.agent-preset-editor__icons [aria-checked="true"]').count(), 1, "random chooses one built-in glyph");
assert.equal(await editor.locator('.agent-preset-icon-picker__colors [aria-checked="true"]').count(), 1, "random chooses one palette color");
if (evidence) {
await page.screenshot({ path: path.join(evidence, "icon-grid-prompt-modes.png"), fullPage: true });
await page.setViewportSize({ width: 800, height: 1000 });
await page.waitForFunction(() => document.documentElement.scrollWidth <= innerWidth);
await page.screenshot({ path: path.join(evidence, "preset-compact.png"), fullPage: true });
}
assert.deepEqual(errors, []);
console.log("PASS preset picker creation, keyboard navigation, image crop/save/copy/readback, upload validation, cancel/reset and stale decode");
} finally {
await browser?.close();
await server.close();
}
1 change: 1 addition & 0 deletions desktop/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"test:composer": "node scripts/run-ci-tests.mjs --group composer",
"test:composer-browser": "node bench/composer-transcript-stability.mjs",
"test:popover-browser": "node bench/anchored-popover-lifecycle.mjs",
"test:agent-presets-browser": "node bench/agent-presets.mjs",
"test:popover-electron": "node bench/anchored-popover-lifecycle.mjs --electron",
"test": "node scripts/run-ci-tests.mjs",
"test:performance": "node scripts/run-ci-tests.mjs --group performance",
Expand Down
Loading
Loading