Skip to content

Commit

Permalink
testcase: route validator - added
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Nov 29, 2024
1 parent a8fd9fc commit 7ddd835
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class Utils {
): RouteMeta[] {
const routesMeta: RouteMeta[] = [] as RouteMeta[];

_lookupFiles(lookupPatterns, ignorePattern).forEach(
this.lookupFiles(lookupPatterns, ignorePattern).forEach(
(filePath: string): void => {
let relativePath: string = relative(process.cwd(), filePath);

Expand Down Expand Up @@ -398,15 +398,15 @@ export default class Utils {
}
return dataObject[keyName];
}
}

function _lookupFiles(
lookupPatterns: string[],
ignorePatterns: string[],
): string[] {
const webPageFilePaths: string[] = globSync(lookupPatterns, {
ignore: [...ignorePatterns, "node_modules/**"],
});
lookupFiles(
lookupPatterns: string[],
ignorePatterns: string[],
): string[] {
const webPageFilePaths: string[] = globSync(lookupPatterns, {
ignore: [...ignorePatterns, "node_modules/**"],
});

return webPageFilePaths;
return webPageFilePaths;
}
}
11 changes: 11 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { chdir } from "node:process";
import { Hawk } from "../lib/core";
import validateRoutes from "./utils/validate-routes";
import validateSitemap from "./utils/validate-sitemap";

const hawkInstance = new Hawk();
const testSampleRootPath = "./test/test-sample";

const cwd = process.cwd();
afterEach(() => {
chdir(cwd);
});

test("Sitemap.xml validation", async () => {
expect(await validateSitemap(testSampleRootPath, hawkInstance)).toBe(
true,
);
});

test("Routes validation", () => {
expect(validateRoutes(testSampleRootPath, hawkInstance)).toBe(true);
});
40 changes: 40 additions & 0 deletions test/utils/validate-routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { utimesSync } from "node:fs";
import { type Hawk } from "../../lib/core";

export default function validateRoutes(
testSampleRoot: string,
hawkInstance: Hawk,
) {
process.chdir(testSampleRoot);

const { lookupPatterns, ignorePattern } = hawkInstance.configurations;

const availableRoutes = hawkInstance.utils.lookupFiles(
lookupPatterns,
ignorePattern,
);

//change mod time of routes
const simulatedLastSubmisssionTime = Date.now();
const newRouteModTime = new Date(simulatedLastSubmisssionTime + 10); //simulate as 10ms latest

//pick some random number of routes and update
const simulated_updatedRoutes = availableRoutes
.slice(0, _getRandomInteger(1, availableRoutes.length))
.map((route) => {
utimesSync(route, newRouteModTime, newRouteModTime);
return route;
});

const updatedRoutes = hawkInstance.utils.getUpdatedRoutesPath(
simulatedLastSubmisssionTime,
lookupPatterns,
ignorePattern,
);

return simulated_updatedRoutes.length === updatedRoutes.length;
}

function _getRandomInteger(min: number, max: number) {
return Math.floor(Math.random() * (max - min + 1)) + min; // Inclusive of both min and max
}

0 comments on commit 7ddd835

Please sign in to comment.