Skip to content

Commit

Permalink
chore: merge configuration and related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlementlegen committed May 12, 2024
1 parent 9187241 commit 6c4f4da
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/configuration/config-reader.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
}

Expand Down
137 changes: 137 additions & 0 deletions tests/__mocks__/results/valid-configuration.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
24 changes: 24 additions & 0 deletions tests/process.issue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,31 @@ 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";

jest.spyOn(ContentEvaluatorModule.prototype, "_evaluateComments").mockImplementation((specification, comments) => {
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");

Expand Down Expand Up @@ -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);
});
});
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 6c4f4da

Please sign in to comment.