Skip to content

Commit

Permalink
Test cases updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
darsan-in committed Nov 21, 2024
1 parent c2ed7fa commit a8f3c17
Show file tree
Hide file tree
Showing 38 changed files with 2,453 additions and 91 deletions.
4 changes: 2 additions & 2 deletions configLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { join } from "node:path";
import { configurationOptions } from "./lib/types";

export default function loadConfig(): configurationOptions {
const CONFIG_FILE_NAME = "richie.config.js";
const CONFIG_FILE_NAME = "richie.config";

const projectConfigFile = join(process.cwd(), CONFIG_FILE_NAME);
const projectConfigFile = join(process.cwd(), `${CONFIG_FILE_NAME}.js`);
const projectHasConfig = existsSync(projectConfigFile);

let projectConfig: configurationOptions = {} as configurationOptions;
Expand Down
11 changes: 8 additions & 3 deletions test/browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { mkdirSync, writeFileSync } from "node:fs";
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
import { dirname, join, relative, resolve } from "node:path";
import puppeteer, { type Browser } from "puppeteer";
import { richies } from "../lib/types";

const outputBase = "test/outputs/browser-env";

async function _RenderResult(
browser: Browser,
htmlPath: string,
Expand All @@ -28,15 +30,15 @@ async function _RenderResult(

// Define the output destination
const outputDest = join(
"test/test-sample-outputs", // Base output folder
outputBase, // Base output folder
relative("test/test-sample", htmlPath), // Relative path from the base source folder
);

// Ensure the destination directory exists, then save the content
mkdirSync(dirname(outputDest), { recursive: true });
writeFileSync(outputDest, content);

console.log(`HTML saved to ${outputDest}`);
/* console.log(`HTML saved to ${outputDest}`); */
} catch (err) {
console.error("Error during rendering:", err);
}
Expand Down Expand Up @@ -105,6 +107,9 @@ const testParamMap: Partial<
};

async function runAll() {
// Clean up and prepare output directory
rmSync(outputBase, { recursive: true, force: true });

// Launch the browser once at the start
const browser = await puppeteer.launch({ headless: true });

Expand Down
109 changes: 43 additions & 66 deletions test/main.test.ts → test/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,124 +1,101 @@
import { existsSync, mkdirSync, rmSync } from "node:fs";

import { basename, join } from "path";
import { richies, testProps } from "../lib/types";
import { richie } from "../richie";

const opfolder = "./test/outputs";
let iterN = 0;

function mkDestPath(testfile: string): string {
iterN += 1;
const opFolder = "./test/outputs/node-env";
let iteration = 0;

// Generate a unique destination path for test output
function createDestinationPath(testFile: string): string {
iteration += 1;
return join(
process.cwd(),
opfolder,
`OP_Of_(${iterN}) ${basename(testfile)}`,
opFolder,
`Output_${iteration}_${basename(testFile)}`,
);
}

function createTestProps(testfile: string): testProps {
// Create test properties object
function createTestProps(testFile: string): testProps {
return {
testfile: testfile,
destFile: mkDestPath(testfile),
testfile: testFile,
destFile: createDestinationPath(testFile),
};
}

// Define test cases
const testRecords: Record<richies, testProps> = {
article: createTestProps("test/test-sample/article.html"),

breadcrumb: createTestProps(
"test/test-sample/breadcrumb/sub-breadcrumb/notindex.html",
),

crecipe: createTestProps("test/test-sample/carousels/recipies.html"),
recipe: createTestProps("test/test-sample/carousels/recipies.html"),

movie: createTestProps("test/test-sample/carousels/movies.html"),
cmovie: createTestProps("test/test-sample/carousels/movies.html"),

restaurant: createTestProps(
"test/test-sample/carousels/restaurants.html",
),
crestaurant: createTestProps(
"test/test-sample/carousels/restaurants.html",
),

course: createTestProps("test/test-sample/carousels/courses.html"),
ccourse: createTestProps("test/test-sample/carousels/courses.html"),

event: createTestProps("test/test-sample/events.html"),

faq: createTestProps("test/test-sample/faq.html"),

video: createTestProps("test/test-sample/videos.html"),

localbusiness: createTestProps("test/test-sample/localbusiness.html"),

organization: createTestProps("test/test-sample/org.html"),

product: createTestProps("test/test-sample/product.html"),
productwv: createTestProps(
"test/test-sample/productVarient/productCombined.html",
),

profile: createTestProps("test/test-sample/profilepage.html"),

searchbox: createTestProps(
"test/test-sample/Sitesearch/searchpage.html",
),

software: createTestProps("test/test-sample/softwareapp.html"),
};

function run(richieName: richies): Promise<void> {
const testfile = testRecords[richieName].testfile;
const destinationFile = testRecords[richieName].destFile;

return richie([richieName], testfile, destinationFile);
// Execute a single test case
async function executeTest(richieName: richies): Promise<void> {
const { testfile, destFile } = testRecords[richieName];
await richie([richieName], testfile, destFile);
}

function runAll(): Promise<string> {
//delete old artifacts
try {
rmSync(opfolder, { recursive: true });
} catch {
//nothing to do
} finally {
//make op dir
mkdirSync(opfolder, { recursive: true });
}

const testOps: Promise<void>[] = [];

for (const richieName of Object.keys(testRecords)) {
testOps.push(run(richieName as richies));
}
// Execute all test cases
async function executeAllTests(): Promise<string> {
// Clean up and prepare output directory
rmSync(opFolder, { recursive: true, force: true });
mkdirSync(opFolder, { recursive: true });

// Run all tests in parallel
await Promise.all(
Object.keys(testRecords).map((richieName) =>
executeTest(richieName as richies),
),
);

return new Promise((resolve, reject) => {
Promise.all(testOps)
.then(() => {
resolve("passed");
})
.catch((err) => {
reject(err);
});
});
return "passed";
}

function outputCheck(): string {
for (const richieName of Object.keys(testRecords)) {
if (!existsSync(testRecords[richieName as richies].destFile)) {
return "failed";
}
}
return "passed";
// Verify output files
function verifyOutputs(): string {
return (
Object.values(testRecords).every(({ destFile }) =>
existsSync(destFile),
)
) ?
"passed"
: "failed";
}

test("InAction test", async () => {
expect(await runAll()).toBe("passed");
// Test cases
test("Execute all tests", async () => {
expect(await executeAllTests()).toBe("passed");
});

test("Output files Check", () => {
expect(outputCheck()).toBe("passed");
test("Verify output files", () => {
expect(verifyOutputs()).toBe("passed");
});
27 changes: 27 additions & 0 deletions test/outputs/browser-env/Sitesearch/searchpage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Form</title>

<script src="./client.js" type="text/javascript" defer=""></script>
<script src="../bundle.min.js">
</script>
<script>
document.addEventListener('DOMContentLoaded', function () {
RichieJS.default({ richieNames: ['searchbox'] })
});
</script>

<script type="application/ld+json">{"@context":"https://schema.org","@type":"WebSite","url":"https://www.cresteem.com/","potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://www.cresteem.com//A:/Git/cresteem/Richie-JS/test/test-sample/Sitesearch/searchpage?q={searchTerm}"},"query-input":"required name=searchTerm"}}</script></head>

<body>
<h1>Search Form</h1>
<form onsubmit="return search()">
<label for="searchInput">Search:</label>
<input type="text" id="searchTerm" name="searchTerm">
<button type="submit">Search</button>
</form>
<div id="result"></div>


</body></html>
52 changes: 52 additions & 0 deletions test/outputs/browser-env/article.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Article Test Page</title>
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">

<script src="./bundle.min.js">
</script>
<script>
document.addEventListener('DOMContentLoaded', function() {
RichieJS.default({richieNames:['article']})
});
</script>

<script type="application/ld+json">{"@context":"https://schema.org","@type":"NewsArticle","headline":"Article Test Page","image":["https://i.imgur.com/X3E4UxT.jpeg","https://i.imgur.com/pJSzybj.jpeg"],"datePublished":"2024-02-06T20:37:00+05:30","dateModified":"2024-06-26T17:23:00+05:30","author":[{"@type":"Person","honorificPrefix":"Dr","name":"Phil","url":"https://www.instagram.com/iamspdarsan/","jobTitle":"Software Engineer"},{"@type":"Organization","name":"Winchester","url":"https://www.linkedin.com/in/iamspdarsan/"}],"publisher":[{"name":"Cresteem","url":"https://cresteem.com/blogs/"},{"name":"Google","url":"https://google.com/blogs/"}]}</script></head>


<body data-articletype="NewsArticle"><!-- article type - Article, NewsArticle, BlogPosting-->
<!-- published date -->
<p class="rjs-article-pdt">2024-02-06 08:37 PM</p>
<!-- modified date -->
<p class="rjs-article-mdt">2024-06-26 05:23 PM</p>

<main>
<!-- thumbnail images or cover images -->
<img class="rjs-article-img" src="https://i.imgur.com/X3E4UxT.jpeg" alt="">
<img class="rjs-article-img" src="https://i.imgur.com/pJSzybj.jpeg" alt="">

<!-- authorname -->
<!-- anameP is Person type -->
<!-- anameO is Organisation type -->
<!-- HonorPrefix supported e.g.: Mr, Mrs, Miss, Ms, Mx, Sir, Dame, Dr, Cllr, Lady, or Lord, or other titles or positions-->
<p class="rjs-article-anameP-1">Dr.Phil</p>

<!-- author profile url -->
<a class="rjs-article-aurl-1" href="https://www.instagram.com/iamspdarsan/">User Profile</a>

<!-- author job title - this is optional-->
<p class="rjs-article-ajob-1">Software Engineer</p>

<p class="rjs-article-anameO-2">Winchester</p>
<a class="rjs-article-aurl-2" href="https://www.linkedin.com/in/iamspdarsan/">User Profile</a>

<p class="rjs-article-pname-1">Cresteem</p>
<a class="rjs-article-purl-1" href="https://cresteem.com/blogs/"></a>

<p class="rjs-article-pname-2">Google</p>
<a class="rjs-article-purl-2" href="https://google.com/blogs/"></a>
</main>


</body></html>
Loading

0 comments on commit a8f3c17

Please sign in to comment.