@opentermsarchive/engine 1.1.1 → 1.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opentermsarchive/engine",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Tracks and makes visible changes to the terms of online services",
5
5
  "homepage": "https://opentermsarchive.org",
6
6
  "bugs": {
@@ -65,7 +65,6 @@
65
65
  "croner": "^4.3.6",
66
66
  "cross-env": "^7.0.3",
67
67
  "datauri": "^4.1.0",
68
- "deep-diff": "^1.0.2",
69
68
  "dotenv": "^10.0.0",
70
69
  "eslint": "^8.53.0",
71
70
  "eslint-config-airbnb-base": "^15.0.0",
@@ -31,7 +31,7 @@ export default async options => {
31
31
  if (options.modified) {
32
32
  const declarationUtils = new DeclarationUtils(instancePath);
33
33
 
34
- ({ services: servicesToValidate } = await declarationUtils.getModifiedServiceTermsTypes());
34
+ ({ services: servicesToValidate } = await declarationUtils.getModifiedServicesAndTermsTypes());
35
35
  }
36
36
 
37
37
  const lintFile = lintAndFixFile(options.fix);
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "Service",
3
+ "documents": {
4
+ "Terms of Service": {
5
+ "fetch": "https://domain.example/tos",
6
+ "select": "body"
7
+ },
8
+ "Privacy Policy": {
9
+ "fetch": "https://domain.example/privacy",
10
+ "select": "body"
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "Service",
3
+ "documents": {
4
+ "Terms of Service": {
5
+ "fetch": "https://domain.example/tos",
6
+ "select": "body",
7
+ "remove": "footer"
8
+ },
9
+ "Privacy Policy": {
10
+ "fetch": "https://domain.example/privacy",
11
+ "select": "body",
12
+ "remove": "footer"
13
+ },
14
+ "Imprint": {
15
+ "fetch": "https://domain.example/imprint",
16
+ "select": "body",
17
+ "remove": "footer"
18
+ }
19
+ }
20
+ }
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "Service",
3
+ "documents": {
4
+ "Terms of Service": {
5
+ "fetch": "https://domain.example/tos",
6
+ "select": "body"
7
+ },
8
+ "Privacy Policy": {
9
+ "fetch": "https://domain.example/privacy",
10
+ "select": "body"
11
+ },
12
+ "Imprint": {
13
+ "fetch": "https://domain.example/imprint",
14
+ "select": "body"
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "Service",
3
+ "documents": {
4
+ "Terms of Service": {
5
+ "fetch": "https://domain.example/tos",
6
+ "select": "body"
7
+ }
8
+ }
9
+ }
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "Service",
3
+ "documents": {
4
+ "Terms of Service": {
5
+ "fetch": "https://domain.example/tos",
6
+ "select": "body",
7
+ "remove": "footer"
8
+ },
9
+ "Privacy Policy": {
10
+ "fetch": "https://domain.example/privacy",
11
+ "select": "body"
12
+ }
13
+ }
14
+ }
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "Service B",
3
+ "documents": {
4
+ "Terms of Service": {
5
+ "fetch": "https://domain.example/tos",
6
+ "select": "body"
7
+ }
8
+ }
9
+ }
@@ -1,6 +1,5 @@
1
1
  import path from 'path';
2
2
 
3
- import DeepDiff from 'deep-diff';
4
3
  import simpleGit from 'simple-git';
5
4
 
6
5
  export default class DeclarationUtils {
@@ -9,13 +8,17 @@ export default class DeclarationUtils {
9
8
  this.defaultBranch = defaultBranch;
10
9
  }
11
10
 
12
- static filePathToServiceId = filePath => path.parse(filePath.replace(/\.history|\.filters/, '')).name;
11
+ static getServiceIdFromFilePath(filePath) {
12
+ return path.parse(filePath.replace(/\.history|\.filters/, '')).name;
13
+ }
13
14
 
14
- async getJSONFile(path, ref) {
15
+ async getJSONFromFile(ref, filePath) {
15
16
  try {
16
- return JSON.parse(await this.git.show([`${ref}:${path}`]));
17
- } catch (e) {
18
- return {};
17
+ const fileContent = await this.git.show([`${ref}:${filePath}`]);
18
+
19
+ return JSON.parse(fileContent);
20
+ } catch (error) {
21
+ // the file does not exist on the requested branch or it is not parsable
19
22
  }
20
23
  }
21
24
 
@@ -24,7 +27,7 @@ export default class DeclarationUtils {
24
27
 
25
28
  const modifiedFilePaths = (modifiedFilePathsAsString ? modifiedFilePathsAsString.split('\0') : []).filter(str => str !== ''); // split on \0 rather than \n due to the -z option of git diff
26
29
 
27
- return { modifiedFilePaths, modifiedServicesIds: Array.from(new Set(modifiedFilePaths.map(DeclarationUtils.filePathToServiceId))) };
30
+ return { modifiedFilePaths, modifiedServicesIds: Array.from(new Set(modifiedFilePaths.map(DeclarationUtils.getServiceIdFromFilePath))) };
28
31
  }
29
32
 
30
33
  async getModifiedServices() {
@@ -33,48 +36,47 @@ export default class DeclarationUtils {
33
36
  return modifiedServicesIds;
34
37
  }
35
38
 
36
- async getModifiedServiceTermsTypes() {
37
- const { modifiedFilePaths, modifiedServicesIds } = await this.getModifiedData();
39
+ async getModifiedServicesAndTermsTypes() {
40
+ const { modifiedFilePaths } = await this.getModifiedData();
38
41
  const servicesTermsTypes = {};
39
42
 
40
43
  await Promise.all(modifiedFilePaths.map(async modifiedFilePath => {
41
- const serviceId = DeclarationUtils.filePathToServiceId(modifiedFilePath);
44
+ const serviceId = DeclarationUtils.getServiceIdFromFilePath(modifiedFilePath);
42
45
 
43
- if (!modifiedFilePath.endsWith('.json')) {
44
- // Here we should compare AST of both files to detect on which function
45
- // change has been made, and then find which terms type depends on this
46
- // function.
47
- // As this is a complicated process, we will just send back all terms types
48
- const declaration = await this.getJSONFile(`declarations/${serviceId}.json`, this.defaultBranch);
46
+ if (modifiedFilePath.endsWith('.filters.js')) {
47
+ const declaration = await this.getJSONFromFile(this.defaultBranch, `declarations/${serviceId}.json`);
49
48
 
50
- return Object.keys(declaration.documents);
49
+ servicesTermsTypes[serviceId] = Object.keys(declaration.documents); // Considering how rarely filters are used, simply return all term types that could potentially be impacted to spare implementing a function change check
50
+
51
+ return;
51
52
  }
52
53
 
53
- const defaultFile = await this.getJSONFile(modifiedFilePath, this.defaultBranch);
54
- const modifiedFile = await this.getJSONFile(modifiedFilePath, 'HEAD');
54
+ const originalService = await this.getJSONFromFile(this.defaultBranch, modifiedFilePath);
55
+ const modifiedService = await this.getJSONFromFile('HEAD', modifiedFilePath);
56
+ const modifiedServiceTermsTypes = Object.keys(modifiedService.documents);
55
57
 
56
- const diff = DeepDiff.diff(defaultFile, modifiedFile);
58
+ if (!originalService) {
59
+ servicesTermsTypes[serviceId] = modifiedServiceTermsTypes;
57
60
 
58
- if (!diff) {
59
- // This can happen if only a lint has been applied to a document
60
61
  return;
61
62
  }
62
63
 
63
- const modifiedTermsTypes = diff.reduce((acc, { path }) => {
64
- if (modifiedFilePath.includes('.history')) {
65
- acc.add(path[0]);
66
- } else if (path[0] == 'documents') {
67
- acc.add(path[1]);
68
- }
64
+ const originalServiceTermsTypes = Object.keys(originalService.documents);
65
+
66
+ modifiedServiceTermsTypes.forEach(termsType => {
67
+ const areTermsAdded = !originalServiceTermsTypes.includes(termsType);
68
+ const areTermsModified = JSON.stringify(originalService.documents[termsType]) != JSON.stringify(modifiedService.documents[termsType]);
69
69
 
70
- return acc;
71
- }, new Set());
70
+ if (!areTermsAdded && !areTermsModified) {
71
+ return;
72
+ }
72
73
 
73
- servicesTermsTypes[serviceId] = Array.from(new Set([ ...servicesTermsTypes[serviceId] || [], ...modifiedTermsTypes ]));
74
+ servicesTermsTypes[serviceId] = [termsType].concat(servicesTermsTypes[serviceId] || []);
75
+ });
74
76
  }));
75
77
 
76
78
  return {
77
- services: modifiedServicesIds,
79
+ services: Object.keys(servicesTermsTypes),
78
80
  servicesTermsTypes,
79
81
  };
80
82
  }
@@ -0,0 +1,154 @@
1
+ import fs from 'fs/promises';
2
+ import path from 'path';
3
+ import { fileURLToPath } from 'url';
4
+
5
+ import { expect } from 'chai';
6
+
7
+ import DeclarationUtils from './index.js';
8
+
9
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
10
+
11
+ let declarationUtils;
12
+
13
+ const SUBJECT_PATH = path.resolve(__dirname, './test');
14
+
15
+ const FIXTURES = {
16
+ serviceA: { path: './fixtures/serviceA.json' },
17
+ serviceATermsUpdated: { path: './fixtures/serviceATermsUpdated.json' },
18
+ serviceAMultipleTermsUpdated: { path: './fixtures/serviceAMultipleTermsUpdated.json' },
19
+ serviceATermsAdded: { path: './fixtures/serviceATermsAdded.json' },
20
+ serviceATermsRemoved: { path: './fixtures/serviceATermsRemoved.json' },
21
+ serviceB: { path: './fixtures/serviceB.json' },
22
+ };
23
+
24
+ const COMMIT_PATHS = {
25
+ serviceA: './declarations/ServiceA.json',
26
+ serviceB: './declarations/ServiceB.json',
27
+ };
28
+
29
+ const commitChanges = async (filePath, content) => {
30
+ await fs.writeFile(path.resolve(SUBJECT_PATH, filePath), JSON.stringify(content, null, 2));
31
+ await declarationUtils.git.add(filePath);
32
+ await declarationUtils.git.commit('Update declarations', filePath);
33
+ };
34
+
35
+ const removeLatestCommit = async () => {
36
+ await declarationUtils.git.reset('hard', ['HEAD~1']);
37
+ };
38
+
39
+ describe.only('DeclarationUtils', () => {
40
+ describe('#getModifiedServicesAndTermsTypes', () => {
41
+ before(async () => {
42
+ await loadFixtures();
43
+ await setupRepository();
44
+ });
45
+
46
+ after(() => fs.rm(SUBJECT_PATH, { recursive: true }));
47
+
48
+ context('when an existing declaration has been modified', () => {
49
+ before(() => commitChanges(COMMIT_PATHS.serviceA, FIXTURES.serviceATermsUpdated.content));
50
+ after(removeLatestCommit);
51
+
52
+ it('returns the service ID and the updated terms type', async () => {
53
+ expect(await declarationUtils.getModifiedServicesAndTermsTypes()).to.deep.equal({
54
+ services: ['ServiceA'],
55
+ servicesTermsTypes: { ServiceA: ['Terms of Service'] },
56
+ });
57
+ });
58
+ });
59
+
60
+ context('when new terms are declared in an existing declaration', () => {
61
+ before(() => commitChanges(COMMIT_PATHS.serviceA, FIXTURES.serviceATermsAdded.content));
62
+ after(removeLatestCommit);
63
+
64
+ it('returns the service ID and the added terms type', async () => {
65
+ expect(await declarationUtils.getModifiedServicesAndTermsTypes()).to.deep.equal({
66
+ services: ['ServiceA'],
67
+ servicesTermsTypes: { ServiceA: ['Imprint'] },
68
+ });
69
+ });
70
+ });
71
+
72
+ context('when a new declaration has been added', () => {
73
+ before(() => commitChanges(COMMIT_PATHS.serviceB, FIXTURES.serviceB.content));
74
+ after(removeLatestCommit);
75
+
76
+ it('returns the added service ID along with the associated terms type', async () => {
77
+ expect(await declarationUtils.getModifiedServicesAndTermsTypes()).to.deep.equal({
78
+ services: ['ServiceB'],
79
+ servicesTermsTypes: { ServiceB: ['Terms of Service'] },
80
+ });
81
+ });
82
+ });
83
+
84
+ context('when a declaration has been removed', () => {
85
+ before(() => removeLatestCommit(declarationUtils.git));
86
+ after(async () => {
87
+ await fs.mkdir(path.resolve(SUBJECT_PATH, './declarations'), { recursive: true });
88
+ await commitChanges(COMMIT_PATHS.serviceA, FIXTURES.serviceA.content);
89
+ });
90
+
91
+ it('returns no services and no terms types', async () => {
92
+ expect(await declarationUtils.getModifiedServicesAndTermsTypes()).to.deep.equal({
93
+ services: [],
94
+ servicesTermsTypes: {},
95
+ });
96
+ });
97
+ });
98
+
99
+ context('when terms are removed from an existing declaration', () => {
100
+ before(() => commitChanges(COMMIT_PATHS.serviceA, FIXTURES.serviceATermsRemoved.content));
101
+ after(removeLatestCommit);
102
+
103
+ it('returns no services and no terms types', async () => {
104
+ expect(await declarationUtils.getModifiedServicesAndTermsTypes()).to.deep.equal({
105
+ services: [],
106
+ servicesTermsTypes: {},
107
+ });
108
+ });
109
+ });
110
+
111
+ context('when there is a combination of an updated declaration and an added declaration', () => {
112
+ before(async () => {
113
+ await commitChanges(COMMIT_PATHS.serviceA, FIXTURES.serviceAMultipleTermsUpdated.content);
114
+ await commitChanges(COMMIT_PATHS.serviceB, FIXTURES.serviceB.content);
115
+ });
116
+
117
+ after(async () => {
118
+ await removeLatestCommit();
119
+ await removeLatestCommit();
120
+ });
121
+
122
+ it('returns the services IDs and the updated terms types', async () => {
123
+ expect(await declarationUtils.getModifiedServicesAndTermsTypes()).to.deep.equal({
124
+ services: [ 'ServiceA', 'ServiceB' ],
125
+ servicesTermsTypes: {
126
+ ServiceA: [ 'Imprint', 'Privacy Policy', 'Terms of Service' ],
127
+ ServiceB: ['Terms of Service'],
128
+ },
129
+ });
130
+ });
131
+ });
132
+ });
133
+ });
134
+
135
+ async function loadFixtures() {
136
+ await Promise.all(Object.values(FIXTURES).map(async fixture => {
137
+ const content = await fs.readFile(path.resolve(__dirname, fixture.path), 'utf-8');
138
+
139
+ fixture.content = JSON.parse(content);
140
+ }));
141
+ }
142
+
143
+ async function setupRepository() {
144
+ await fs.mkdir(path.resolve(SUBJECT_PATH, './declarations'), { recursive: true });
145
+
146
+ declarationUtils = new DeclarationUtils(SUBJECT_PATH, 'main');
147
+ await declarationUtils.git.init();
148
+ await declarationUtils.git.addConfig('user.name', 'Open Terms Archive Testing Bot');
149
+ await declarationUtils.git.addConfig('user.email', 'testing-bot@opentermsarchive.org');
150
+ await declarationUtils.git.checkoutLocalBranch('main');
151
+ await commitChanges('./README.md', 'This directory is auto-generated by test executions and requires cleanup after each test run');
152
+ await commitChanges(COMMIT_PATHS.serviceA, FIXTURES.serviceA.content);
153
+ await declarationUtils.git.checkoutLocalBranch('updated');
154
+ }
@@ -37,7 +37,7 @@ export default async options => {
37
37
  if (options.modified) {
38
38
  const declarationUtils = new DeclarationUtils(instancePath);
39
39
 
40
- ({ services: servicesToValidate, servicesTermsTypes } = await declarationUtils.getModifiedServiceTermsTypes());
40
+ ({ services: servicesToValidate, servicesTermsTypes } = await declarationUtils.getModifiedServicesAndTermsTypes());
41
41
  }
42
42
 
43
43
  describe('Service declarations validation', async function () {