refactor: centralize test path containment checks

This commit is contained in:
Peter Steinberger
2026-01-09 18:47:52 +01:00
parent f0a909f6dd
commit 837cec64af
3 changed files with 40 additions and 44 deletions

View File

@@ -3,6 +3,7 @@ import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { isPathWithinBase } from "../../test/helpers/paths.js";
import { withTempHome } from "../../test/helpers/temp-home.js";
const runtime = {
@@ -28,26 +29,7 @@ describe("web logout", () => {
vi.resetModules();
const { logoutWeb, WA_WEB_AUTH_DIR } = await import("./session.js");
if (process.platform === "win32") {
const normalizedHome = path.win32.normalize(home).toLowerCase();
const normalizedAuthDir = path.win32
.normalize(WA_WEB_AUTH_DIR)
.toLowerCase();
const rel = path.win32.relative(normalizedHome, normalizedAuthDir);
const isWithinHome =
rel.length > 0 &&
!rel.startsWith("..") &&
!path.win32.isAbsolute(rel);
expect(isWithinHome).toBe(true);
} else {
const rel = path.relative(
path.resolve(home),
path.resolve(WA_WEB_AUTH_DIR),
);
expect(rel && !rel.startsWith("..") && !path.isAbsolute(rel)).toBe(
true,
);
}
expect(isPathWithinBase(home, WA_WEB_AUTH_DIR)).toBe(true);
fs.mkdirSync(WA_WEB_AUTH_DIR, { recursive: true });
fs.writeFileSync(path.join(WA_WEB_AUTH_DIR, "creds.json"), "{}");