@n42/cli 0.2.42 → 0.2.72
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yaml +36 -0
- package/.github/workflows/cli-test-npm.yaml +66 -0
- package/README.md +12 -7
- package/package.json +28 -5
- package/src/assets/{wrapper.html.template → discover.html.template} +1 -1
- package/src/assets/validator-light.css +49 -0
- package/src/assets/validator.html.template +27 -0
- package/src/assets/wrapper-light.css +7 -0
- package/src/cli.js +46 -23
- package/src/completion/bash.sh +7 -1
- package/src/config.js +4 -1
- package/src/discover.js +1 -1
- package/src/utils.js +6 -1
- package/src/validator.js +286 -0
- package/test/asserts/validate_tests.js +28 -0
- package/test/cli.test.js +83 -0
- package/test/discover.test.js +112 -0
- package/test/errors.test.js +61 -0
- package/test/user.test.js +103 -0
- package/test/utils.test.js +105 -7
- package/test/validator.test.js +66 -0
- package/jest.config.js +0 -6
- package/src/browser.js +0 -11
- /package/src/assets/{wrapper.js → discover.js} +0 -0
package/test/utils.test.js
CHANGED
|
@@ -1,15 +1,83 @@
|
|
|
1
|
-
const { buildDocLabel } = require("../src/utils");
|
|
2
1
|
const { expect } = require("chai");
|
|
2
|
+
const sinon = require("sinon");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const os = require("os");
|
|
6
|
+
|
|
7
|
+
const config = require("../src/config");
|
|
8
|
+
const db = require("../src/db");
|
|
9
|
+
|
|
10
|
+
const TEST_ROOT = path.join(os.tmpdir(), "node42-utils-test");
|
|
3
11
|
|
|
4
12
|
describe("utils", () => {
|
|
13
|
+
let utils;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
sinon.restore();
|
|
17
|
+
|
|
18
|
+
// isolate fs paths
|
|
19
|
+
sinon.stub(config, "NODE42_DIR").value(TEST_ROOT);
|
|
20
|
+
sinon.stub(config, "ARTEFACTS_DIR").value(path.join(TEST_ROOT, "artefacts"));
|
|
21
|
+
sinon.stub(config, "TRANSACTIONS_DIR").value(path.join(TEST_ROOT, "transactions"));
|
|
22
|
+
sinon.stub(config, "VALIDATIONS_DIR").value(path.join(TEST_ROOT, "validations"));
|
|
23
|
+
sinon.stub(config, "CONFIG_FILE").value(path.join(TEST_ROOT, "config.json"));
|
|
24
|
+
sinon.stub(config, "TOKENS_FILE").value(path.join(TEST_ROOT, "tokens.json"));
|
|
25
|
+
sinon.stub(config, "DATABASE_FILE").value(path.join(TEST_ROOT, "db.json"));
|
|
26
|
+
|
|
27
|
+
if (fs.existsSync(TEST_ROOT)) {
|
|
28
|
+
fs.rmSync(TEST_ROOT, { recursive: true, force: true });
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
delete require.cache[require.resolve("../src/utils")];
|
|
32
|
+
utils = require("../src/utils");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
afterEach(() => {
|
|
36
|
+
sinon.restore();
|
|
37
|
+
if (fs.existsSync(TEST_ROOT)) {
|
|
38
|
+
fs.rmSync(TEST_ROOT, { recursive: true, force: true });
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
/* ---------- PURE HELPERS ---------- */
|
|
43
|
+
|
|
44
|
+
it("capitalizes string", () => {
|
|
45
|
+
expect(utils.capitalize("test")).to.equal("Test");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("returns short id", () => {
|
|
49
|
+
expect(utils.getShortId("1234567890")).to.equal("12345678");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("returns artefact extension", () => {
|
|
53
|
+
expect(utils.getArtefactExt("plantuml", "svg")).to.equal("svg");
|
|
54
|
+
expect(utils.getArtefactExt("plantuml", "text")).to.equal("puml");
|
|
55
|
+
expect(utils.getArtefactExt("json")).to.equal("json");
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
/* ---------- VALIDATION ---------- */
|
|
59
|
+
|
|
60
|
+
it("validates environment", () => {
|
|
61
|
+
expect(() => utils.validateEnv("TEST")).to.not.throw();
|
|
62
|
+
expect(() => utils.validateEnv("prod")).to.not.throw();
|
|
63
|
+
expect(() => utils.validateEnv("BAD")).to.throw();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("validates participant id", () => {
|
|
67
|
+
expect(() => utils.validateId("participant", "9915:abc123")).to.not.throw();
|
|
68
|
+
expect(() => utils.validateId("participant", "bad-id")).to.throw();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/* ---------- buildDocLabel ---------- */
|
|
72
|
+
|
|
5
73
|
describe("buildDocLabel()", () => {
|
|
6
74
|
it("formats wildcard invoice", () => {
|
|
7
75
|
const doc = {
|
|
8
|
-
|
|
9
|
-
|
|
76
|
+
scheme: "peppol-doctype-wildcard",
|
|
77
|
+
value: "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2::Invoice##*"
|
|
10
78
|
};
|
|
11
79
|
|
|
12
|
-
expect(buildDocLabel(doc)).to.equal("Any Invoice (Wildcard)");
|
|
80
|
+
expect(utils.buildDocLabel(doc)).to.equal("Any Invoice (Wildcard)");
|
|
13
81
|
});
|
|
14
82
|
|
|
15
83
|
it("formats BIS invoice", () => {
|
|
@@ -18,12 +86,42 @@ describe("utils", () => {
|
|
|
18
86
|
value: "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2::Invoice##urn:cen.eu:en16931:2017"
|
|
19
87
|
};
|
|
20
88
|
|
|
21
|
-
expect(buildDocLabel(doc)).to.equal("Invoice (BIS 3)");
|
|
89
|
+
expect(utils.buildDocLabel(doc)).to.equal("Invoice (BIS 3)");
|
|
22
90
|
});
|
|
23
91
|
|
|
24
92
|
it("falls back on 'unknown' value", () => {
|
|
25
93
|
const doc = { scheme: "busdox-docid-qns", value: "unknown" };
|
|
26
|
-
expect(buildDocLabel(doc)).to.equal("Document");
|
|
94
|
+
expect(utils.buildDocLabel(doc)).to.equal("Document");
|
|
27
95
|
});
|
|
28
96
|
});
|
|
29
|
-
|
|
97
|
+
|
|
98
|
+
/* ---------- FS / SIDE EFFECTS ---------- */
|
|
99
|
+
|
|
100
|
+
it("creates application directories and config", () => {
|
|
101
|
+
utils.createAppDirs(true);
|
|
102
|
+
|
|
103
|
+
expect(fs.existsSync(config.NODE42_DIR)).to.be.true;
|
|
104
|
+
expect(fs.existsSync(config.ARTEFACTS_DIR)).to.be.true;
|
|
105
|
+
expect(fs.existsSync(config.CONFIG_FILE)).to.be.true;
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("cleans artefacts directory", () => {
|
|
109
|
+
fs.mkdirSync(config.ARTEFACTS_DIR, { recursive: true });
|
|
110
|
+
fs.writeFileSync(path.join(config.ARTEFACTS_DIR, "x"), "x");
|
|
111
|
+
|
|
112
|
+
sinon.stub(db, "clear");
|
|
113
|
+
|
|
114
|
+
utils.cleanAppDirs({ artefacts: true });
|
|
115
|
+
|
|
116
|
+
expect(fs.existsSync(config.ARTEFACTS_DIR)).to.be.true;
|
|
117
|
+
expect(fs.readdirSync(config.ARTEFACTS_DIR)).to.have.length(0);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("prints message when nothing to clean", () => {
|
|
121
|
+
sinon.stub(console, "log");
|
|
122
|
+
|
|
123
|
+
utils.cleanAppDirs({});
|
|
124
|
+
|
|
125
|
+
expect(console.log.calledWithMatch("Nothing to clean")).to.be.true;
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const { expect } = require("chai");
|
|
2
|
+
const sinon = require("sinon");
|
|
3
|
+
|
|
4
|
+
const utils = require("../src/utils");
|
|
5
|
+
const errors = require("../src/errors");
|
|
6
|
+
|
|
7
|
+
describe("runValidation()", () => {
|
|
8
|
+
let validator;
|
|
9
|
+
let fetchStub;
|
|
10
|
+
let spinnerStub;
|
|
11
|
+
let handleErrorStub;
|
|
12
|
+
let exitStub;
|
|
13
|
+
|
|
14
|
+
beforeEach(() => {
|
|
15
|
+
sinon.restore();
|
|
16
|
+
|
|
17
|
+
delete require.cache[require.resolve("../src/auth")];
|
|
18
|
+
delete require.cache[require.resolve("../src/validator")];
|
|
19
|
+
|
|
20
|
+
const auth = require("../src/auth");
|
|
21
|
+
fetchStub = sinon.stub(auth, "fetchWithAuth").resolves({
|
|
22
|
+
ok: true,
|
|
23
|
+
json: async () => ({ sections: [] })
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
handleErrorStub = sinon.stub(errors, "handleError").resolves();
|
|
27
|
+
spinnerStub = sinon.stub(utils, "startSpinner").returns(() => {});
|
|
28
|
+
exitStub = sinon.stub(process, "exit").throws(new Error("exit"));
|
|
29
|
+
|
|
30
|
+
validator = require("../src/validator");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
afterEach(() => sinon.restore());
|
|
34
|
+
|
|
35
|
+
it("sends XML to validator endpoint and handles successful response", async () => {
|
|
36
|
+
await validator.runValidation(
|
|
37
|
+
"Invoice.xml",
|
|
38
|
+
"<xml></xml>",
|
|
39
|
+
{ ruleset: "current" }
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
expect(fetchStub.calledOnce).to.equal(true);
|
|
43
|
+
expect(handleErrorStub.notCalled).to.equal(true);
|
|
44
|
+
expect(exitStub.notCalled).to.equal(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("handles validator error response and exits", async () => {
|
|
48
|
+
fetchStub.resolves({
|
|
49
|
+
ok: false,
|
|
50
|
+
status: 400,
|
|
51
|
+
json: async () => ({ code: "VALIDATION_FAILED" })
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
await validator.runValidation(
|
|
56
|
+
"Invoice.xml",
|
|
57
|
+
"<xml></xml>",
|
|
58
|
+
{ ruleset: "current" }
|
|
59
|
+
);
|
|
60
|
+
} catch {}
|
|
61
|
+
|
|
62
|
+
expect(fetchStub.calledOnce).to.equal(true);
|
|
63
|
+
expect(handleErrorStub.calledOnce).to.equal(true);
|
|
64
|
+
expect(process.exit.calledWith(1)).to.be.true;
|
|
65
|
+
});
|
|
66
|
+
});
|
package/jest.config.js
DELETED
package/src/browser.js
DELETED
|
File without changes
|