Files
dbx-main/tests/js/perf_auto_refresh.browser.test.js
king 18d2835304 feat(performance): 자동 업데이트가 끝나면 매출 실적·광고비 화면 새로 고침
- 화면이 20초마다 GET /api/update-status?kind=sales|ads 로 마지막으로 끝난 업데이트를 보고,
  그린 때(update_watch.seen)와 다르고 도는 중이 아니면 reload (jobs.last_finished — 메모리만, DB 안 읽음)
- 보는 사람이 직접 누른 업데이트는 제외(진행 창이 새로 그림), 매출·광고비 따로
- 칸 입력·창·메뉴·확인 창이 열려 있으면 닫힐 때까지 기다리고 "새 데이터 있음" 안내, 탭이 안 보이면 안 물음
- 테스트: last_finished·update-status API, 브라우저(새로 고침·기다림·종류별)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 18:37:39 +09:00

111 lines
6.0 KiB
JavaScript

// 실적 화면 자동 새로 고침(2026-10-26) — 실제 브라우저(Chromium).
// python tests/js/render_performance.py
// NODE_PATH=<playwright 경로> PJ_TEST_BROWSER=<Chrome 경로> node tests/js/perf_auto_refresh.browser.test.js
// 자동 업데이트(다른 사람 업데이트 포함)가 끝나면 매출 실적·광고비 화면이 20초 안에 알아채 새로 고친다(GET /api/update-status).
// 도는 중이면 끝날 때까지 기다림, 칸 입력·창이 열려 있으면 닫힐 때까지 기다림(안내 표시), 매출/광고비 종류 따로.
const fs = require("fs"), path = require("path");
const { chromium } = require("playwright");
const ROOT = path.join(__dirname, "..", "..");
const fails = [];
function check(name, cond, extra) {
if (cond) console.log("PASS " + name);
else { console.log("FAIL " + name + (extra !== undefined ? " → " + JSON.stringify(extra) : "")); fails.push(name); }
}
function page(file) {
const css = fs.readFileSync(path.join(ROOT, "app", "static", "performance.css"), "utf8");
const js = fs.readFileSync(path.join(ROOT, "app", "static", "performance.js"), "utf8");
return fs.readFileSync(path.join(__dirname, ".out", file), "utf8")
.replace(/<link rel="stylesheet" href="\/static\/performance\.css[^"]*" \/>/, function () { return "<style>" + css + "</style>"; })
.replace(/<script src="\/static\/performance\.js[^"]*" defer><\/script>/, function () { return "<script>" + js + "</script>"; });
}
(async function () {
const browser = await chromium.launch(process.env.PJ_TEST_BROWSER ? { executablePath: process.env.PJ_TEST_BROWSER } : {});
try {
async function open(file, url) {
const ctx = await browser.newContext({ viewport: { width: 1400, height: 900 } });
const p = await ctx.newPage();
const s = { loads: 0, asks: [], status: { running: false, done: "job-old" } };
await p.clock.install({ time: new Date("2026-10-07T10:17:25+09:00") });
await p.route("**/*", function (r) {
const u = r.request().url();
if (u.indexOf("/performance/api/update-status") >= 0) {
s.asks.push(u.split("?")[1]);
return r.fulfill({ status: 200, contentType: "application/json", body: JSON.stringify(Object.assign({ kind: "sales", user: "자동 업데이트" }, s.status)) });
}
if (u.indexOf("/static/") >= 0) return r.fulfill({ status: 200, body: "" });
if (r.request().resourceType() === "document") s.loads++;
r.fulfill({ status: 200, contentType: "text/html; charset=utf-8", body: page(file) });
});
await p.goto(url);
return { ctx: ctx, p: p, s: s };
}
const tick = async function (o, ms) { await o.p.clock.runFor(ms); await o.p.waitForTimeout(150); };
// ── 매출 실적 ──
let o = await open("perf_dash.html", "http://perf.test/performance/");
await tick(o, 21000);
check("20초마다 묻는다(매출)", o.s.asks.length === 1 && o.s.asks[0] === "kind=sales", o.s.asks);
check("그린 때와 같으면 그대로", o.s.loads === 1, o.s.loads);
o.s.status = { running: true, done: "job-new" };
await tick(o, 20000);
check("업데이트가 도는 중이면 기다림", o.s.loads === 1, o.s.loads);
o.s.status = { running: false, done: "job-new" };
await tick(o, 20000);
await o.p.waitForLoadState();
check("자동 업데이트 끝 → 새로 고침", o.s.loads === 2, o.s.loads);
// 새로 그린 화면(이 테스트는 같은 HTML — seen=job-old) — 창이 열려 있으면 닫을 때까지 기다림
o.s.status = { running: false, done: "job-old" };
await tick(o, 21000);
check("새로 고친 뒤 같은 값이면 그대로", o.s.loads === 2, o.s.loads);
await o.p.evaluate(function () {
const m = document.createElement("div"); m.className = "perf-modal"; m.id = "fake-modal"; document.body.appendChild(m);
});
o.s.status = { running: false, done: "job-2" };
await tick(o, 21000);
check("창이 열려 있으면 새로 고치지 않음", o.s.loads === 2, o.s.loads);
const note = await o.p.textContent("#perf-last-update");
check("안내: 창을 닫으면 새로 고침", /새 데이터 있음/.test(note), note);
await tick(o, 5000);
check("창이 열린 동안은 계속 기다림", o.s.loads === 2, o.s.loads);
await o.p.evaluate(function () { document.getElementById("fake-modal").remove(); });
await tick(o, 2000);
await o.p.waitForLoadState();
check("창을 닫으면 새로 고침", o.s.loads === 3, o.s.loads);
// 칸 입력 중(포커스)이면 기다림
o.s.status = { running: false, done: "job-3" };
await o.p.evaluate(function () {
const i = document.createElement("input"); i.id = "fake-input"; document.querySelector("#perf-dash").appendChild(i); i.focus();
});
await tick(o, 21000);
check("입력 중이면 새로 고치지 않음", o.s.loads === 3, o.s.loads);
await o.p.evaluate(function () { document.getElementById("fake-input").blur(); });
await tick(o, 2000);
await o.p.waitForLoadState();
check("입력을 마치면 새로 고침", o.s.loads === 4, o.s.loads);
await o.ctx.close();
// ── 광고비 — 광고비 업데이트를 따로 본다 ──
o = await open("perf_ads.html", "http://perf.test/performance/ads");
await tick(o, 21000);
check("광고비 화면은 kind=ads", o.s.asks[0] === "kind=ads", o.s.asks);
o.s.status = { running: false, done: "job-ads" };
await tick(o, 20000);
await o.p.waitForLoadState();
check("광고비 자동 업데이트 끝 → 새로 고침", o.s.loads === 2, o.s.loads);
await o.ctx.close();
// ── 자동 새로 고침 정보가 없는 화면(예전 서버 등) — 묻지 않음 ──
o = await open("perf_dash_groups.html", "http://perf.test/performance/");
await tick(o, 45000);
check("update_watch 없으면 묻지 않음", o.s.asks.length === 0, o.s.asks);
await o.ctx.close();
} finally {
await browser.close();
}
console.log(fails.length ? "\n" + fails.length + " FAILED" : "\nALL PASS");
process.exit(fails.length ? 1 : 0);
})();