Skip to content

Commit 7ddd835

Browse files
committed
testcase: route validator - added
1 parent a8fd9fc commit 7ddd835

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

lib/utils.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default class Utils {
4747
): RouteMeta[] {
4848
const routesMeta: RouteMeta[] = [] as RouteMeta[];
4949

50-
_lookupFiles(lookupPatterns, ignorePattern).forEach(
50+
this.lookupFiles(lookupPatterns, ignorePattern).forEach(
5151
(filePath: string): void => {
5252
let relativePath: string = relative(process.cwd(), filePath);
5353

@@ -398,15 +398,15 @@ export default class Utils {
398398
}
399399
return dataObject[keyName];
400400
}
401-
}
402401

403-
function _lookupFiles(
404-
lookupPatterns: string[],
405-
ignorePatterns: string[],
406-
): string[] {
407-
const webPageFilePaths: string[] = globSync(lookupPatterns, {
408-
ignore: [...ignorePatterns, "node_modules/**"],
409-
});
402+
lookupFiles(
403+
lookupPatterns: string[],
404+
ignorePatterns: string[],
405+
): string[] {
406+
const webPageFilePaths: string[] = globSync(lookupPatterns, {
407+
ignore: [...ignorePatterns, "node_modules/**"],
408+
});
410409

411-
return webPageFilePaths;
410+
return webPageFilePaths;
411+
}
412412
}

test/main.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
import { chdir } from "node:process";
12
import { Hawk } from "../lib/core";
3+
import validateRoutes from "./utils/validate-routes";
24
import validateSitemap from "./utils/validate-sitemap";
35

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

9+
const cwd = process.cwd();
10+
afterEach(() => {
11+
chdir(cwd);
12+
});
13+
714
test("Sitemap.xml validation", async () => {
815
expect(await validateSitemap(testSampleRootPath, hawkInstance)).toBe(
916
true,
1017
);
1118
});
19+
20+
test("Routes validation", () => {
21+
expect(validateRoutes(testSampleRootPath, hawkInstance)).toBe(true);
22+
});

test/utils/validate-routes.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { utimesSync } from "node:fs";
2+
import { type Hawk } from "../../lib/core";
3+
4+
export default function validateRoutes(
5+
testSampleRoot: string,
6+
hawkInstance: Hawk,
7+
) {
8+
process.chdir(testSampleRoot);
9+
10+
const { lookupPatterns, ignorePattern } = hawkInstance.configurations;
11+
12+
const availableRoutes = hawkInstance.utils.lookupFiles(
13+
lookupPatterns,
14+
ignorePattern,
15+
);
16+
17+
//change mod time of routes
18+
const simulatedLastSubmisssionTime = Date.now();
19+
const newRouteModTime = new Date(simulatedLastSubmisssionTime + 10); //simulate as 10ms latest
20+
21+
//pick some random number of routes and update
22+
const simulated_updatedRoutes = availableRoutes
23+
.slice(0, _getRandomInteger(1, availableRoutes.length))
24+
.map((route) => {
25+
utimesSync(route, newRouteModTime, newRouteModTime);
26+
return route;
27+
});
28+
29+
const updatedRoutes = hawkInstance.utils.getUpdatedRoutesPath(
30+
simulatedLastSubmisssionTime,
31+
lookupPatterns,
32+
ignorePattern,
33+
);
34+
35+
return simulated_updatedRoutes.length === updatedRoutes.length;
36+
}
37+
38+
function _getRandomInteger(min: number, max: number) {
39+
return Math.floor(Math.random() * (max - min + 1)) + min; // Inclusive of both min and max
40+
}

0 commit comments

Comments
 (0)