@adobe/ccweb-add-on-analytics 0.1.0
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/.c8rc.json +14 -0
- package/.mocharc.json +5 -0
- package/CODE_OF_CONDUCT.md +79 -0
- package/CONTRIBUTING.md +47 -0
- package/LICENSE +21 -0
- package/README.md +13 -0
- package/dist/app/AnalyticsConsent.d.ts +40 -0
- package/dist/app/AnalyticsConsent.d.ts.map +1 -0
- package/dist/app/AnalyticsConsent.js +25 -0
- package/dist/app/AnalyticsService.d.ts +47 -0
- package/dist/app/AnalyticsService.d.ts.map +1 -0
- package/dist/app/AnalyticsService.js +25 -0
- package/dist/app/WxpAnalyticsConsent.d.ts +54 -0
- package/dist/app/WxpAnalyticsConsent.d.ts.map +1 -0
- package/dist/app/WxpAnalyticsConsent.js +113 -0
- package/dist/app/WxpAnalyticsService.d.ts +60 -0
- package/dist/app/WxpAnalyticsService.d.ts.map +1 -0
- package/dist/app/WxpAnalyticsService.js +112 -0
- package/dist/app/index.d.ts +28 -0
- package/dist/app/index.d.ts.map +1 -0
- package/dist/app/index.js +28 -0
- package/dist/config/index.d.ts +26 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +26 -0
- package/dist/config/inversify.config.d.ts +27 -0
- package/dist/config/inversify.config.d.ts.map +1 -0
- package/dist/config/inversify.config.js +32 -0
- package/dist/config/inversify.types.d.ts +28 -0
- package/dist/config/inversify.types.d.ts.map +1 -0
- package/dist/config/inversify.types.js +28 -0
- package/dist/constants.d.ts +33 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +33 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +28 -0
- package/dist/models/CLIProgram.d.ts +44 -0
- package/dist/models/CLIProgram.d.ts.map +1 -0
- package/dist/models/CLIProgram.js +47 -0
- package/dist/models/index.d.ts +25 -0
- package/dist/models/index.d.ts.map +1 -0
- package/dist/models/index.js +25 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +45 -0
- package/src/app/AnalyticsConsent.ts +41 -0
- package/src/app/AnalyticsService.ts +50 -0
- package/src/app/WxpAnalyticsConsent.ts +119 -0
- package/src/app/WxpAnalyticsService.ts +120 -0
- package/src/app/index.ts +28 -0
- package/src/config/index.ts +26 -0
- package/src/config/inversify.config.ts +37 -0
- package/src/config/inversify.types.ts +28 -0
- package/src/constants.ts +33 -0
- package/src/index.ts +28 -0
- package/src/models/CLIProgram.ts +49 -0
- package/src/models/index.ts +25 -0
- package/src/test/app/WxpAnalyticsConsent.spec.ts +190 -0
- package/src/test/app/WxpAnalyticsService.spec.ts +162 -0
- package/src/test/models/CLIProgram.spec.ts +41 -0
- package/tsconfig.json +31 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* MIT License
|
|
3
|
+
|
|
4
|
+
* © Copyright 2023 Adobe. All rights reserved.
|
|
5
|
+
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
********************************************************************************/
|
|
24
|
+
|
|
25
|
+
export * from "./app/index.js";
|
|
26
|
+
export * from "./config/index.js";
|
|
27
|
+
export * from "./constants.js";
|
|
28
|
+
export * from "./models/index.js";
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* MIT License
|
|
3
|
+
|
|
4
|
+
* © Copyright 2023 Adobe. All rights reserved.
|
|
5
|
+
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
********************************************************************************/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A CLI program.
|
|
27
|
+
*/
|
|
28
|
+
export class CLIProgram {
|
|
29
|
+
/**
|
|
30
|
+
* Name of the program.
|
|
31
|
+
*/
|
|
32
|
+
readonly name: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Current version of the program.
|
|
36
|
+
*/
|
|
37
|
+
readonly version: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Instantiate {@link CLIProgram}.
|
|
41
|
+
* @param name - Name of the program.
|
|
42
|
+
* @param version - Current version of the program.
|
|
43
|
+
* @returns Reference to a new {@link CLIProgram} instance.
|
|
44
|
+
*/
|
|
45
|
+
constructor(name: string, version: string) {
|
|
46
|
+
this.name = name;
|
|
47
|
+
this.version = version;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* MIT License
|
|
3
|
+
|
|
4
|
+
* © Copyright 2023 Adobe. All rights reserved.
|
|
5
|
+
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
********************************************************************************/
|
|
24
|
+
|
|
25
|
+
export * from "./CLIProgram.js";
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* MIT License
|
|
3
|
+
|
|
4
|
+
* © Copyright 2023 Adobe. All rights reserved.
|
|
5
|
+
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
********************************************************************************/
|
|
24
|
+
|
|
25
|
+
import type { Logger, Preferences } from "@adobe/ccweb-add-on-core";
|
|
26
|
+
import { ADD_ON_PREFERENCES_FILE, PreferenceJson } from "@adobe/ccweb-add-on-core";
|
|
27
|
+
import { assert } from "chai";
|
|
28
|
+
import chalk from "chalk";
|
|
29
|
+
import "mocha";
|
|
30
|
+
import prompts from "prompts";
|
|
31
|
+
import type { SinonSandbox } from "sinon";
|
|
32
|
+
import sinon from "sinon";
|
|
33
|
+
import type { StubbedInstance } from "ts-sinon";
|
|
34
|
+
import { stubInterface } from "ts-sinon";
|
|
35
|
+
import type { AnalyticsConsent } from "../../app/index.js";
|
|
36
|
+
import { WxpAnalyticsConsent } from "../../app/index.js";
|
|
37
|
+
|
|
38
|
+
describe("WxpAnalyticsConsent", () => {
|
|
39
|
+
let sandbox: SinonSandbox;
|
|
40
|
+
|
|
41
|
+
let cliPreferences: StubbedInstance<Preferences>;
|
|
42
|
+
let logger: StubbedInstance<Logger>;
|
|
43
|
+
let analyticsConsent: AnalyticsConsent;
|
|
44
|
+
|
|
45
|
+
const choices: { title: string; value: boolean }[] = [];
|
|
46
|
+
choices.push({
|
|
47
|
+
title: promptMessageOption("Yes, send analytics to Adobe"),
|
|
48
|
+
value: true
|
|
49
|
+
});
|
|
50
|
+
choices.push({
|
|
51
|
+
title: promptMessageOption("No, don't send analytics to Adobe"),
|
|
52
|
+
value: false
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
sandbox = sinon.createSandbox();
|
|
57
|
+
|
|
58
|
+
cliPreferences = stubInterface<Preferences>();
|
|
59
|
+
|
|
60
|
+
logger = stubInterface<Logger>();
|
|
61
|
+
logger.warning.returns();
|
|
62
|
+
|
|
63
|
+
analyticsConsent = new WxpAnalyticsConsent(cliPreferences, logger);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
afterEach(() => {
|
|
67
|
+
sandbox.restore();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
describe("get", () => {
|
|
71
|
+
beforeEach(() => {
|
|
72
|
+
sandbox = sinon.createSandbox();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
afterEach(() => {
|
|
76
|
+
sandbox.restore();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it(`should ask the user for analytics consent if it does not exist in ${ADD_ON_PREFERENCES_FILE}.`, async () => {
|
|
80
|
+
const promptsStub = sandbox.stub(prompts, "prompt");
|
|
81
|
+
promptsStub.resolves({ analyticsConsent: choices[0].value });
|
|
82
|
+
|
|
83
|
+
const preferenceJson = new PreferenceJson({});
|
|
84
|
+
cliPreferences.get.returns(preferenceJson);
|
|
85
|
+
|
|
86
|
+
const userConsent = await analyticsConsent.get();
|
|
87
|
+
|
|
88
|
+
assert.equal(
|
|
89
|
+
logger.warning.calledWith(
|
|
90
|
+
"This tool collects and sends analytics to help Adobe improve its products.",
|
|
91
|
+
{
|
|
92
|
+
prefix: "\n"
|
|
93
|
+
}
|
|
94
|
+
),
|
|
95
|
+
true
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
assert.equal(promptsStub.callCount, 1);
|
|
99
|
+
assert.equal(
|
|
100
|
+
promptsStub.calledWith({
|
|
101
|
+
type: "select",
|
|
102
|
+
name: "analyticsConsent",
|
|
103
|
+
message: promptMessage("Do you want to allow sending analytics to Adobe"),
|
|
104
|
+
choices,
|
|
105
|
+
initial: 0
|
|
106
|
+
}),
|
|
107
|
+
true
|
|
108
|
+
);
|
|
109
|
+
assert.equal(userConsent, true);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it(`should return the user's analytics consent if it exists in ${ADD_ON_PREFERENCES_FILE}.`, async () => {
|
|
113
|
+
const promptsStub = sandbox.stub(prompts, "prompt");
|
|
114
|
+
promptsStub.resolves({ analyticsConsent: choices[0].value });
|
|
115
|
+
|
|
116
|
+
const preferenceJson = new PreferenceJson({ hasTelemetryConsent: true });
|
|
117
|
+
cliPreferences.get.returns(preferenceJson);
|
|
118
|
+
|
|
119
|
+
const userConsent = await analyticsConsent.get();
|
|
120
|
+
|
|
121
|
+
assert.equal(userConsent, true);
|
|
122
|
+
assert.equal(promptsStub.callCount, 0);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("should exit if the user does not select any option when asked for analytics consent.", async () => {
|
|
126
|
+
const promptsStub = sandbox.stub(prompts, "prompt");
|
|
127
|
+
const exitProcessStub = sandbox.stub(process, "exit");
|
|
128
|
+
promptsStub.resolves({ analyticsConsent: undefined });
|
|
129
|
+
|
|
130
|
+
const preferenceJson = new PreferenceJson({});
|
|
131
|
+
cliPreferences.get.returns(preferenceJson);
|
|
132
|
+
|
|
133
|
+
const userConsent = await analyticsConsent.get();
|
|
134
|
+
|
|
135
|
+
assert.equal(
|
|
136
|
+
logger.warning.calledWith(
|
|
137
|
+
"This tool collects and sends analytics to help Adobe improve its products.",
|
|
138
|
+
{
|
|
139
|
+
prefix: "\n"
|
|
140
|
+
}
|
|
141
|
+
),
|
|
142
|
+
true
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
assert.equal(promptsStub.callCount, 1);
|
|
146
|
+
assert.equal(
|
|
147
|
+
promptsStub.calledWith({
|
|
148
|
+
type: "select",
|
|
149
|
+
name: "analyticsConsent",
|
|
150
|
+
message: promptMessage("Do you want to allow sending analytics to Adobe"),
|
|
151
|
+
choices,
|
|
152
|
+
initial: 0
|
|
153
|
+
}),
|
|
154
|
+
true
|
|
155
|
+
);
|
|
156
|
+
assert.equal(userConsent, undefined);
|
|
157
|
+
assert.equal(exitProcessStub.calledWith(0), true);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("set", () => {
|
|
162
|
+
beforeEach(() => {
|
|
163
|
+
sandbox = sinon.createSandbox();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
afterEach(() => {
|
|
167
|
+
sandbox.restore();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it(`should set the user's analytics consent in ${ADD_ON_PREFERENCES_FILE}.`, async () => {
|
|
171
|
+
const preferenceJson = new PreferenceJson({ hasTelemetryConsent: false });
|
|
172
|
+
cliPreferences.get.returns(preferenceJson);
|
|
173
|
+
|
|
174
|
+
const analyticsConsent: AnalyticsConsent = new WxpAnalyticsConsent(cliPreferences, logger);
|
|
175
|
+
|
|
176
|
+
await analyticsConsent.set(true);
|
|
177
|
+
|
|
178
|
+
assert.equal(cliPreferences.set.callCount, 1);
|
|
179
|
+
assert.equal(cliPreferences.set.calledWith(new PreferenceJson({ hasTelemetryConsent: true })), true);
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
function promptMessage(message: string): string {
|
|
185
|
+
return chalk.hex("#E59400")(message);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function promptMessageOption(message: string): string {
|
|
189
|
+
return chalk.green.bold(message);
|
|
190
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* MIT License
|
|
3
|
+
|
|
4
|
+
* © Copyright 2023 Adobe. All rights reserved.
|
|
5
|
+
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
********************************************************************************/
|
|
24
|
+
|
|
25
|
+
import type { Preferences } from "@adobe/ccweb-add-on-core";
|
|
26
|
+
import { PreferenceJson } from "@adobe/ccweb-add-on-core";
|
|
27
|
+
import axios from "axios";
|
|
28
|
+
import { assert } from "chai";
|
|
29
|
+
import "mocha";
|
|
30
|
+
import osName from "os-name";
|
|
31
|
+
import type { SinonSandbox } from "sinon";
|
|
32
|
+
import sinon from "sinon";
|
|
33
|
+
import type { StubbedInstance } from "ts-sinon";
|
|
34
|
+
import { stubInterface } from "ts-sinon";
|
|
35
|
+
import type { AnalyticsService } from "../../app/AnalyticsService.js";
|
|
36
|
+
import { WxpAnalyticsService } from "../../app/WxpAnalyticsService.js";
|
|
37
|
+
import { ANALYTICS_API } from "../../constants.js";
|
|
38
|
+
import { CLIProgram } from "../../models/CLIProgram.js";
|
|
39
|
+
|
|
40
|
+
describe("WxpAnalyticsService", () => {
|
|
41
|
+
let sandbox: SinonSandbox;
|
|
42
|
+
|
|
43
|
+
let cliPreferences: StubbedInstance<Preferences>;
|
|
44
|
+
let analyticsService: AnalyticsService;
|
|
45
|
+
|
|
46
|
+
const program = new CLIProgram("test-program", "1.0.0");
|
|
47
|
+
|
|
48
|
+
beforeEach(() => {
|
|
49
|
+
sandbox = sinon.createSandbox();
|
|
50
|
+
|
|
51
|
+
cliPreferences = stubInterface<Preferences>();
|
|
52
|
+
analyticsService = new WxpAnalyticsService(cliPreferences);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
sandbox.restore();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("postEvent", () => {
|
|
60
|
+
it("should post event when user has provided consent to collect and send analytics to Adobe.", async () => {
|
|
61
|
+
const currentTime = Date.now();
|
|
62
|
+
sandbox.stub(Date, "now").returns(currentTime);
|
|
63
|
+
|
|
64
|
+
const clientId = 10001;
|
|
65
|
+
const preferenceJSON = new PreferenceJson({ clientId, hasTelemetryConsent: true });
|
|
66
|
+
|
|
67
|
+
cliPreferences.get.returns(preferenceJSON);
|
|
68
|
+
|
|
69
|
+
sandbox.stub(Math, "random").returns(0.3);
|
|
70
|
+
|
|
71
|
+
const axiosPostStub = sandbox.stub(axios, "post");
|
|
72
|
+
axiosPostStub.resolves();
|
|
73
|
+
|
|
74
|
+
const eventType = "SUCCESS";
|
|
75
|
+
const eventData = "foobar";
|
|
76
|
+
|
|
77
|
+
const expectedApiRequestBody = JSON.stringify({
|
|
78
|
+
id: Math.floor(currentTime * Math.random()),
|
|
79
|
+
timestamp: currentTime,
|
|
80
|
+
_adobeio: {
|
|
81
|
+
eventType,
|
|
82
|
+
eventData,
|
|
83
|
+
cliVersion: program.version,
|
|
84
|
+
clientId,
|
|
85
|
+
command: program.name,
|
|
86
|
+
commandDuration: 0,
|
|
87
|
+
commandSuccess: true,
|
|
88
|
+
nodeVersion: process.version,
|
|
89
|
+
osNameVersion: osName()
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
analyticsService.program = program;
|
|
94
|
+
analyticsService.startTime = Date.now();
|
|
95
|
+
|
|
96
|
+
await analyticsService.postEvent(eventType, eventData, true);
|
|
97
|
+
|
|
98
|
+
assert.equal(cliPreferences.set.callCount, 0);
|
|
99
|
+
assert.equal(
|
|
100
|
+
axiosPostStub.calledOnceWith(ANALYTICS_API.URL, expectedApiRequestBody, {
|
|
101
|
+
headers: ANALYTICS_API.HEADERS
|
|
102
|
+
}),
|
|
103
|
+
true
|
|
104
|
+
);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
const runs = [{ hasTelemetryConsent: undefined }, { hasTelemetryConsent: false }];
|
|
108
|
+
runs.forEach(({ hasTelemetryConsent }) => {
|
|
109
|
+
it("should not post event when user has not provided consent to collect and send analytics to Adobe.", async () => {
|
|
110
|
+
const currentTime = Date.now();
|
|
111
|
+
sandbox.stub(Date, "now").returns(currentTime);
|
|
112
|
+
|
|
113
|
+
const clientId = 10001;
|
|
114
|
+
const preferenceJSON = new PreferenceJson({ clientId, hasTelemetryConsent });
|
|
115
|
+
|
|
116
|
+
cliPreferences.get.returns(preferenceJSON);
|
|
117
|
+
|
|
118
|
+
sandbox.stub(Math, "random").returns(0.3);
|
|
119
|
+
|
|
120
|
+
const axiosPostStub = sandbox.stub(axios, "post");
|
|
121
|
+
|
|
122
|
+
const eventType = "SUCCESS";
|
|
123
|
+
const eventData = "foobar";
|
|
124
|
+
|
|
125
|
+
analyticsService.program = program;
|
|
126
|
+
analyticsService.startTime = currentTime;
|
|
127
|
+
|
|
128
|
+
await analyticsService.postEvent(eventType, eventData, true);
|
|
129
|
+
|
|
130
|
+
assert.equal(axiosPostStub.callCount, 0);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("should set a new clientId for the user if it does not exist and silently return for any errors.", async () => {
|
|
135
|
+
const currentTime = Date.now();
|
|
136
|
+
sandbox.stub(Date, "now").returns(currentTime);
|
|
137
|
+
|
|
138
|
+
const preferenceJSON = new PreferenceJson({ hasTelemetryConsent: true });
|
|
139
|
+
|
|
140
|
+
cliPreferences.get.returns(preferenceJSON);
|
|
141
|
+
cliPreferences.set.returns();
|
|
142
|
+
|
|
143
|
+
sandbox.stub(Math, "random").returns(0.3);
|
|
144
|
+
|
|
145
|
+
const axiosPostStub = sandbox.stub(axios, "post");
|
|
146
|
+
axiosPostStub.rejects();
|
|
147
|
+
|
|
148
|
+
const eventType = "SUCCESS";
|
|
149
|
+
const eventData = "foobar";
|
|
150
|
+
|
|
151
|
+
analyticsService.program = program;
|
|
152
|
+
analyticsService.startTime = currentTime;
|
|
153
|
+
|
|
154
|
+
await analyticsService.postEvent(eventType, eventData, true);
|
|
155
|
+
|
|
156
|
+
preferenceJSON.clientId = Math.floor(Date.now() * Math.random());
|
|
157
|
+
|
|
158
|
+
assert.equal(cliPreferences.set.calledOnceWith(preferenceJSON), true);
|
|
159
|
+
assert.equal(axiosPostStub.callCount, 1);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/********************************************************************************
|
|
2
|
+
* MIT License
|
|
3
|
+
|
|
4
|
+
* © Copyright 2023 Adobe. All rights reserved.
|
|
5
|
+
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
********************************************************************************/
|
|
24
|
+
|
|
25
|
+
import { assert } from "chai";
|
|
26
|
+
import "mocha";
|
|
27
|
+
import { CLIProgram } from "../../models/index.js";
|
|
28
|
+
|
|
29
|
+
describe("CLIProgram", () => {
|
|
30
|
+
describe("constructor", () => {
|
|
31
|
+
it("should create a new instance of CLIOptions.", () => {
|
|
32
|
+
const name = "test-program";
|
|
33
|
+
const version = "1.0.0";
|
|
34
|
+
|
|
35
|
+
const cliProgram = new CLIProgram(name, version);
|
|
36
|
+
|
|
37
|
+
assert.equal(cliProgram.name, name);
|
|
38
|
+
assert.equal(cliProgram.version, version);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowSyntheticDefaultImports": true,
|
|
4
|
+
"declaration": true,
|
|
5
|
+
"declarationMap": true,
|
|
6
|
+
"emitDecoratorMetadata": true,
|
|
7
|
+
"esModuleInterop": true,
|
|
8
|
+
"experimentalDecorators": true,
|
|
9
|
+
"forceConsistentCasingInFileNames": true,
|
|
10
|
+
"importHelpers": true,
|
|
11
|
+
"incremental": true,
|
|
12
|
+
"isolatedModules": true,
|
|
13
|
+
"module": "ESNext",
|
|
14
|
+
"moduleResolution": "node",
|
|
15
|
+
"noImplicitOverride": false,
|
|
16
|
+
"noUnusedLocals": true,
|
|
17
|
+
"outDir": "dist",
|
|
18
|
+
"preserveConstEnums": true,
|
|
19
|
+
"resolveJsonModule": true,
|
|
20
|
+
"sourceMap": true,
|
|
21
|
+
"strict": true,
|
|
22
|
+
"target": "ESNext",
|
|
23
|
+
"types": ["reflect-metadata"],
|
|
24
|
+
"useUnknownInCatchVariables": false
|
|
25
|
+
},
|
|
26
|
+
"exclude": ["node_modules", "src/test/**/*"],
|
|
27
|
+
"include": ["src/**/*"],
|
|
28
|
+
"ts-node": {
|
|
29
|
+
"files": true
|
|
30
|
+
}
|
|
31
|
+
}
|