bd0c47e6fc
Requires asset_db initialized with scripts/sql/asset_db_init.sql and ASSET_DB_URL configured before enabling storage.
532 lines
17 KiB
JavaScript
532 lines
17 KiB
JavaScript
// Actual Chrome, real ERP template/CSS/JS, synthetic assets and controlled APIs.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const http = require("node:http");
|
|
const path = require("node:path");
|
|
const { chromium } = require("playwright");
|
|
const ROOT = path.resolve(__dirname, "../.."),
|
|
OUT = path.join(__dirname, ".out"),
|
|
STATIC = path.join(ROOT, "app/static");
|
|
const parsed = JSON.parse(
|
|
fs.readFileSync(path.join(OUT, "asset_report.json"), "utf8"),
|
|
);
|
|
let conflict = false,
|
|
slow = false,
|
|
mutations = [],
|
|
counter = 30;
|
|
const timestamp = "2026-10-06T01:00:00+09:00";
|
|
const records = Array.from({ length: 30 }, (_, i) => ({
|
|
id: "asset" + i,
|
|
tag: "AST-" + String(i + 1).padStart(6, "0"),
|
|
name: i === 0 ? "KING-DESKTOP" : `테스트 자산 ${i}`,
|
|
category: i % 3 === 0 ? "computer" : i % 3 === 1 ? "chair" : "mouse",
|
|
status: "available",
|
|
manufacturer: "",
|
|
model: i === 0 ? parsed.summary.motherboard : "",
|
|
serial: "",
|
|
location: "본사 3층",
|
|
department: "디자인팀",
|
|
assignee_email: "",
|
|
purchased_on: "2026-01-01",
|
|
purchase_cost: "1250000",
|
|
vendor: "",
|
|
warranty_until: "2026-10-20",
|
|
notes: "",
|
|
latest_report_id: i === 0 ? "report1" : null,
|
|
version: 1,
|
|
created_at: timestamp,
|
|
updated_at: timestamp,
|
|
specs: i === 0 ? parsed.summary : null,
|
|
}));
|
|
const users = [
|
|
{ email: "reader@test.local", name: "담당자" },
|
|
{ email: "staff@test.local", name: "직원" },
|
|
];
|
|
const events = [
|
|
{
|
|
id: 1,
|
|
asset_id: "asset0",
|
|
actor: "reader@test.local",
|
|
kind: "create",
|
|
note: "자산 등록",
|
|
changes: {},
|
|
created_at: timestamp,
|
|
},
|
|
];
|
|
const reports = [
|
|
{
|
|
id: "report1",
|
|
asset_id: "asset0",
|
|
filename: "KING-DESKTOP.XML",
|
|
uploaded_at: timestamp,
|
|
actor: "reader@test.local",
|
|
summary: parsed.summary,
|
|
scan_time: parsed.scan_time,
|
|
},
|
|
];
|
|
function json(res, value, code = 200) {
|
|
res.writeHead(code, { "Content-Type": "application/json" });
|
|
res.end(JSON.stringify(value));
|
|
}
|
|
function filter(p) {
|
|
let rows = records.filter(
|
|
(a) => !p.get("category") || a.category === p.get("category"),
|
|
);
|
|
for (const key of ["status", "location"])
|
|
rows = rows.filter((a) => !p.get(key) || a[key] === p.get(key));
|
|
if (p.get("assignee"))
|
|
rows = rows.filter((a) => a.assignee_email === p.get("assignee"));
|
|
if (p.get("group"))
|
|
rows = rows.filter(
|
|
(a) =>
|
|
(p.get("group") === "furniture") ===
|
|
["chair", "desk", "furniture"].includes(a.category),
|
|
);
|
|
if (p.get("q"))
|
|
rows = rows.filter((a) => (a.name + a.tag + a.notes).includes(p.get("q")));
|
|
return rows;
|
|
}
|
|
const server = http.createServer(async (req, res) => {
|
|
const url = new URL(req.url, "http://localhost");
|
|
if (url.pathname === "/asset/") {
|
|
res.setHeader("Content-Type", "text/html; charset=utf-8");
|
|
res.end(
|
|
fs.readFileSync(
|
|
path.join(
|
|
OUT,
|
|
url.searchParams.has("unconfigured")
|
|
? "asset_unconfigured.html"
|
|
: "asset_render.html",
|
|
),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
if (url.pathname.startsWith("/static/")) {
|
|
const file = path.resolve(
|
|
STATIC,
|
|
"." + decodeURIComponent(url.pathname.slice(7)),
|
|
);
|
|
if (
|
|
!file.startsWith(STATIC + path.sep) ||
|
|
!fs.existsSync(file) ||
|
|
!fs.statSync(file).isFile()
|
|
) {
|
|
res.writeHead(404);
|
|
res.end();
|
|
return;
|
|
}
|
|
const types = {
|
|
".css": "text/css",
|
|
".js": "text/javascript",
|
|
".woff2": "font/woff2",
|
|
".png": "image/png",
|
|
};
|
|
res.setHeader(
|
|
"Content-Type",
|
|
types[path.extname(file)] || "application/octet-stream",
|
|
);
|
|
fs.createReadStream(file).pipe(res);
|
|
return;
|
|
}
|
|
const route = url.pathname.slice("/asset/api/".length),
|
|
parts = route.split("/");
|
|
const chunks = [];
|
|
for await (const chunk of req) chunks.push(chunk);
|
|
const raw = Buffer.concat(chunks),
|
|
body = req.headers["content-type"]?.includes("application/json")
|
|
? JSON.parse(raw.toString())
|
|
: null;
|
|
if (req.method !== "GET") {
|
|
assert.equal(req.headers["x-asset-csrf"], "browser-test");
|
|
mutations.push({ route, body });
|
|
}
|
|
if (route === "bootstrap") {
|
|
const totals = {
|
|
total: records.length,
|
|
available: 0,
|
|
in_use: 0,
|
|
maintenance: 0,
|
|
lost: 0,
|
|
retired: 0,
|
|
warranty_soon: records.length,
|
|
purchase_total: "37500000",
|
|
};
|
|
records.forEach((a) => totals[a.status]++);
|
|
json(res, {
|
|
totals,
|
|
users,
|
|
locations: ["本社", "본사 3층"],
|
|
categories: ["computer", "chair", "mouse"].map((category) => ({
|
|
category,
|
|
count: records.filter((a) => a.category === category).length,
|
|
})),
|
|
activity: events
|
|
.slice(-12)
|
|
.reverse()
|
|
.map((e) => ({
|
|
...e,
|
|
tag: "AST-000001",
|
|
name: records.find((a) => a.id === e.asset_id)?.name || "Asset",
|
|
})),
|
|
});
|
|
return;
|
|
}
|
|
if (route === "assets" && req.method === "GET") {
|
|
const rows = filter(url.searchParams),
|
|
page = Number(url.searchParams.get("page") || 1),
|
|
result = {
|
|
items: rows.slice((page - 1) * 25, page * 25),
|
|
total: rows.length,
|
|
page,
|
|
page_size: 25,
|
|
};
|
|
if (slow && url.searchParams.get("q") === "테스트") {
|
|
setTimeout(() => json(res, result), 500);
|
|
return;
|
|
}
|
|
json(res, result);
|
|
return;
|
|
}
|
|
if (route === "assets" && req.method === "POST") {
|
|
const a = {
|
|
...records[1],
|
|
...body,
|
|
id: "asset" + counter++,
|
|
tag: body.tag || "AST-TEST",
|
|
specs: null,
|
|
latest_report_id: null,
|
|
};
|
|
records.push(a);
|
|
json(res, { id: a.id });
|
|
return;
|
|
}
|
|
if (route === "reports/preview") {
|
|
if (raw.toString().includes("invalid.xml")) {
|
|
json(res, { detail: "올바르지 않은 보고서입니다." }, 422);
|
|
return;
|
|
}
|
|
json(res, {
|
|
filename: "test.xml",
|
|
summary: parsed.summary,
|
|
property_count: parsed.property_count,
|
|
scan_time: parsed.scan_time,
|
|
warnings: [],
|
|
});
|
|
return;
|
|
}
|
|
if (route === "reports/import") {
|
|
const match = raw
|
|
.toString()
|
|
.match(/name="payload"\r\n\r\n([\s\S]*?)\r\n--/);
|
|
assert.ok(match, "multipart import must carry the asset payload");
|
|
const data = JSON.parse(match[1]);
|
|
const a = {
|
|
...records[0],
|
|
...data,
|
|
id: "asset" + counter++,
|
|
tag: "AST-IMPORT",
|
|
};
|
|
records.push(a);
|
|
json(res, { id: a.id });
|
|
return;
|
|
}
|
|
if (parts[0] === "assets" && parts[1]) {
|
|
const a = records.find((a) => a.id === parts[1]);
|
|
if (!a) {
|
|
json(res, { detail: "없음" }, 404);
|
|
return;
|
|
}
|
|
if (parts.length === 2 && req.method === "GET") {
|
|
json(res, {
|
|
asset: a,
|
|
report: a.latest_report_id ? { ...reports[0], parsed } : null,
|
|
reports: a.latest_report_id ? reports : [],
|
|
events: events.filter((e) => e.asset_id === a.id),
|
|
});
|
|
return;
|
|
}
|
|
if (parts.length === 2 && req.method === "PUT") {
|
|
if (conflict) {
|
|
conflict = false;
|
|
json(
|
|
res,
|
|
{ detail: "다른 사용자가 수정했습니다. 다시 확인하세요." },
|
|
409,
|
|
);
|
|
return;
|
|
}
|
|
assert.equal(body.version, a.version);
|
|
Object.assign(a, body, { version: a.version + 1 });
|
|
json(res, { ok: true });
|
|
return;
|
|
}
|
|
if (parts[2] === "actions") {
|
|
assert.equal(body.version, a.version);
|
|
a.version++;
|
|
a.status = {
|
|
checkout: "in_use",
|
|
checkin: "available",
|
|
maintenance: "maintenance",
|
|
available: "available",
|
|
lost: "lost",
|
|
retired: "retired",
|
|
}[body.action];
|
|
a.assignee_email = body.action === "checkout" ? body.assignee_email : "";
|
|
events.push({
|
|
id: events.length + 1,
|
|
asset_id: a.id,
|
|
kind: body.action,
|
|
note: body.note,
|
|
actor: users[0].email,
|
|
changes: {},
|
|
created_at: timestamp,
|
|
});
|
|
json(res, { ok: true });
|
|
return;
|
|
}
|
|
if (parts[2] === "reports" && parts.length === 3) {
|
|
a.latest_report_id = "report1";
|
|
a.specs = parsed.summary;
|
|
a.version++;
|
|
json(res, { id: "report1" });
|
|
return;
|
|
}
|
|
if (parts[2] === "reports" && parts.length === 4) {
|
|
json(res, { ...reports[0], parsed });
|
|
return;
|
|
}
|
|
}
|
|
res.writeHead(404);
|
|
res.end();
|
|
});
|
|
|
|
(async () => {
|
|
let browser;
|
|
try {
|
|
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
browser = await chromium.launch({
|
|
headless: true,
|
|
executablePath:
|
|
process.env.PJ_TEST_BROWSER ||
|
|
"C:/Program Files/Google/Chrome/Application/chrome.exe",
|
|
});
|
|
const page = await browser.newPage({
|
|
viewport: { width: 1600, height: 1050 },
|
|
});
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
const origin = `http://127.0.0.1:${server.address().port}`;
|
|
await page.goto(origin + "/asset/");
|
|
await page.locator("#am-total").filter({ hasText: "30" }).waitFor();
|
|
assert.equal(await page.locator("#am-rows tr").count(), 25);
|
|
await page.screenshot({
|
|
path: path.join(OUT, "asset-desktop.png"),
|
|
fullPage: true,
|
|
});
|
|
await page.locator("#am-next").click();
|
|
await page.waitForFunction(
|
|
() => document.querySelector("#am-page-number").textContent === "2 / 2",
|
|
);
|
|
assert.equal(await page.locator("#am-rows tr").count(), 5);
|
|
await page.locator('[data-view="furniture"]').click();
|
|
await page.waitForFunction(
|
|
() =>
|
|
document.querySelector("#am-result-count").textContent === "10개 자산",
|
|
);
|
|
assert.equal(await page.locator("#am-page-number").textContent(), "1 / 1");
|
|
await page.locator("#am-reset").click();
|
|
await page.waitForFunction(
|
|
() =>
|
|
document.querySelector("#am-result-count").textContent === "30개 자산",
|
|
);
|
|
slow = true;
|
|
await page.locator("#am-query").fill("테스트");
|
|
await page.waitForTimeout(300);
|
|
await page.locator("#am-query").fill("KING");
|
|
await page.waitForTimeout(800);
|
|
assert.equal(
|
|
await page.locator("#am-result-count").textContent(),
|
|
"1개 자산",
|
|
"late search response must not replace the newest result",
|
|
);
|
|
await page.locator('[data-open="asset0"][data-tab="specs"]').click();
|
|
await page.locator("#am-spec-query").waitFor();
|
|
assert.ok(
|
|
(await page.locator(".am-spec-card").first().textContent()).includes(
|
|
parsed.summary.cpu,
|
|
),
|
|
);
|
|
await page.locator("#am-spec-section").selectOption("MEMORY");
|
|
await page.locator("#am-detail").evaluate((el) => {
|
|
el.scrollTop = 0;
|
|
});
|
|
await page.screenshot({ path: path.join(OUT, "asset-specs.png") });
|
|
await page.locator("#am-spec-query").fill("M378A2K43EB1");
|
|
if (parsed.summary.computer_name === "KING-DESKTOP")
|
|
assert.equal(await page.locator(".am-node").count(), 2);
|
|
await page.locator('[data-detail-tab="overview"]').click();
|
|
await page.locator("[data-edit]").click();
|
|
await page
|
|
.locator('#am-editor-form [name="notes"]')
|
|
.fill("저장 전 초안 <script>window.assetInjected=true</script>");
|
|
conflict = true;
|
|
await page.locator('#am-editor-form [type="submit"]').click();
|
|
await page
|
|
.locator("#am-editor-form .am-form-error")
|
|
.filter({ hasText: "다른 사용자" })
|
|
.waitFor();
|
|
assert.ok(
|
|
(
|
|
await page.locator('#am-editor-form [name="notes"]').inputValue()
|
|
).includes("초안"),
|
|
);
|
|
await page.locator('#am-editor-form [type="submit"]').click();
|
|
await page.waitForFunction(
|
|
() => !document.querySelector("#am-editor").open,
|
|
);
|
|
await page.locator('[data-action="checkout"]').waitFor();
|
|
assert.equal(await page.evaluate(() => window.assetInjected), undefined);
|
|
await page.locator('[data-action="checkout"]').click();
|
|
await page.locator("#am-action-user").selectOption("staff@test.local");
|
|
await page.locator("#am-action-save").click();
|
|
await page.waitForFunction(
|
|
() => !document.querySelector("#am-action").open,
|
|
);
|
|
await page.locator('[data-action="checkin"]').waitFor();
|
|
assert.equal(
|
|
await page.locator('[data-action="retired"]').count(),
|
|
0,
|
|
"assigned assets cannot be retired directly",
|
|
);
|
|
await page.locator('[data-action="checkin"]').click();
|
|
await page.locator("#am-action-save").click();
|
|
await page.waitForFunction(
|
|
() => !document.querySelector("#am-action").open,
|
|
);
|
|
await page.locator('[data-action="checkout"]').waitFor();
|
|
await page.locator('[data-detail-tab="reports"]').click();
|
|
await page.locator("[data-report]").click();
|
|
await page.locator("#am-spec-query").waitFor();
|
|
assert.ok(
|
|
(await page.locator("#am-detail-content").textContent()).includes(
|
|
"선택한 보고서",
|
|
),
|
|
);
|
|
await page.locator('[data-close="am-detail"]').click();
|
|
await page.locator("#am-import").click();
|
|
await page.locator("#am-report-file").setInputFiles({
|
|
name: "invalid.xml",
|
|
mimeType: "text/xml",
|
|
buffer: Buffer.from("<broken/>"),
|
|
});
|
|
await page
|
|
.locator("#am-upload-form .am-form-error")
|
|
.filter({ hasText: "올바르지" })
|
|
.waitFor();
|
|
assert.equal(await page.locator("#am-import-save").isDisabled(), true);
|
|
await page
|
|
.locator("#am-report-file")
|
|
.setInputFiles(path.join(OUT, "asset_upload.xml"));
|
|
await page.locator("#am-report-preview").waitFor({ state: "visible" });
|
|
assert.equal(
|
|
await page.locator("#am-import-name").inputValue(),
|
|
parsed.summary.computer_name,
|
|
);
|
|
await page.screenshot({ path: path.join(OUT, "asset-import.png") });
|
|
await page.locator("#am-import-name").fill("Uploaded workstation");
|
|
await page.locator("#am-import-save").click();
|
|
await page.waitForFunction(
|
|
() => !document.querySelector("#am-upload").open,
|
|
);
|
|
await page
|
|
.locator("#am-detail-title")
|
|
.filter({ hasText: "Uploaded workstation" })
|
|
.waitFor();
|
|
await page.locator("#am-spec-query").waitFor();
|
|
await page.locator("[data-attach]").click();
|
|
await page
|
|
.locator("#am-report-file")
|
|
.setInputFiles(path.join(OUT, "asset_upload.xml"));
|
|
await page.locator("#am-report-preview").waitFor({ state: "visible" });
|
|
assert.equal(await page.locator("#am-import-fields").isVisible(), false);
|
|
await page.locator("#am-import-save").click();
|
|
await page.waitForFunction(
|
|
() => !document.querySelector("#am-upload").open,
|
|
);
|
|
await page
|
|
.locator("#am-detail-title")
|
|
.filter({ hasText: "Uploaded workstation" })
|
|
.waitFor();
|
|
await page.keyboard.press("Escape");
|
|
await page.locator("#am-create").click();
|
|
await page.locator('#am-editor-form [name="name"]').fill("테스트 의자");
|
|
await page
|
|
.locator('#am-editor-form [name="category"]')
|
|
.selectOption("chair");
|
|
await page.locator('#am-editor-form [type="submit"]').click();
|
|
await page.waitForFunction(
|
|
() => !document.querySelector("#am-editor").open,
|
|
);
|
|
await page
|
|
.locator("#am-detail-title")
|
|
.filter({ hasText: "테스트 의자" })
|
|
.waitFor();
|
|
assert.equal(await page.locator('[data-detail-tab="specs"]').count(), 0);
|
|
await page.keyboard.press("Escape");
|
|
await page.setViewportSize({ width: 390, height: 844 });
|
|
await page.locator("#am-reset").click();
|
|
await page.waitForTimeout(100);
|
|
assert.ok(
|
|
await page.evaluate(
|
|
() => document.documentElement.scrollWidth <= innerWidth + 1,
|
|
),
|
|
"mobile document should not overflow horizontally",
|
|
);
|
|
await page.screenshot({
|
|
path: path.join(OUT, "asset-mobile.png"),
|
|
fullPage: true,
|
|
});
|
|
await page.locator('[data-open="asset0"]').first().click();
|
|
await page
|
|
.locator("#am-detail-title")
|
|
.filter({ hasText: "KING-DESKTOP" })
|
|
.waitFor();
|
|
assert.ok(
|
|
await page.evaluate(
|
|
() =>
|
|
document.querySelector("#am-detail").scrollWidth <= innerWidth + 1,
|
|
),
|
|
);
|
|
await page.locator('.am-tabs [data-detail-tab="specs"]').click();
|
|
await page.locator("#am-spec-query").waitFor();
|
|
await page.locator("#am-spec-section").selectOption("MEMORY");
|
|
await page.screenshot({ path: path.join(OUT, "asset-mobile-specs.png") });
|
|
await page.keyboard.press("Escape");
|
|
await page.goto(origin + "/asset/?category=chair&q=테스트");
|
|
await page.waitForFunction(
|
|
() =>
|
|
document.querySelector("#am-result-count").textContent === "11개 자산",
|
|
);
|
|
assert.equal(await page.locator("#am-category").inputValue(), "chair");
|
|
await page.goto(origin + "/asset/?unconfigured=1");
|
|
assert.equal(await page.locator("#am-create").isDisabled(), true);
|
|
await page.locator("#am-import").click();
|
|
await page
|
|
.locator("#am-report-file")
|
|
.setInputFiles(path.join(OUT, "asset_upload.xml"));
|
|
await page.locator("#am-report-preview").waitFor({ state: "visible" });
|
|
assert.equal(await page.locator("#am-import-save").isDisabled(), true);
|
|
assert.deepEqual(errors, []);
|
|
console.log(
|
|
"ALL PASS: Chrome desktop/mobile, search races, paging, specs, XSS escaping, conflict draft, create, checkout/checkin, report preview, setup mode",
|
|
);
|
|
} finally {
|
|
await browser?.close();
|
|
await new Promise((resolve) => server.close(resolve));
|
|
}
|
|
})().catch((error) => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|