Files
magpie/internal/gui/tests/sync-follow-server.test.cjs
T
yetone 281a7e024f sync: a computer can follow the server, taking its setup and sending none (Ramon317317 on X)
Ramon317317 asked for a main profile: one computer the others take their
setup from, so they never drift apart. Sync merged both ways only;
Restore is a one-time pull and an agent's own fields cover agents only.

A computer can now be set to follow the server (Settings -> Sync -> This
computer: Both ways / Follows the server; /api/davsync/follow-server;
`magpie webdav|s3|github follow on|off`). It is chosen per computer and
kept in sync.json (Config.FollowServer, kept by Configure like AutoEvery).
A following computer:
- takes every changed part from the file, whichever side is newer;
- never writes the file: no push, no merged sign-in, no rebuild of a
  damaged file;
- keeps a change made on it in the sync folder, as a join does, and the
  notice says it was replaced because this computer follows the server
  (Notice.Followed; GUI and CLI);
- still keeps an unset part on the server from emptying its own, and own
  agent fields and agents only here;
- refuses to follow a server with no file yet, sending nothing.
The computer followed stays two-way; nothing changes for anyone who
doesn't pick it. Usage sharing is unchanged. docs/subsystems/sync-backup.md
says so.

app.js stays inside the core script budget (TestCoreScriptBudget) with
no raise: renderSync's comments are cut down to pay for the row.

Run (mote, at this head):
- go build ./...; go vet ./... (darwin), GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./...; gofmt -l clean.
- go test ./internal/davsync ./internal/cli ./internal/gui: ok (cli
  with the route guard fix committed beside this one).
  TestFollowServer (empty server, join, own change replaced and kept,
  both changed, unchanged sync, Configure keeps it, back to both ways)
  and TestFollowServerSendsNoSignIn are new.
- GUI (node --test, Chromium and WebKit): sync-follow-server (new; en and
  zh, 900px and 400px: name, sub, picked segment, the post, the notice,
  no scroll, no overflow, no select, re-pick posts nothing), plus
  sync-auto-restore, sync-note-show, sync-own-fields, sync-not-backup,
  sync-other-kind, sync-signins, sync-spin, i18n-keys, i18n-en-no-cjk,
  gui-zh-tw, gui-ja, gui-de: all pass.

Broken by hand:
- push guard removed: TestFollowServerSendsNoSignIn red ("sent its
  sign-in: 1 puts").
- follow branch off: TestFollowServer red ("b's own change stayed:
  [deepseek=k1 kimi=kb2]").
- the row's click not posting: sync-follow-server red, 8 of 8.

Not tried: a real WebDAV, S3 or GitHub server (fake servers only); two
real computers; Windows run of the tests (vet only). The full suite starts
after the tag.
2026-11-10 15:43:41 +08:00

138 lines
7.6 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A computer can follow the server: take its setup and send none of its
// own (Ramon317317 on X: one computer the others all take theirs from).
// With sync on, the This computer row in the sync list shows Both ways
// picked, and says changes go both ways. Picking Follows the server posts
// follow-server on, and the row then says changes made here are replaced
// and kept. A notice from a sync that replaced a change made here says it
// was because this computer follows the server. Picking Both ways again
// posts it off. No click moves the page; nothing is wider than the window
// at a phone's width. In English and Chinese, Chromium and WebKit. The API
// is faked; nothing is synced.
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 { openPart } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
function serve(lang, posts) {
const settings = {
theme: "light", lang, tray: "panel", version: "test", dir: "/tmp/magpie",
gateway: "http://127.0.0.1:3425", visionModels: [], imageGenModels: [],
fx: { rate: 7.2, stale: false },
};
let sync = { on: true, kind: "webdav", url: "https://dav.jianguoyun.com/dav/", user: "me", passwordSet: true, passphraseSet: true,
keys: true, agents: true, library: true, auto: 3, last: new Date().toISOString() };
return async (route) => {
const request = route.request();
const pathname = new URL(request.url()).pathname;
const json = (data) => route.fulfill({ json: data });
if (pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:false};` });
if (pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (pathname === "/api/state") return json({ agents: [], profiles: [], settings });
if (pathname === "/api/settings") return json(settings);
if (pathname === "/api/usage/quotas") return json([]);
if (pathname === "/api/groups") return json({ groups: [], models: [] });
if (pathname === "/api/plugins") return json({ plugins: [] });
if (pathname === "/api/davsync") return json(sync);
if (pathname.startsWith("/api/davsync/")) {
const action = pathname.slice("/api/davsync/".length);
posts.push({ action, body: request.postDataJSON() });
if (action === "follow-server") {
const on = request.postDataJSON().on;
sync = { ...sync, followServer: on || undefined,
notice: on ? { at: new Date().toISOString(), here: ["providers"], followed: true, saved: "/tmp/magpie/sync" } : undefined };
}
return json(sync);
}
if (pathname.startsWith("/api/")) return json({});
const file = path.join(assets, pathname === "/" ? "index.html" : pathname);
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
return route.fulfill({ body: await fs.readFile(file), contentType });
};
}
const words = {
en: {
row: "This computer", both: "Both ways", follow: "Follows the server",
bothSub: "Changes go to the other computers, and theirs come here",
followSub: "Takes the server's setup and sends none. A change made here is replaced and kept in the sync folder.",
note: "Replaced by the server's, as this computer follows it: providers",
},
zh: {
row: "本机", both: "双向同步", follow: "跟随服务器",
bothSub: "本机的改动同步到其他电脑,其他电脑的改动也同步到本机",
followSub: "只采用服务器上的配置,不上传本机的。本机的改动会被替换,副本保存在同步文件夹中。",
note: "本机跟随服务器,本机改过的供应商已被服务器上的替换",
},
};
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
for (const width of [900, 400]) {
const w = words[lang];
test(`${engine} ${lang} ${width}px: this computer can follow the server, and sync both ways again`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
const page = await (await browser.newContext({ viewport: { width, height: 700 }, reducedMotion: "reduce" })).newPage();
const errors = [], posts = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, posts));
t.after(async () => {
if (process.env.ARTIFACT_DIR) {
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}-sync-follow-server.png`), fullPage: true });
}
await browser.close();
});
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
await page.goto("http://magpie.test/");
await page.locator("#prefs").click();
await openPart(page, "sync");
const list = page.locator("#syncList");
const row = list.locator(".row.pref").filter({ has: page.locator(".sync-follows") });
await row.waitFor({ state: "visible" });
assert.equal(await row.locator(".name").textContent(), w.row);
assert.equal(await row.locator(".sub").textContent(), w.bothSub);
assert.equal(await row.locator(".segs .opt.on").textContent(), w.both);
await row.scrollIntoViewIfNeeded();
await page.waitForTimeout(300);
const top = () => page.evaluate(() => document.querySelector("#view-settings").scrollTop);
// follow: posted on, said, and a sync's notice says why
let at = await top();
await row.locator(".segs .opt", { hasText: w.follow }).click();
await page.waitForFunction((s) => document.querySelector("#syncList .sub.sync-follows")?.textContent === s, w.followSub);
assert.deepEqual(posts.at(-1), { action: "follow-server", body: { on: true } });
assert.equal(await row.locator(".segs .opt.on").textContent(), w.follow);
assert.equal(await list.locator(".sync-note .sub").first().textContent(), w.note);
assert.equal(await top(), at, "picking Follows the server didn't move the page");
const wide = await page.evaluate(() => {
const r = document.querySelector("#syncList .sub.sync-follows").closest(".row").getBoundingClientRect();
return r.right - document.documentElement.clientWidth;
});
assert.ok(wide <= 1, `the row is ${wide}px wider than the window`);
// picked again: nothing posted
const n = posts.length;
await row.locator(".segs .opt", { hasText: w.follow }).click();
await page.waitForTimeout(200);
assert.equal(posts.length, n, "picking the choice already made posts nothing");
// both ways again
at = await top();
await row.locator(".segs .opt", { hasText: w.both }).click();
await page.waitForFunction((s) => document.querySelector("#syncList .sub.sync-follows")?.textContent === s, w.bothSub);
assert.deepEqual(posts.at(-1), { action: "follow-server", body: { on: false } });
assert.equal(await top(), at, "picking Both ways didn't move the page");
assert.equal(await list.locator("select").count(), 0, "no native select");
assert.deepEqual(errors, []);
});
}
}
}