Files
magpie/internal/gui/tests/image-resolution.test.cjs
T
yetone f6a538ece9 images: a resolution tier, 1K, 2K or 4K, in Settings and the image MCP (skingstyle48487 on X)
Asking the image MCP for 4K through a relay came back at 2K: nothing
told the model how large to draw. Settings › Models › Images now has
Image resolution (Automatic, 1K, 2K, 4K; an app menu, saved as
imageResolution), and generate_image takes a resolution argument. The
agent's own resolution or WIDTHxHEIGHT size wins over the setting.

The images API takes `resolution` (or a tier given as size, "4K"), and
each vendor gets it in its own field:
- Gemini: imageConfig.imageSize (3.x image models; 2.x draws at 1K);
- chat image models (Gemini's): image_config.image_size;
- OpenRouter /images: resolution;
- Grok Imagine: resolution 1k or 2k (its largest);
- Seedream (Ark): the size tier the model has, or its pixels for a ratio;
- MiniMax H3: area;
- GPT Image 2 and other WxH images APIs: pixels at the ratio, within
  GPT Image 2's limits (edge 3840, 655,360 to 8,294,400 pixels, /16);
- gpt-image-1, DALL·E and SenseNova keep their own sizes.
A vendor that answers 400 or 422 to a tier is asked once more without
it. An unknown resolution is a 400. Both MCPs now say what size each
image came back at, and when that is under the tier asked.

The row is drawn by videogen.js, after Video generation's, so app.js
keeps its budget (it was at 1,088,641 to the byte); the server keeps
imageResolution when a save doesn't send it (TestSettingsImageResolution),
so prefsKeep doesn't carry it. The menu has no notes: "The model's own"
was a plugin's key already.

Run (magpie-desktop WSL on xiaomi-gpu; mote was over load 18 all day):
- gofmt -l: clean; go vet -tags nogui ./... and GOOS=windows go vet
  ./...: ok.
- go test ./internal/gateway -count=1 (1995s), ./internal/imagemcp,
  ./internal/settings, ./internal/settingsapi: ok; at this head go vet
  -tags nogui of gui, gateway and settingsapi, and gateway -run
  "Tier|ImagesAPIResolution|ImageMCP|Draw|Image", settingsapi and
  imagemcp: ok.
- New: draw_tier_test.go (each protocol's field, a fake upstream for
  the images API and the image MCP, the 400 retry, the precedence).
- At this head (on 88b65d758): go test -tags nogui ./internal/gui -run
  "Budget|I18n|SettingsImage": ok; node --test i18n-keys,
  image-resolution (22 of 22: chromium and webkit, en zh zh-TW ja de,
  1100 and 420 wide), video-gen-pick and old-webkit: 62 of 64. The 2 reds are
  old-webkit's "compat.js is the first..." (guideback.js) and "the
  styles hold up in Safari 15.0" (app.css :has()), the same on a clean
  copy of origin/main 88b65d758: #6570.
- Broken by hand: imagesAPISize returning the size as given, the retry
  off, chat image_size and OpenRouter resolution dropped, the image MCP
  ignoring the setting, the server's keep of imageResolution removed,
  a zh string removed: each turned its test red.

Not run yet: the full go test ./... and the whole GUI suite (WSL's one
suite slot was taken by another session); they run after this push.
Not tried against a real vendor.
2026-11-19 00:27:58 +08:00

137 lines
7.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Run with Node's test runner and Playwright on the module path; see README.md.
// Settings › Models › Images (skingstyle48487 on X: a 4K image asked through
// the image MCP came back at 2K): Image resolution is Automatic at first, a
// menu of the app's own (no native select) of Automatic, 1K, 2K and 4K;
// 4K saves imageResolution "4K" with the rest of the settings kept, and
// Automatic saves "". It is gone with image generation off, and another
// row's save keeps it. In English, Chinese, Traditional Chinese, Japanese
// and German, 1100 and 420 wide.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const { reach, watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const words = {
en: { name: "Image resolution", auto: "Automatic", sub: "when the agent names no size" },
zh: { name: "生图分辨率", auto: "自动", sub: "智能体没指定尺寸时" },
"zh-TW": { name: "生圖解析度", auto: "自動", sub: "代理沒指定尺寸時" },
ja: { name: "画像の解像度", auto: "自動", sub: "サイズを指定しないとき" },
de: { name: "Bildauflösung", auto: "Automatisch", sub: "keine Größe nennt" },
};
function serve(lang, saved, own) {
const settings = { lang, theme: "light", redact: true, imageGen: "", imageGenAuto: "codex/gpt-image-2",
imageGenModels: [{ id: "codex/gpt-image-2", name: "GPT Image 2", provider: "codex", providerName: "Codex", icon: "openai" }], ...own };
const state = { agents: [], profiles: [], settings };
return async (r) => {
const url = new URL(r.request().url());
const json = (data) => r.fulfill({ json: data });
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json(state);
if (url.pathname === "/api/settings") {
if (r.request().method() === "POST") {
saved.push(JSON.parse(r.request().postData()));
Object.assign(settings, saved.at(-1));
}
return json(settings);
}
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
if (url.pathname.startsWith("/api/")) return json({});
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
try { await r.fulfill({ body: await fs.readFile(file), contentType }); } catch { await r.fulfill({ status: 404, body: "" }); }
};
}
async function open(engine, lang, width, saved, own, t) {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 900 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, saved, own));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=models");
await settingsDrawn(page); // rows found are the ones that stay (#1659)
await page.locator("#imagesList #imageTimeoutRow").waitFor();
return { page, errors };
}
const waitSaved = async (page, saved, n) => {
for (let i = 0; i < 200 && saved.length < n; i++) await page.waitForTimeout(50);
};
async function pick(page, name) {
const b = page.locator("#imageResolutionRow .sess-pick");
await reach(page, b);
await b.click();
const menu = page.locator(".proto-menu");
await menu.waitFor();
const item = menu.locator(".pm-item", { has: page.locator(".pm-name", { hasText: new RegExp(`^${name}$`) }) });
await item.click();
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) for (const width of [1100, 420]) {
const w = words[lang];
test(`${engine} ${lang} ${width}: how large an image is drawn`, async (t) => {
const saved = [];
const { page, errors } = await open(engine, lang, width, saved, {}, t);
const row = page.locator("#imagesList #imageResolutionRow");
await row.waitFor();
assert.equal(await row.locator(".name").textContent(), w.name);
assert((await row.locator(".sub").textContent()).includes(w.sub));
assert.equal(await row.evaluate((r) => r.nextElementSibling?.id), "imageTimeoutRow", "above the timeout");
assert.equal(await row.locator("select").count(), 0, "no native select");
assert.equal((await row.locator(".sess-pick").textContent()).trim(), w.auto, "Automatic at first");
const fit = await row.locator(".sess-pick").evaluate((e) => e.getBoundingClientRect().right <= innerWidth + 1);
assert(fit, `the picker fits at ${width}`);
const b = row.locator(".sess-pick");
await reach(page, b);
await b.click();
const menu = page.locator(".proto-menu");
await menu.waitFor();
assert.deepEqual(await menu.locator(".pm-name").allTextContents(), [w.auto, "1K", "2K", "4K"]);
assert.deepEqual(await menu.locator(".pm-item .pm-note").allTextContents(), ["", "", "", ""], "no notes, the names say it");
assert.equal(await menu.locator(".pm-item[aria-checked=true] .pm-name").textContent(), w.auto);
const inside = await menu.evaluate((m) => { const r = m.getBoundingClientRect(); return r.left >= -1 && r.right <= innerWidth + 1; });
assert(inside, `the menu fits at ${width}`);
await menu.locator(".pm-item", { has: page.locator(".pm-name", { hasText: /^4K$/ }) }).click();
await waitSaved(page, saved, 1);
assert.equal(saved.length, 1, "saved once");
assert.equal(saved[0].imageResolution, "4K");
assert.equal(saved[0].redact, true, "the rest kept");
assert.equal(saved[0].lang, lang);
await page.waitForFunction(() => document.querySelector("#imageResolutionRow .sess-pick")?.textContent.trim() === "4K", null, { timeout: 10000 });
await pick(page, w.auto);
await waitSaved(page, saved, 2);
assert.equal(saved.length, 2, JSON.stringify(saved));
assert.equal(saved[1].imageResolution, "");
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth + 1), `no sideways scroll at ${width}px`);
assert.deepEqual(errors, []);
});
}
test(`${engine}: other saves leave the tier set alone, and the row goes with image generation off`, async (t) => {
const saved = [];
const { page, errors } = await open(engine, "en", 1100, saved, { imageResolution: "2K" }, t);
assert.equal((await page.locator("#imageResolutionRow .sess-pick").textContent()).trim(), "2K");
await page.evaluate(() => savePrefs({ ...prefsKeep(prefs), redact: false }));
await waitSaved(page, saved, 1);
// the server keeps what a save doesn't send (TestSettingsImageResolution)
assert(!("imageResolution" in saved[0]), JSON.stringify(saved));
const off = [];
const o = await open(engine, "en", 1100, off, { imageGen: "off" }, t);
assert.equal(await o.page.locator("#imageResolutionRow").count(), 0);
assert.deepEqual([...errors, ...o.errors], []);
});
}