Skip to content

Commit

Permalink
chore: add jsconfig; type check js (#1618)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Aug 13, 2023
1 parent 25e0b24 commit c0199d7
Show file tree
Hide file tree
Showing 27 changed files with 177 additions and 72 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ src/
.pm2.config.js
.prettierignore
.prettierrc
jsconfig.json
CHANGELOG.md
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Expand Down
10 changes: 10 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
// "checkJs": true,
// "strict": true
"lib": ["ES2022"],
"moduleResolution": "nodenext",
"resolveJsonModule": true
},
"exclude": ["dist", "node_modules"]
}
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"devDependencies": {
"@commitlint/cli": "^17.6.7",
"@commitlint/config-conventional": "^17.6.7",
"@types/jest": "^29.5.3",
"autocannon": "^7.12.0",
"esbuild": "^0.18.14",
"esbuild-plugin-glob": "^2.2.2",
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/doc-to-txt/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const sensible = require("@fastify/sensible");
const plugin = require(".");

describe("DOC-to-TXT conversion plugin", () => {
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/docx-to-html/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const sensible = require("@fastify/sensible");
const plugin = require(".");

describe("DOCX-to-HTML conversion plugin", () => {
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/embed-html-images/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const getConfig = require("../../config");

describe("Embed-HTML-Images plugin", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/html-to-txt/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const sensible = require("@fastify/sensible");
const plugin = require(".");

describe("HTML-to-TXT conversion plugin", () => {
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/image-to-txt/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const getConfig = require("../../config");

describe("Image-to-TXT conversion plugin", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down
15 changes: 9 additions & 6 deletions src/plugins/pdf-to-html/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const artifacts =

describe("PDF-to-HTML conversion plugin", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down Expand Up @@ -73,8 +76,8 @@ describe("PDF-to-HTML conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
lastPageToConvert: 2,
ignoreImages: false,
lastPageToConvert: "2",
ignoreImages: "false",
...query,
},
headers: {
Expand All @@ -97,10 +100,10 @@ describe("PDF-to-HTML conversion plugin", () => {
httpEquiv: expect.stringMatching(/^content-type$/iu),
});
expect(
dom.window.document.head.querySelector("title").textContent
dom.window.document.head.querySelector("title")?.textContent
).toMatch(/^docsmith_pdf-to-html_/u);
// String found in first paragraph of the test document
expect(dom.window.document.querySelector("p").textContent).toMatch(
expect(dom.window.document.querySelector("p")?.textContent).toMatch(
/for\sEngland\s/u
);
// String found in last paragraph of the test document
Expand Down Expand Up @@ -167,8 +170,8 @@ describe("PDF-to-HTML conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
lastPageToConvert: 1,
ignoreImages: false,
lastPageToConvert: "1",
ignoreImages: "false",
},
headers: {
"content-type": "application/pdf",
Expand Down
35 changes: 19 additions & 16 deletions src/plugins/pdf-to-txt/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const imageToTxt = require("../image-to-txt");

describe("PDF-to-TXT conversion plugin", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down Expand Up @@ -67,8 +70,8 @@ describe("PDF-to-TXT conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
firstPageToConvert: 2,
lastPageToConvert: 2,
firstPageToConvert: "2",
lastPageToConvert: "2",
...query,
},
headers: {
Expand All @@ -87,12 +90,12 @@ describe("PDF-to-TXT conversion plugin", () => {
it.each([
{
testName: "Converts PDF file to TXT using OCR",
query: { ocr: true },
query: { ocr: "true" },
},
{
testName:
"Converts PDF file to TXT using OCR and ignore invalid `test` query string param ",
query: { ocr: true, test: "test" },
query: { ocr: "true", test: "test" },
},
])("$testName", async ({ query }) => {
const response = await server.inject({
Expand All @@ -102,8 +105,8 @@ describe("PDF-to-TXT conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
firstPageToConvert: 1,
lastPageToConvert: 2,
firstPageToConvert: "1",
lastPageToConvert: "2",
...query,
},
headers: {
Expand Down Expand Up @@ -137,9 +140,9 @@ describe("PDF-to-TXT conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
firstPageToConvert: 2,
generateHtmlMetaFile: true,
lastPageToConvert: 2,
firstPageToConvert: "2",
generateHtmlMetaFile: "true",
lastPageToConvert: "2",
},
headers: {
"content-type": "application/pdf",
Expand All @@ -160,14 +163,14 @@ describe("PDF-to-TXT conversion plugin", () => {
httpEquiv: expect.stringMatching(/^content-type$/iu),
});
expect(
dom.window.document.head.querySelector("title").textContent
dom.window.document.head.querySelector("title")?.textContent
).toMatch(/^docsmith_pdf-to-txt_/u);
// String found at the start of the HTML document
expect(dom.window.document.querySelector("pre").textContent).toMatch(
expect(dom.window.document.querySelector("pre")?.textContent).toMatch(
"The NHS belongs to the people"
);
// String found at the end of the HTML document
expect(dom.window.document.querySelector("pre").textContent).toMatch(
expect(dom.window.document.querySelector("pre")?.textContent).toMatch(
/a full and transparent debate with the public, patients and staff.$/mu
);
expect(response.statusCode).toBe(200);
Expand All @@ -184,8 +187,8 @@ describe("PDF-to-TXT conversion plugin", () => {
testName: "is not a valid PDF file for OCR",
read: true,
query: {
lastPageToConvert: 1,
ocr: true,
lastPageToConvert: "1",
ocr: "true",
},
},
])(
Expand Down Expand Up @@ -224,7 +227,7 @@ describe("PDF-to-TXT conversion plugin", () => {
testName: "poppler.pdfToCairo() for OCR",
funcName: "pdfToCairo",
query: {
ocr: true,
ocr: "true",
},
},
])(
Expand All @@ -241,7 +244,7 @@ describe("PDF-to-TXT conversion plugin", () => {
"./test_resources/test_files/pdf_1.3_NHS_Constitution.pdf"
),
query: {
lastPageToConvert: 1,
lastPageToConvert: "1",
...query,
},
headers: {
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/rtf-to-html/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const artifacts =

describe("RTF-to-HTML conversion plugin", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down Expand Up @@ -76,7 +79,7 @@ describe("RTF-to-HTML conversion plugin", () => {
httpEquiv: expect.stringMatching(/^content-type$/iu),
});
expect(
dom.window.document.head.querySelector("title").textContent
dom.window.document.head.querySelector("title")?.textContent
).toMatch(/^docsmith_rtf-to-html_/u);
// Check all images are removed
expect(dom.window.document.querySelectorAll("img")).toHaveLength(0);
Expand Down
11 changes: 7 additions & 4 deletions src/plugins/tidy-css/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ const getConfig = require("../../config");

describe("Tidy-CSS plugin", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down Expand Up @@ -112,17 +115,17 @@ describe("Tidy-CSS plugin", () => {
// Check CSS is combined into one style tag
expect(dom.window.document.querySelectorAll("style")).toHaveLength(1);
// Check font-family is set to expected value
expect(style.innerHTML).toMatch(expected?.fonts || /./u);
expect(style?.innerHTML).toMatch(expected?.fonts || /./u);
// Check background-color is set to expected value
expect(style.innerHTML).toMatch(expected?.backgroundColor || /./u);
expect(style?.innerHTML).toMatch(expected?.backgroundColor || /./u);
// Check page-break-inside is set to avoid
expect(style.innerHTML).toMatch(
expect(style?.innerHTML).toMatch(
file !== "html_valid_no_style_type.html"
? /page-break-inside:avoid/u
: /./u
);
// Check CSS is tidied and minified
expect(style.innerHTML).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(style?.innerHTML).not.toMatch(/;\}|<!--|--!?>|\n|\r/u);
expect(response.statusCode).toBe(200);
});

Expand Down
1 change: 1 addition & 0 deletions src/plugins/tidy-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { promisify } = require("util");
const fp = require("fastify-plugin");
const { JSDOM } = require("jsdom");
// @ts-ignore
const { minify } = require("html-minifier-terser");
const { tidy } = require("htmltidy2");
const { check } = require("language-tags");
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/tidy-html/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ const sensible = require("@fastify/sensible");
const plugin = require(".");

describe("Tidy-HTML plugin", () => {
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeEach(() => {
Expand Down Expand Up @@ -62,10 +65,10 @@ describe("Tidy-HTML plugin", () => {
expect(response.body).toMatchSnapshot();
// Check language is set to default or options.language
expect(
dom.window.document.querySelector("html").getAttribute("lang")
dom.window.document.querySelector("html")?.getAttribute("lang")
).toBe(options?.language || "en");
expect(
dom.window.document.querySelector("html").getAttribute("xml:lang")
dom.window.document.querySelector("html")?.getAttribute("xml:lang")
).toBe(options?.language || "en");

// Check alt attributes are removed if options.removeAlt is true
Expand Down
3 changes: 3 additions & 0 deletions src/routes/admin/healthcheck/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const sharedSchemas = require("../../../plugins/shared-schemas");
describe("Healthcheck route", () => {
describe("GET requests", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/doc/txt/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const sharedSchemas = require("../../../plugins/shared-schemas");

describe("DOC-to-TXT route", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/docs/openapi/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const sharedSchemas = require("../../../plugins/shared-schemas");
describe("OpenAPI route", () => {
describe("GET requests", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down
3 changes: 3 additions & 0 deletions src/routes/docs/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const sharedSchemas = require("../../plugins/shared-schemas");
describe("Docs route", () => {
describe("GET requests", () => {
let config;
/**
* @type {Fastify.FastifyInstance}
*/
let server;

beforeAll(async () => {
Expand Down
Loading

0 comments on commit c0199d7

Please sign in to comment.