aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2019-12-11 09:40:22 -0500
committerKevin J Hoerr <kjhoerr@protonmail.com>2019-12-11 09:40:22 -0500
commit3ae4f9352ee73a21581735883b4049327dce07bf (patch)
tree6c24d2e14cf37474b6580d4b31cc4ec5ac215742 /src
parente51ba6220576172b39d1a96a9ab4dfba87769428 (diff)
downloadao-coverage-3ae4f9352ee73a21581735883b4049327dce07bf.tar.gz
ao-coverage-3ae4f9352ee73a21581735883b4049327dce07bf.tar.bz2
ao-coverage-3ae4f9352ee73a21581735883b4049327dce07bf.zip
Add Jest to handle unit and integration testing
Wrote a couple of initial unit tests for some of the logic-heavy points in the application, being logger.ts and formats.ts. Evidently colorize() causes an error when transforming on the Format object that's returned. Since that is specific to the Console transport anyways, I just moved it to the transport, since the unit test was only testing the passed label in the returned Formats object. Also the bash template has some issues that I didn't test (oops), so those are fixed now.
Diffstat (limited to 'src')
-rw-r--r--src/formats.test.ts57
-rw-r--r--src/formats.ts9
-rw-r--r--src/util/logger.test.ts24
-rw-r--r--src/util/logger.ts19
4 files changed, 96 insertions, 13 deletions
diff --git a/src/formats.test.ts b/src/formats.test.ts
new file mode 100644
index 0000000..fa22f07
--- /dev/null
+++ b/src/formats.test.ts
@@ -0,0 +1,57 @@
+import { defaultColorMatches } from "./formats";
+
+describe("Color matcher", () => {
+ it.each`
+ n | s1 | s2 | expected
+ ${100} | ${75} | ${50} | ${"0f0"}
+ ${75} | ${75} | ${50} | ${"0f0"}
+ ${50} | ${75} | ${50} | ${"ff0"}
+ ${0} | ${75} | ${50} | ${"f00"}
+ ${51} | ${51} | ${50} | ${"0f0"}
+ ${50} | ${51} | ${50} | ${"ff0"}
+ ${0} | ${50} | ${1} | ${"f00"}
+ ${100} | ${100} | ${0} | ${"0f0"}
+ ${0} | ${100} | ${0} | ${"ff0"}
+ ${75} | ${75} | ${60} | ${"0f0"}
+ ${74} | ${75} | ${60} | ${"1f0"}
+ ${73} | ${75} | ${60} | ${"2f0"}
+ ${72} | ${75} | ${60} | ${"3f0"}
+ ${71} | ${75} | ${60} | ${"4f0"}
+ ${70} | ${75} | ${60} | ${"5f0"}
+ ${69} | ${75} | ${60} | ${"6f0"}
+ ${68} | ${75} | ${60} | ${"7f0"}
+ ${67} | ${75} | ${60} | ${"8f0"}
+ ${66} | ${75} | ${60} | ${"9f0"}
+ ${65} | ${75} | ${60} | ${"af0"}
+ ${64} | ${75} | ${60} | ${"bf0"}
+ ${63} | ${75} | ${60} | ${"cf0"}
+ ${62} | ${75} | ${60} | ${"df0"}
+ ${61} | ${75} | ${60} | ${"ef0"}
+ ${60} | ${75} | ${60} | ${"ff0"}
+ ${15} | ${75} | ${15} | ${"ff0"}
+ ${14} | ${75} | ${15} | ${"fe0"}
+ ${13} | ${75} | ${15} | ${"fd0"}
+ ${12} | ${75} | ${15} | ${"fc0"}
+ ${11} | ${75} | ${15} | ${"fb0"}
+ ${10} | ${75} | ${15} | ${"fa0"}
+ ${9} | ${75} | ${15} | ${"f90"}
+ ${8} | ${75} | ${15} | ${"f80"}
+ ${7} | ${75} | ${15} | ${"f70"}
+ ${6} | ${75} | ${15} | ${"f60"}
+ ${5} | ${75} | ${15} | ${"f50"}
+ ${4} | ${75} | ${15} | ${"f40"}
+ ${3} | ${75} | ${15} | ${"f30"}
+ ${2} | ${75} | ${15} | ${"f20"}
+ ${1} | ${75} | ${15} | ${"f10"}
+ ${0} | ${75} | ${15} | ${"f00"}
+ `("should return $expected at $n%", ({ n, s1, s2, expected }) => {
+ // Arrange
+ const gradient = { stage1: s1, stage2: s2 };
+
+ // Act
+ const result = defaultColorMatches(n, gradient);
+
+ // Assert
+ expect(result).toEqual(expected);
+ });
+});
diff --git a/src/formats.ts b/src/formats.ts
index e878399..86d3f6b 100644
--- a/src/formats.ts
+++ b/src/formats.ts
@@ -24,7 +24,7 @@ export interface GradientStyle {
}
// color is a gradient from green (>=stage_1) -> yellow (stage_2) -> red. Stage values should come from metadata.
-const defaultColorMatches = (
+export const defaultColorMatches = (
coverage: number,
style: GradientStyle
): string => {
@@ -32,9 +32,12 @@ const defaultColorMatches = (
coverage >= style.stage1
? 15
: coverage >= style.stage2
- ? (Math.floor(coverage) - style.stage2) * 16 + 15
+ ? Math.floor(
+ ((style.stage1 - coverage) / (style.stage1 - style.stage2)) * 240
+ ) + 15
: 240 + Math.floor(coverage / (style.stage2 / 15));
- return gradient.toString(16) + "0";
+ const result = gradient.toString(16);
+ return (result.length === 1 ? "0" : "") + result + "0";
};
const FormatsObj: FormatObj = {
diff --git a/src/util/logger.test.ts b/src/util/logger.test.ts
new file mode 100644
index 0000000..ca181c4
--- /dev/null
+++ b/src/util/logger.test.ts
@@ -0,0 +1,24 @@
+import configureLogger from "./logger";
+
+describe("Logger configurer", () => {
+ it("should set passed clazz as label", () => {
+ // Arrange
+ const clazz = "important-clazz-name";
+
+ // Act
+ const result = configureLogger(clazz);
+
+ // Assert
+ const adapter = {
+ level: "info",
+ message: "test/10"
+ };
+ const actual = result.format.transform(Object.assign({}, adapter));
+ expect(typeof actual).not.toEqual("boolean");
+ if (typeof actual !== "boolean") {
+ expect(actual.level).toEqual(adapter.level);
+ expect(actual.message).toEqual(adapter.message);
+ expect(actual.label).toEqual(clazz);
+ }
+ });
+});
diff --git a/src/util/logger.ts b/src/util/logger.ts
index d108ae0..27eeefe 100644
--- a/src/util/logger.ts
+++ b/src/util/logger.ts
@@ -6,6 +6,13 @@ const { Console } = winston.transports;
const LOG_LEVEL = process.env.LOG_LEVEL || "info";
+const consoleFormat = combine(
+ colorize(),
+ printf(({ level, message, label, timestamp }) => {
+ return `${timestamp} [${label}] ${level}: ${message}`;
+ })
+);
+
/**
* Provides standard logging format and output for the server.
*/
@@ -16,14 +23,6 @@ export default (
format: Format;
transports: Transport[];
} => ({
- format: combine(
- splat(),
- timestamp(),
- label({ label: clazz }),
- colorize(),
- printf(({ level, message, label, timestamp }) => {
- return `${timestamp} [${label}] ${level}: ${message}`;
- })
- ),
- transports: [new Console({ level: level })]
+ format: combine(splat(), timestamp(), label({ label: clazz })),
+ transports: [new Console({ level: level, format: consoleFormat })]
});