- Add `originMigration.js` for handling allowed origins, standalone contexts, and redirect routes. - Create `OutdatedGateway.vue` and `OutdatedInstallation.vue` error views with timer-based redirection and user instructions. - Update router to include `/outdated-installation` and `/outdated-gateway` paths. - Implement unit tests for migration utility methods and route guards. - Remove debug `console.log` statements across multiple components.
102 lines
2.8 KiB
JavaScript
102 lines
2.8 KiB
JavaScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import {
|
|
OUTDATED_GATEWAY_ROUTE_NAME,
|
|
OUTDATED_INSTALLATION_ROUTE_NAME,
|
|
buildMigrationUrl,
|
|
resolveOriginMigrationRoute,
|
|
sanitizeRedirectPath,
|
|
startRedirectCountdown,
|
|
} from "@/middleware/originMigration";
|
|
|
|
describe("origin migration route guard", () => {
|
|
const allowedOrigins = ["https://truckwash.io", "https://www.truckwash.io"];
|
|
const route = { name: "landing", fullPath: "/user/bookings?tab=open#latest" };
|
|
|
|
it("allows navigation on allowed origins", () => {
|
|
const target = resolveOriginMigrationRoute({
|
|
to: route,
|
|
currentOrigin: "https://truckwash.io",
|
|
allowedOrigins,
|
|
standalone: false,
|
|
});
|
|
|
|
expect(target).toBeNull();
|
|
});
|
|
|
|
it("routes to outdated installation for disallowed standalone context", () => {
|
|
const target = resolveOriginMigrationRoute({
|
|
to: route,
|
|
currentOrigin: "https://legacy.truckwash.io",
|
|
allowedOrigins,
|
|
standalone: true,
|
|
});
|
|
|
|
expect(target).toEqual({ name: OUTDATED_INSTALLATION_ROUTE_NAME });
|
|
});
|
|
|
|
it("routes to outdated gateway for disallowed browser context", () => {
|
|
const target = resolveOriginMigrationRoute({
|
|
to: route,
|
|
currentOrigin: "https://legacy.truckwash.io",
|
|
allowedOrigins,
|
|
standalone: false,
|
|
});
|
|
|
|
expect(target).toEqual({
|
|
name: OUTDATED_GATEWAY_ROUTE_NAME,
|
|
query: { redirect: "/user/bookings?tab=open#latest" },
|
|
});
|
|
});
|
|
|
|
it("bypasses the gate on dedicated outdated routes", () => {
|
|
const target = resolveOriginMigrationRoute({
|
|
to: { name: OUTDATED_GATEWAY_ROUTE_NAME, fullPath: "/outdated-gateway" },
|
|
currentOrigin: "https://legacy.truckwash.io",
|
|
allowedOrigins,
|
|
standalone: false,
|
|
});
|
|
|
|
expect(target).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("origin migration redirect url", () => {
|
|
it("preserves path, query, and hash on migration origin", () => {
|
|
const redirectUrl = buildMigrationUrl(
|
|
"https://truckwash.io",
|
|
"/user/bookings?tab=open#latest",
|
|
);
|
|
|
|
expect(redirectUrl).toBe("https://truckwash.io/user/bookings?tab=open#latest");
|
|
});
|
|
|
|
it("sanitizes invalid redirect paths to root", () => {
|
|
expect(sanitizeRedirectPath("https://malicious.example")).toBe("/");
|
|
});
|
|
});
|
|
|
|
describe("gateway redirect countdown", () => {
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it("ticks every second and redirects after five seconds", () => {
|
|
vi.useFakeTimers();
|
|
|
|
const onRedirect = vi.fn();
|
|
const tickValues = [];
|
|
const stop = startRedirectCountdown({
|
|
seconds: 5,
|
|
onTick: (seconds) => tickValues.push(seconds),
|
|
onRedirect,
|
|
});
|
|
|
|
vi.advanceTimersByTime(5000);
|
|
|
|
expect(tickValues).toEqual([4, 3, 2, 1, 0]);
|
|
expect(onRedirect).toHaveBeenCalledTimes(1);
|
|
|
|
stop();
|
|
});
|
|
});
|