Files

244 lines
8.5 KiB
JavaScript

const fs = require("node:fs"),
path = require("node:path"),
assert = require("node:assert/strict"),
{ spawn } = require("node:child_process");
const { chromium } = require(process.env.PLAYWRIGHT_MODULE);
(async () => {
const root = fs.mkdtempSync(
path.join(process.env.FILEBROWSER_QA_OUTPUT, "navigation-")
);
const files = path.join(root, "files");
fs.mkdirSync(files);
fs.writeFileSync(path.join(files, "anonymous.txt"), "NO PASSWORD");
for (const folder of ["Fast", "Slow", "Empty"])
fs.mkdirSync(path.join(files, folder));
fs.writeFileSync(path.join(files, "Fast", "fast.txt"), "FAST");
fs.writeFileSync(path.join(files, "Slow", "slow.txt"), "SLOW");
fs.mkdirSync(path.join(files, "Fast", "Nested"));
fs.writeFileSync(path.join(files, "Fast", "Nested", "nested.txt"), "NESTED");
const config = path.join(root, "config.json"),
db = path.join(root, "test.db");
fs.writeFileSync(
config,
JSON.stringify({
root: files,
database: db,
sso: { issuer: "invalid-provider-configuration" },
})
);
const args = [
"--config",
config,
"--root",
files,
"--database",
db,
"--port",
"0",
];
const processOne = spawn(process.env.FILEBROWSER_EXE, [...args, "--no-sso"], {
cwd: root,
windowsHide: true,
stdio: ["ignore", "pipe", "pipe"],
});
let logs = "",
browser;
const started = new Promise((resolve, reject) => {
const timeout = setTimeout(
() => reject(Error("startup timeout " + logs)),
12000
);
const read = (chunk) => {
logs += chunk.toString();
const found = logs.match(/Listening on (127\.0\.0\.1:\d+)/);
if (found) {
clearTimeout(timeout);
resolve("http://" + found[1]);
}
};
processOne.stdout.on("data", read);
processOne.stderr.on("data", read);
processOne.once("exit", (code) => {
clearTimeout(timeout);
reject(Error("exited " + code + " " + logs));
});
});
try {
const url = await started;
browser = await chromium.launch({
executablePath: process.env.BROWSER_EXECUTABLE,
headless: true,
});
const context = await browser.newContext();
const page = await context.newPage();
const errors = [];
page.on("pageerror", (error) => errors.push(String(error)));
const response = await context.request.get(url + "/api/session");
assert.equal(response.status(), 200);
const user = await response.json();
assert.equal(user.authMode, "no-sso");
assert.equal(user.perm.admin, true);
await page.goto(url + "/files/");
await page.getByText("anonymous.txt", { exact: true }).waitFor();
assert.equal(await page.locator("iframe").count(), 0);
assert.equal(await page.locator('a[href$="/auth/account"]').count(), 0);
// Sample every rendered frame and observe removals: a single missing frame fails.
await page.evaluate(() => {
window.navNodes = [
document.querySelector("#listing"),
document.querySelector("header"),
document.querySelector(".workspace-sidebar"),
];
window.navRemoved = [];
window.navBlankFrames = 0;
window.navObserver = new MutationObserver((changes) => {
for (const change of changes)
for (const removed of change.removedNodes)
for (let i = 0; i < window.navNodes.length; i++)
if (
removed === window.navNodes[i] ||
removed.contains?.(window.navNodes[i])
)
window.navRemoved.push(i);
});
window.navObserver.observe(document.body, {
childList: true,
subtree: true,
});
window.navSampling = true;
function frame() {
if (!window.navSampling) return;
if (
!document.querySelector("#listing") ||
!document.querySelector("header")
)
window.navBlankFrames++;
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
});
let releaseSlow;
const slowGate = new Promise((resolve) => (releaseSlow = resolve));
await page.route("**/api/resources/Slow/", async (route) => {
await slowGate;
try {
await route.continue();
} catch {
/* superseded request was aborted */
}
});
const slowRequest = page
.waitForRequest((r) => r.url().endsWith("/api/resources/Slow/"))
.catch(() => null);
await page
.locator("#listing")
.getByRole("button", { name: "Slow", exact: true })
.dblclick();
await slowRequest;
await page.waitForTimeout(250);
assert.equal(new URL(page.url()).pathname, "/files/");
await page.getByText("anonymous.txt", { exact: true }).waitFor();
assert(await page.locator(".navigation-progress").isVisible());
await page
.locator("#listing")
.getByRole("button", { name: "Fast", exact: true })
.dblclick();
await page.waitForURL("**/files/Fast/");
await page.getByText("fast.txt", { exact: true }).waitFor();
releaseSlow();
await page.waitForTimeout(150);
assert.equal(new URL(page.url()).pathname, "/files/Fast/");
assert.equal(await page.getByText("slow.txt", { exact: true }).count(), 0);
await page
.locator("#listing")
.getByRole("button", { name: "Nested", exact: true })
.dblclick();
await page.waitForURL("**/files/Fast/Nested/");
await page.getByText("nested.txt", { exact: true }).waitFor();
await page.goBack();
await page.waitForURL("**/files/Fast/");
await page.getByText("fast.txt", { exact: true }).waitFor();
const stability = await page.evaluate(() => {
window.navSampling = false;
window.navObserver.disconnect();
return {
removed: window.navRemoved,
blankFrames: window.navBlankFrames,
sameNodes: window.navNodes.map((node) => document.contains(node)),
};
});
assert.deepEqual(stability.removed, []);
assert.equal(stability.blankFrames, 0);
assert(stability.sameNodes.every(Boolean));
assert.equal(await page.locator(".navigation-progress").count(), 0);
await page.locator(".account-button").click();
await page.locator('a[href="/settings/profile"]').click();
await page.waitForURL("**/settings/profile");
await page.locator(".settings-page").waitFor();
assert(await page.evaluate(() => document.contains(window.navNodes[2])));
await page.goBack();
await page.waitForURL("**/files/Fast/");
await page.getByText("fast.txt", { exact: true }).waitFor();
// The same workspace works on a phone, including an empty directory and back.
await page.setViewportSize({ width: 390, height: 844 });
await page.goBack();
await page.waitForURL("**/files/");
await page
.locator("#listing")
.getByRole("button", { name: "Empty", exact: true })
.dblclick();
await page.waitForURL("**/files/Empty/");
await page.locator(".message").waitFor();
assert(
await page.evaluate(
() => document.documentElement.scrollWidth <= innerWidth
)
);
await page.goBack();
await page.waitForURL("**/files/");
await page.getByText("anonymous.txt", { exact: true }).waitFor();
// Preview entry and return are client-side transitions too.
await page
.getByRole("button", { name: "anonymous.txt", exact: true })
.dblclick();
await page.waitForURL("**/files/anonymous.txt");
await page.locator("#previewer").waitFor();
await page.goBack();
await page.waitForURL("**/files/");
await page.getByText("anonymous.txt", { exact: true }).waitFor();
assert.equal(
await page.evaluate(
() => performance.getEntriesByType("navigation").length
),
1
);
assert.deepEqual(errors, []);
await page.screenshot({ path: path.join(root, "mobile-navigation.png") });
console.log(
"Atomic SPA navigation: persistent listing/header/sidebar; zero blank frames; slow and superseded requests; nested/back/empty/mobile/preview passed."
);
} catch (error) {
if (browser) {
for (const ctx of browser.contexts())
for (const tab of ctx.pages()) {
fs.writeFileSync(
path.join(root, "failure.html"),
await tab.content()
);
await tab.screenshot({ path: path.join(root, "failure.png") });
}
}
throw error;
} finally {
if (browser) await browser.close();
if (processOne.exitCode === null) {
processOne.kill();
await new Promise((r) => processOne.once("exit", r));
}
fs.writeFileSync(path.join(root, "server.log"), logs);
}
})().catch((e) => {
console.error(e);
process.exitCode = 1;
});