diff --git a/test/commands/scorecard.test.js b/test/commands/scorecard.test.js index 58ab6eef..d434e158 100644 --- a/test/commands/scorecard.test.js +++ b/test/commands/scorecard.test.js @@ -1,7 +1,8 @@ // Import Node.js Dependencies +import fs from "node:fs"; import { fileURLToPath } from "node:url"; import path from "node:path"; -import { test } from "node:test"; +import { test, mock } from "node:test"; import assert from "node:assert"; // Import Third-party Dependencies @@ -12,6 +13,7 @@ import { Ok } from "@openally/result"; // Import Internal Dependencies import { runProcess } from "../helpers/cliCommandRunner.js"; import { arrayFromAsync, getExpectedScorecardLines } from "../helpers/utils.js"; +import * as testingModule from "../../src/commands/scorecard.js"; // CONSTANTS const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -97,27 +99,51 @@ test("should not display scorecard for unknown repository", async() => { assert.deepEqual(givenLines, expectedLines, `lines should be ${expectedLines}`); }); -test("should retrieve repository whithin git config", async() => { - const testingModule = await esmock("../../src/commands/scorecard.js", { - fs: { - readFileSync: () => [ - "[remote \"origin\"]", - "\turl = git@github.com:myawesome/repository.git" - ].join("\n") - } - }); - - assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"])); +// test("should retrieve repository whithin git config", async() => { +// const testingModule = await esmock("../../src/commands/scorecard.js", { +// fs: { +// readFileSync: () => [ +// "[remote \"origin\"]", +// "\turl = git@github.com:myawesome/repository.git" +// ].join("\n") +// } +// }); + +// assert.deepEqual(testingModule.getCurrentRepository(), Ok(["myawesome/repository", "github"])); +// }); +test("should retrieve repository within git config", async () => { + const readFileSyncMock = mock.method(fs, 'readFileSync', () => + [ + '[remote "origin"]', + '\turl = git@github.com:myawesome/repository.git', + ].join('\n') + ); + + assert.deepEqual( + testingModule.getCurrentRepository(), + Ok(["myawesome/repository", "github"]) + ); + + readFileSyncMock.mock.restoreAll(); }); -test("should not find origin remote", async() => { - const testingModule = await esmock("../../src/commands/scorecard.js", { - fs: { - readFileSync: () => "just one line" - } - }); +// test("should not find origin remote", async() => { +// const testingModule = await esmock("../../src/commands/scorecard.js", { +// fs: { +// readFileSync: () => "just one line" +// } +// }); +// const result = testingModule.getCurrentRepository(); + +// assert.equal(result.err, true); +// assert.equal(result.val, "Cannot find origin remote."); +// }); +test("should not find origin remote", async () => { + const readFileSyncMock = mock.method(fs, 'readFileSync', () => "just one line"); const result = testingModule.getCurrentRepository(); assert.equal(result.err, true); assert.equal(result.val, "Cannot find origin remote."); -}); + + readFileSyncMock.mock.restoreAll(); +}); \ No newline at end of file diff --git a/test/httpServer.test.js b/test/httpServer.test.js index f59500d4..d1097564 100644 --- a/test/httpServer.test.js +++ b/test/httpServer.test.js @@ -1,10 +1,11 @@ // Import Node.js Dependencies -import fs from "node:fs"; +import fs, { createReadStream } from "node:fs"; import { fileURLToPath } from "node:url"; -import { after, before, describe, test } from "node:test"; +import { after, before, describe, test, mock } from "node:test"; import { once } from "node:events"; import path from "node:path"; import assert from "node:assert"; +import { pipeline as streamPipeline } from "node:stream"; // Import Third-party Dependencies import { get, post, MockAgent, getGlobalDispatcher, setGlobalDispatcher } from "@myunisoft/httpie"; @@ -14,10 +15,15 @@ import * as flags from "@nodesecure/flags"; import enableDestroy from "server-destroy"; import esmock from "esmock"; import cacache from "cacache"; +import sendType from "@polka/send-type"; // Require Internal Dependencies import { buildServer } from "../src/http-server/index.js"; import { CACHE_PATH } from "../src/http-server/cache.js"; +import * as rootModule from "../src/http-server/endpoints/root.js"; +import * as flagsModule from "../src/http-server/endpoints/flags.js"; +import * as indexModule from "../src/http-server/index.js"; + // CONSTANTS const HTTP_PORT = 17049; @@ -79,20 +85,34 @@ describe("httpServer", { concurrency: 1 }, () => { assert.equal(result.data, templateStr); }); - test("'/' should fail", async() => { + // test("'/' should fail", async() => { + // const errors = []; + // const module = await esmock("../src/http-server/endpoints/root.js", { + // "@polka/send-type": { + // default: (res, status, { error }) => errors.push(error) + // } + // }); + + + // await module.get({}, ({ + // writeHead: () => { + // throw new Error("fake error"); + // } + // })); + // assert.deepEqual(errors, ["fake error"]); + // }); + test("'/' should fail", () => { const errors = []; - const module = await esmock("../src/http-server/endpoints/root.js", { - "@polka/send-type": { - default: (res, status, { error }) => errors.push(error) - } - }); - - await module.get({}, ({ + const sendTypeMock = mock.method(sendType, "default", (res, status, { error }) => errors.push(error)); + + rootModule.get({}, { writeHead: () => { throw new Error("fake error"); - } - })); + }, + }); + assert.deepEqual(errors, ["fake error"]); + sendTypeMock.mock.restoreAll(); }); test("'/flags' should return the flags list as JSON", async() => { @@ -121,20 +141,33 @@ describe("httpServer", { concurrency: 1 }, () => { }); }); - test("'/flags/description/:title' should fail", async() => { - const module = await esmock("../src/http-server/endpoints/flags.js", { - stream: { - pipeline: (stream, res, err) => err("fake error") - }, - fs: { - createReadStream: () => "foo" - } - }); + // test("'/flags/description/:title' should fail", async() => { + // const module = await esmock("../src/http-server/endpoints/flags.js", { + // stream: { + // pipeline: (stream, res, err) => err("fake error") + // }, + // fs: { + // createReadStream: () => "foo" + // } + // }); + // const logs = []; + // console.error = (data) => logs.push(data); + + // await module.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true })); + // assert.deepEqual(logs, ["fake error"]); + // }); + test("'/flags/description/:title' should fail", () => { const logs = []; + const streamPipelineMock = mock.method(streamPipeline, "pipeline", (stream, res, err) => err("fake error")); + const createReadStreamMock = mock.method(createReadStream, "default", () => "foo"); console.error = (data) => logs.push(data); - - await module.get({ params: { title: "hasWarnings" } }, ({ writeHead: () => true })); + + flagsModule.get({ params: { title: "hasWarnings" } }, { writeHead: () => true }); + assert.deepEqual(logs, ["fake error"]); + + streamPipelineMock.mock.restoreAll(); + createReadStreamMock.mock.restoreAll(); }); test("'/data' should return the fixture payload we expect", async() => { @@ -320,23 +353,51 @@ describe("httpServer", { concurrency: 1 }, () => { }); }); +// describe("httpServer without options", () => { +// let httpServer; +// let opened = false; +// // We want to disable WS +// process.env.NODE_ENV = "test"; + +// before(async() => { +// const module = await esmock("../src/http-server/index.js", { +// open: () => (opened = true) +// }); + +// httpServer = module.buildServer(JSON_PATH); +// await once(httpServer.server, "listening"); +// enableDestroy(httpServer.server); +// }); + +// after(async() => { +// httpServer.server.destroy(); +// }); + +// test("should listen on random port", () => { +// assert.ok(httpServer.server.address().port > 0); +// }); + +// test("should have openLink to true", () => { +// assert.equal(opened, true); +// }); +// }); describe("httpServer without options", () => { let httpServer; let opened = false; - // We want to disable WS process.env.NODE_ENV = "test"; - before(async() => { - const module = await esmock("../src/http-server/index.js", { - open: () => (opened = true) + before(async () => { + const openMock = mock.method(indexModule, "open", () => { + opened = true; }); - httpServer = module.buildServer(JSON_PATH); + httpServer = indexModule.buildServer(JSON_PATH); await once(httpServer.server, "listening"); enableDestroy(httpServer.server); + openMock.mock.restoreAll(); }); - after(async() => { + after(async () => { httpServer.server.destroy(); }); @@ -349,6 +410,8 @@ describe("httpServer without options", () => { }); }); + + /** * HELPERS */