Skip to content

Commit

Permalink
test(*): update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaslz committed Jan 19, 2024
1 parent ef2979f commit b69dcbe
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 201 deletions.
7 changes: 7 additions & 0 deletions packages/cli/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from "fs";
import shell from "shelljs";
import { beforeEach, vi } from "vitest";

Expand All @@ -20,4 +21,10 @@ beforeEach(() => {
mkcert: vi.fn(),
};
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn(fs, "readdirSync").mockImplementationOnce((): any[] => [
"some-domain.com-cert.pem",
"some-domain.com-key.pem",
]);
});
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"build": "ncc build src/index.ts -o dist",
"to-publish": "npx vite-node publish.js",
"test": "vitest --reporter=verbose",
"lint": "oxlint ."
"lint": "eslint ./src"
},
"dependencies": {
"@dimaslz/local-ssl-management-core": "*",
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/generate-proxy-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ describe("Generate proxy image", () => {

describe("failure", () => {
test("does not exists config to create reverse proxy", () => {
expect(() => {
generateProxyImage([]);
}).toThrow();
generateProxyImage([]);

expect(consola.warn).toBeCalledWith(
"Does not exists config to create reverse proxy",
);
expect(shell.exit).toBeCalledWith(1);
});
});

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/generate-proxy-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ const renderTable = (config: Config[]) => {
const generateProxyImage = (config: Config[]) => {
if (!config.length) {
consola.warn("Does not exists config to create reverse proxy");
shell.exit(1);

return;
}

const localhostCertExists = fs.existsSync(sslPath + "/localhost-cert.pem");
Expand Down
7 changes: 2 additions & 5 deletions packages/cli/src/list-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ describe("List container", () => {
};
});

expect(() => {
listContainer();
}).toThrow();
listContainer();

expect(consola.error).toBeCalledWith(
new Error("Something have been failure. Contact with the author."),
"Something have been failure. Contact with the author.",
);
expect(shell.exit).toBeCalledTimes(1);
});
});
});
7 changes: 3 additions & 4 deletions packages/cli/src/list-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ const listContainer = () => {
.find((line) => /local-ssl-management/.test(line)) || "";

if (!containerData) {
consola.error(
new Error("Something have been failure. Contact with the author."),
);
shell.exit(1);
consola.error("Something have been failure. Contact with the author.");

return;
}

const [containerId, containerName, ports] = containerData
Expand Down
89 changes: 32 additions & 57 deletions packages/cli/src/on-create-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,49 @@ describe("On create action", () => {
const domain = "wrong.domain";
const port = "3000";

expect(() => {
onCreateAction(domain, { port });
}).toThrow();
vi.spyOn(shell, "exec")
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.mockImplementationOnce((): any => "")
.mockImplementationOnce(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(): any => {
return { stdout: 200 };
},
);

onCreateAction(domain, { port });

expect(consola.error).toBeCalledWith(
new Error("Domain (https://wrong.domain) format is not valid"),
"Domain (https://wrong.domain) format is not valid",
);
expect(shell.exit).toHaveBeenCalledWith(1);
});

test("can not create with invalid port", () => {
const domain = "some-domain.com";
const port = "666";

expect(() => {
onCreateAction(domain, { port });
}).toThrow();
vi.spyOn(shell, "exec")
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.mockImplementationOnce((): any => "")
.mockImplementationOnce(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(): any => {
return { stdout: 200 };
},
);

onCreateAction(domain, { port });

expect(consola.error).toBeCalledWith(
new Error(
"Port (--port <port>) should be into the range 1025 to 65535",
),
"Port (--port <port>) should be into the range 1025 to 65535",
);
expect(shell.exit).toHaveBeenCalledWith(1);
});

test("can not create duplicated location", () => {
const domain = "some-domain.com";
const port = "3000";
const location = "/app-name";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn(fs, "readdirSync").mockImplementationOnce((): any[] => [
"some-domain.com-cert.pem",
"some-domain.com-key.pem",
]);

vi.spyOn(fs, "readFileSync").mockReturnValueOnce(
JSON.stringify([
{
Expand All @@ -76,25 +82,16 @@ describe("On create action", () => {
]),
);

expect(() => {
onCreateAction(domain, { port, location });
}).toThrow();
onCreateAction(domain, { port, location });

expect(consola.error).toBeCalledWith(
new Error('Location "/app-name" already exists'),
'Location "/app-name" already exists',
);
expect(shell.exit).toHaveBeenCalledWith(1);
});

test("can not create duplicated domain", () => {
const domain = "some-domain.com";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn(fs, "readdirSync").mockImplementationOnce((): any[] => [
"some-domain.com-cert.pem",
"some-domain.com-key.pem",
]);

vi.spyOn(fs, "readFileSync").mockReturnValueOnce(
JSON.stringify([
{
Expand All @@ -116,28 +113,16 @@ describe("On create action", () => {
]),
);

expect(() => {
onCreateAction(domain, {});
}).toThrow();
onCreateAction(domain, {});

expect(consola.error).toBeCalledWith(
new Error(
'Domain "some-domain.com" already created with the default location "/"',
),
'Domain "some-domain.com" already created with the default location "/"',
);
expect(shell.exit).toHaveBeenCalledWith(1);
});
});

describe("success", () => {
beforeEach(() => {
vi.spyOn(fs, "mkdirSync");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn(fs, "readdirSync").mockImplementationOnce((): any[] => [
"some-domain.com-cert.pem",
"some-domain.com-key.pem",
]);

vi.spyOn(shell, "exec")
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.mockImplementationOnce((): any => "")
Expand Down Expand Up @@ -290,16 +275,11 @@ describe("On create action", () => {
const domain = "some-domain.com";
const port = "3000";

expect(() => {
onCreateAction(domain, { port });
}).toThrow();
onCreateAction(domain, { port });

expect(consola.error).toBeCalledWith(
new Error(
'Domain "some-domain.com" already created with the default location "/"',
),
'Domain "some-domain.com" already created with the default location "/"',
);
expect(shell.exit).toHaveBeenCalledWith(1);
});
});

Expand Down Expand Up @@ -479,16 +459,11 @@ describe("On create action", () => {
const domain = "some-domain.com";
const port = "3000";

expect(() => {
onCreateAction(domain, { port });
}).toThrow();
onCreateAction(domain, { port });

expect(consola.error).toBeCalledWith(
new Error(
'Domain "some-domain.com" already created with the default location "/"',
),
'Domain "some-domain.com" already created with the default location "/"',
);
expect(shell.exit).toHaveBeenCalledWith(1);
});
});
});
Expand Down
19 changes: 9 additions & 10 deletions packages/cli/src/on-create-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import consola from "consola";
import crypto from "crypto";
import fs from "fs";
import path from "path";
import shell from "shelljs";

import generateProxyImage from "./generate-proxy-image";
import { validateDomain, validateLocation, validatePort } from "./utils";
Expand Down Expand Up @@ -65,29 +64,29 @@ const onCreateAction = (
if (domainExists && locationExists) {
if (location === "/") {
consola.error(
new Error(
`Domain "${domain}" already created with the default location "${location}"`,
),
`Domain "${domain}" already created with the default location "${location}"`,
);
} else {
consola.error(new Error(`Location "${location}" already exists`));
consola.error(`Location "${location}" already exists`);
}

shell.exit(1);
return;
}

if (domainExists && locationExists) {
consola.error(new Error(`Domain "${domain}" already exists`));
shell.exit(1);
consola.error(`Domain "${domain}" already exists`);

return;
}

const portExists =
config[domainIndex]?.services.some((service) => service.port === port) ||
false;

if (portExists) {
consola.error(new Error(`Port "${port}" already exists`));
shell.exit(1);
consola.error(`Port "${port}" already exists`);

return;
}

if (domainIndex > -1) {
Expand Down
5 changes: 1 addition & 4 deletions packages/cli/src/on-list-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ describe("On list action", () => {
test("no domains available", () => {
vi.spyOn(fs, "readFileSync").mockReturnValue("[]");

expect(() => {
onListAction();
}).toThrow();
onListAction();

expect(consola.box).toBeCalledWith(`Does not exists configs yet.`);
expect(shell.exit).toBeCalledTimes(1);
});

test("list domains availables", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/on-list-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Config } from "@dimaslz/local-ssl-management-core";
import consola from "consola";
import fs from "fs";
import path from "path";
import shell from "shelljs";

import { listConfigs } from "./utils";

Expand All @@ -17,7 +16,8 @@ const onListAction = () => {

if (!config.length) {
consola.box("Does not exists configs yet.");
shell.exit(1);

return;
}

listConfigs(config);
Expand Down
29 changes: 8 additions & 21 deletions packages/cli/src/on-remove-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,19 @@ describe("On remove action", () => {
test("removing domain by name does not exists", () => {
vi.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify([]));

expect(() => {
onRemoveAction("dummy");
}).toThrow();
onRemoveAction("dummy");

expect(consola.error).toBeCalledWith(
new Error('Domain "dummy" does not exists'),
);
expect(shell.exit).toHaveBeenCalledWith(1);
expect(consola.error).toBeCalledWith('Domain "dummy" does not exists');
});

test("remove domain by id does not exists", () => {
vi.spyOn(fs, "readFileSync").mockReturnValueOnce(JSON.stringify([]));

expect(() => {
onRemoveAction("6eb61d17-ba78-4618-a2ac-47aeb4ba8b26");
}).toThrow();
onRemoveAction("6eb61d17-ba78-4618-a2ac-47aeb4ba8b26");

expect(consola.error).toBeCalledWith(
new Error(
'Domain with id "6eb61d17-ba78-4618-a2ac-47aeb4ba8b26" does not exists',
),
'Domain with id "6eb61d17-ba78-4618-a2ac-47aeb4ba8b26" does not exists',
);
expect(shell.exit).toHaveBeenCalledWith(1);
});

test("removing path does not exists", () => {
Expand All @@ -65,16 +55,13 @@ describe("On remove action", () => {
),
);

expect(() => {
onRemoveAction("6eb61d17-ba78-4618-a2ac-47aeb4ba8b26", {
location: "/something",
});
}).toThrow();
onRemoveAction("6eb61d17-ba78-4618-a2ac-47aeb4ba8b26", {
location: "/something",
});

expect(consola.error).toBeCalledWith(
new Error('Location "/something" does not exists'),
'Location "/something" does not exists',
);
expect(shell.exit).toHaveBeenCalledWith(1);
});
});

Expand Down
Loading

0 comments on commit b69dcbe

Please sign in to comment.