From 6c4f4da0da968aaf011454dbe49ce1917612cdd5 Mon Sep 17 00:00:00 2001 From: gentlementlegen Date: Mon, 13 May 2024 00:44:45 +0900 Subject: [PATCH] chore: merge configuration and related tests --- package.json | 1 + src/configuration/config-reader.ts | 6 +- .../results/valid-configuration.json | 137 ++++++++++++++++++ tests/process.issue.test.ts | 24 +++ yarn.lock | 2 +- 5 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 tests/__mocks__/results/valid-configuration.json diff --git a/package.json b/package.json index d9c8de6a..c1547886 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "dotenv": "16.4.5", "js-tiktoken": "1.0.10", "jsdom": "24.0.0", + "lodash": "4.17.21", "markdown-it": "14.1.0", "openai": "4.29.1", "tsx": "4.7.1", diff --git a/src/configuration/config-reader.ts b/src/configuration/config-reader.ts index d6e8a2f6..1310ee94 100644 --- a/src/configuration/config-reader.ts +++ b/src/configuration/config-reader.ts @@ -1,6 +1,7 @@ import program from "../parser/command-line"; import { IncentivesConfiguration, incentivesConfigurationSchema, validateIncentivesConfiguration } from "./incentives"; import { Value } from "@sinclair/typebox/value"; +import { merge } from "lodash"; let configuration: IncentivesConfiguration | null = null; @@ -14,9 +15,12 @@ try { if (program.opts().settings) { const settings = JSON.parse(program.opts().settings); if (validateIncentivesConfiguration.test(settings)) { - configuration = settings; + configuration = merge(configuration, settings); } else { console.warn("Invalid incentives configuration detected, will revert to defaults."); + for (const error of validateIncentivesConfiguration.errors(settings)) { + console.warn(error); + } } } diff --git a/tests/__mocks__/results/valid-configuration.json b/tests/__mocks__/results/valid-configuration.json new file mode 100644 index 00000000..4025b43a --- /dev/null +++ b/tests/__mocks__/results/valid-configuration.json @@ -0,0 +1,137 @@ +{ + "incentives": { + "enabled": false, + "contentEvaluator": { + "enabled": true + }, + "userExtractor": { + "enabled": true, + "redeemTask": true + }, + "dataPurge": { + "enabled": true + }, + "formattingEvaluator": { + "enabled": true, + "multipliers": [ + { + "type": [ + "ISSUE", + "ISSUER", + "SPECIFICATION" + ], + "formattingMultiplier": 1, + "wordValue": 0.1 + }, + { + "type": [ + "ISSUE", + "ISSUER", + "COMMENTED" + ], + "formattingMultiplier": 1, + "wordValue": 0.2 + }, + { + "type": [ + "ISSUE", + "ASSIGNEE", + "COMMENTED" + ], + "formattingMultiplier": 0, + "wordValue": 0 + }, + { + "type": [ + "ISSUE", + "COLLABORATOR", + "COMMENTED" + ], + "formattingMultiplier": 1, + "wordValue": 0.1 + }, + { + "type": [ + "ISSUE", + "CONTRIBUTOR", + "COMMENTED" + ], + "formattingMultiplier": 0.25, + "wordValue": 0.1 + }, + { + "type": [ + "REVIEW", + "ISSUER", + "TASK" + ], + "formattingMultiplier": 0, + "wordValue": 0 + }, + { + "type": [ + "REVIEW", + "ISSUER", + "COMMENTED" + ], + "formattingMultiplier": 2, + "wordValue": 0.2 + }, + { + "type": [ + "REVIEW", + "ASSIGNEE", + "COMMENTED" + ], + "formattingMultiplier": 1, + "wordValue": 0.1 + }, + { + "type": [ + "REVIEW", + "COLLABORATOR", + "COMMENTED" + ], + "formattingMultiplier": 1, + "wordValue": 0.1 + }, + { + "type": [ + "REVIEW", + "CONTRIBUTOR", + "COMMENTED" + ], + "formattingMultiplier": 0.25, + "wordValue": 0.1 + } + ], + "scores": { + "br": 0, + "code": 1, + "p": 1, + "em": 0, + "img": 0, + "strong": 0, + "blockquote": 0, + "h1": 1, + "h2": 1, + "h3": 1, + "h4": 1, + "h5": 1, + "h6": 1, + "a": 1, + "li": 1, + "td": 1, + "hr": 0 + } + }, + "permitGeneration": { + "enabled": true + }, + "githubComment": { + "enabled": true, + "post": true, + "debug": false + } + } +} diff --git a/tests/process.issue.test.ts b/tests/process.issue.test.ts index 18768bc6..d2832822 100644 --- a/tests/process.issue.test.ts +++ b/tests/process.issue.test.ts @@ -18,6 +18,9 @@ import { PermitGenerationModule } from "../src/parser/permit-generation-module"; import { db as mockDb } from "./__mocks__/db"; import { GithubCommentModule } from "../src/parser/github-comment-module"; import fs from "fs"; +import configuration from "../src/configuration/config-reader"; +import validConfiguration from "./__mocks__/results/valid-configuration.json"; +import "../src/parser/command-line"; const issueUrl = process.env.TEST_ISSUE_URL || "https://github.com/ubiquibot/comment-incentives/issues/22"; @@ -25,6 +28,21 @@ jest.spyOn(ContentEvaluatorModule.prototype, "_evaluateComments").mockImplementa return Promise.resolve(comments.map(() => new Decimal(0.8))); }); +jest.mock("../src/parser/command-line", () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const cfg = require("./__mocks__/results/valid-configuration.json"); + return { + opts: jest.fn(() => { + return { + settings: JSON.stringify({ + ...cfg, + incentives: { ...cfg.incentives, enabled: false }, + }), + }; + }), + }; +}); + jest.mock("@ubiquibot/permit-generation/core", () => { const originalModule = jest.requireActual("@ubiquibot/permit-generation/core"); @@ -151,4 +169,10 @@ describe("Modules tests", () => { expect(fs.readFileSync("./output.html")).toEqual(fs.readFileSync("./tests/__mocks__/results/output.html")); logSpy.mockReset(); }); + + it("Should properly generate the configuration", () => { + const cfg = configuration; + + expect(cfg).toEqual(validConfiguration); + }); }); diff --git a/yarn.lock b/yarn.lock index 4ea601ed..4a488839 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6450,7 +6450,7 @@ lodash.upperfirst@^4.3.1: resolved "https://registry.yarnpkg.com/lodash.upperfirst/-/lodash.upperfirst-4.3.1.tgz#1365edf431480481ef0d1c68957a5ed99d49f7ce" integrity sha512-sReKOYJIJf74dhJONhU4e0/shzi1trVbSWDOhKYE5XV2O+H7Sb2Dihwuc7xWxVl+DgFPyTqIN3zMfT9cq5iWDg== -lodash@^4.17.15, lodash@^4.17.21: +lodash@4.17.21, lodash@^4.17.15, lodash@^4.17.21: version "4.17.21" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==