@anonympins/fingerprint 0.4.2 → 0.4.4

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.
@@ -19,6 +19,30 @@ const FunctionRegistry = {};
19
19
  // avec la structure attendue par le ProblemManager.
20
20
  FunctionRegistry['cpc.solve'] = Optimization.Operators.solveOptimalCPC; // NOUVEAU: Enregistrement du solveur CPC
21
21
 
22
+ /**
23
+ * Évalue le coût total pour le problème de placement d'infrastructures.
24
+ * @param {Array<{x: number, y: number}>} facilities - Les installations.
25
+ * @param {object} payload - Le payload contenant les clients et les options.
26
+ * @returns {number} Le coût total.
27
+ */
28
+ FunctionRegistry['facility.calculateEnergy'] = (facilities, payload) => {
29
+ const customers = payload.customers || [];
30
+ const fixedCostPerFacility = payload.options?.fixedCostPerFacility || 0;
31
+ const distanceSq = (p1, p2) => Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
32
+ let totalConnectionCost = 0;
33
+ for (const customer of customers) {
34
+ let minDistanceToCustomer = Infinity;
35
+ for (const facility of facilities) {
36
+ const d = distanceSq(customer, facility);
37
+ if (d < minDistanceToCustomer) {
38
+ minDistanceToCustomer = d;
39
+ }
40
+ }
41
+ totalConnectionCost += Math.sqrt(minDistanceToCustomer);
42
+ }
43
+ return totalConnectionCost + facilities.length * fixedCostPerFacility;
44
+ };
45
+
22
46
  /**
23
47
  * Évalue la distance totale d'un chemin pour le problème du voyageur de commerce (TSP).
24
48
  * @param {Array<{x: number, y: number}>} path - Un tableau de points représentant le chemin.
@@ -1,120 +1,141 @@
1
- import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
2
- import {JSDOM} from 'jsdom';
3
- import ClientLibrary from '../fingerprint.client.js';
4
-
5
- // --- Setup JSDOM Environment ---
6
- // Vitest peut être configuré pour le faire automatiquement, mais le faire manuellement
7
- // ici rend le test explicite et portable.
8
- const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', {
9
- url: 'http://localhost',
10
- });
11
-
12
- // In recent Node.js versions, some global properties like 'navigator', 'performance',
13
- // and 'screen' are read-only. To ensure our JSDOM environment works correctly across
14
- // all versions, we use Object.defineProperty to make these properties writable.
15
- Object.defineProperty(global, 'window', { value: dom.window, writable: true });
16
- Object.defineProperty(global, 'document', { value: dom.window.document, writable: true });
17
- Object.defineProperty(global, 'navigator', { value: dom.window.navigator, writable: true });
18
- Object.defineProperty(global, 'screen', { value: dom.window.screen, writable: true });
19
- Object.defineProperty(global, 'performance', { value: dom.window.performance, writable: true });
20
-
21
- global.fetch = vi.fn(); // Mock global fetch
22
- global.Headers = dom.window.Headers;
23
- global.Request = dom.window.Request;
24
- global.URL = dom.window.URL;
25
- global.TextEncoder = dom.window.TextEncoder;
26
-
27
-
28
- describe('ClientLibrary.initializeClient', () => {
29
-
30
- // On utilise des espions (spies) pour vérifier si les méthodes internes sont appelées.
31
- let startMouseSpy, startKeystrokeSpy, initHoneypotsSpy, injectTrapsSpy, initFetchSpy;
32
-
33
- beforeEach(() => {
34
- // Réinitialiser l'état du client avant chaque test
35
- ClientLibrary._resetCache();
36
-
37
- // Créer les espions sur les méthodes internes
38
- startMouseSpy = vi.spyOn(ClientLibrary, 'startMouseEntropyTracker');
39
- startKeystrokeSpy = vi.spyOn(ClientLibrary, 'startKeystrokeDynamicsTracker');
40
- initHoneypotsSpy = vi.spyOn(ClientLibrary, 'initializeHoneypots');
41
- injectTrapsSpy = vi.spyOn(ClientLibrary, 'injectTrapLinks');
42
- initFetchSpy = vi.spyOn(ClientLibrary, 'initializeFetch');
43
- });
44
-
45
- afterEach(() => {
46
- // Restaurer les espions après chaque test pour ne pas affecter les autres tests
47
- vi.restoreAllMocks();
48
- });
49
-
50
- it('should enable all trackers by default', () => {
51
- ClientLibrary.initializeClient();
52
-
53
- expect(startMouseSpy).toHaveBeenCalled();
54
- expect(startKeystrokeSpy).toHaveBeenCalled();
55
- });
56
-
57
- it('should disable mouse and keystroke trackers when configured', () => {
58
- ClientLibrary.initializeClient({ mouse: false, keystrokes: false });
59
-
60
- expect(startMouseSpy).not.toHaveBeenCalled();
61
- expect(startKeystrokeSpy).not.toHaveBeenCalled();
62
- });
63
-
64
- it('should initialize honeypots with the provided field names', () => {
65
- const honeypotFields = ['email_confirm', 'user_nickname'];
66
- ClientLibrary.initializeClient({ honeypots: honeypotFields });
67
-
68
- expect(initHoneypotsSpy).toHaveBeenCalledWith(honeypotFields);
69
- });
70
-
71
- it('should not initialize honeypots if the array is empty', () => {
72
- ClientLibrary.initializeClient({ honeypots: [] });
73
-
74
- expect(initHoneypotsSpy).not.toHaveBeenCalled();
75
- });
76
-
77
- it('should inject trap URLs when provided', () => {
78
- const urls = ['/trap1?sig=123', '/trap2?sig=456'];
79
- ClientLibrary.initializeClient({ trapUrls: urls });
80
-
81
- expect(injectTrapsSpy).toHaveBeenCalledWith(urls);
82
- });
83
-
84
- it('should not inject trap URLs if the array is empty', () => {
85
- ClientLibrary.initializeClient({ trapUrls: [] });
86
-
87
- expect(injectTrapsSpy).not.toHaveBeenCalled();
88
- });
89
-
90
- it('should initialize fetch interception when fetch config is present', () => {
91
- const targetDomains = ['api.example.com'];
92
- ClientLibrary.initializeClient({ fetch: { targetDomains } });
93
-
94
- expect(initFetchSpy).toHaveBeenCalledWith(targetDomains);
95
- });
96
-
97
- it('should not initialize fetch interception if fetch config is absent', () => {
98
- ClientLibrary.initializeClient(); // No fetch config
99
-
100
- expect(initFetchSpy).not.toHaveBeenCalled();
101
- });
102
-
103
- it('should call all initializers correctly when a full config is provided', () => {
104
- const config = {
105
- mouse: true,
106
- keystrokes: true,
107
- honeypots: ['field1'],
108
- trapUrls: ['/trap1'],
109
- fetch: { targetDomains: ['api.com'] }
110
- };
111
- ClientLibrary.initializeClient(config);
112
-
113
- expect(startMouseSpy).toHaveBeenCalled();
114
- expect(startKeystrokeSpy).toHaveBeenCalled();
115
- expect(initHoneypotsSpy).toHaveBeenCalledWith(config.honeypots);
116
- expect(injectTrapsSpy).toHaveBeenCalledWith(config.trapUrls);
117
- expect(initFetchSpy).toHaveBeenCalledWith(config.fetch.targetDomains);
118
- });
119
-
1
+ import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
2
+ import {JSDOM} from 'jsdom';
3
+ import ClientLibrary from '../fingerprint.client.js';
4
+
5
+ // --- Setup JSDOM Environment ---
6
+ // Vitest peut être configuré pour le faire automatiquement, mais le faire manuellement
7
+ // ici rend le test explicite et portable.
8
+ const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>', {
9
+ url: 'http://localhost',
10
+ });
11
+
12
+ // In recent Node.js versions, some global properties like 'navigator', 'performance',
13
+ // and 'screen' are read-only. To ensure our JSDOM environment works correctly across
14
+ // all versions, we use Object.defineProperty to make these properties writable.
15
+ Object.defineProperty(global, 'window', { value: dom.window, writable: true });
16
+ Object.defineProperty(global, 'document', { value: dom.window.document, writable: true });
17
+ Object.defineProperty(global, 'navigator', { value: dom.window.navigator, writable: true });
18
+ Object.defineProperty(global, 'screen', { value: dom.window.screen, writable: true });
19
+ Object.defineProperty(global, 'performance', { value: dom.window.performance, writable: true });
20
+
21
+ global.fetch = vi.fn(); // Mock global fetch
22
+ global.Headers = dom.window.Headers;
23
+ global.Request = dom.window.Request;
24
+ global.URL = dom.window.URL;
25
+ global.TextEncoder = dom.window.TextEncoder;
26
+
27
+
28
+ describe('ClientLibrary.initializeClient', () => {
29
+
30
+ // On utilise des espions (spies) pour vérifier si les méthodes internes sont appelées.
31
+ let startMouseSpy, startKeystrokeSpy, startClickSpy, startTouchSpy, initWasmSpy, initHoneypotsSpy, injectTrapsSpy, initFetchSpy, injectPhantomTrapsSpy;
32
+
33
+ beforeEach(() => {
34
+ // Réinitialiser l'état du client avant chaque test
35
+ ClientLibrary._resetCache();
36
+
37
+ // Créer les espions sur les méthodes internes
38
+ startMouseSpy = vi.spyOn(ClientLibrary, 'startMouseEntropyTracker');
39
+ startKeystrokeSpy = vi.spyOn(ClientLibrary, 'startKeystrokeDynamicsTracker');
40
+ startClickSpy = vi.spyOn(ClientLibrary, 'startClickTracker');
41
+ startTouchSpy = vi.spyOn(ClientLibrary, 'startTouchEventTracker');
42
+ initWasmSpy = vi.spyOn(ClientLibrary, 'initializeWasm').mockResolvedValue(undefined);
43
+ initHoneypotsSpy = vi.spyOn(ClientLibrary, 'initializeHoneypots');
44
+ injectTrapsSpy = vi.spyOn(ClientLibrary, 'injectTrapLinks');
45
+ initFetchSpy = vi.spyOn(ClientLibrary, 'initializeFetch');
46
+ injectPhantomTrapsSpy = vi.spyOn(ClientLibrary, 'injectPhantomTraps');
47
+ });
48
+
49
+ afterEach(() => {
50
+ // Restaurer les espions après chaque test pour ne pas affecter les autres tests
51
+ vi.restoreAllMocks();
52
+ });
53
+
54
+ it('should enable all trackers by default', () => {
55
+ ClientLibrary.initializeClient();
56
+
57
+ expect(startMouseSpy).toHaveBeenCalled();
58
+ expect(startKeystrokeSpy).toHaveBeenCalled();
59
+ expect(startClickSpy).toHaveBeenCalled();
60
+ expect(startTouchSpy).toHaveBeenCalled();
61
+ expect(injectPhantomTrapsSpy).toHaveBeenCalled();
62
+ });
63
+
64
+ it('should disable trackers when configured', () => {
65
+ ClientLibrary.initializeClient({ mouse: false, keystrokes: false, clicks: false, touches: false });
66
+
67
+ expect(startMouseSpy).not.toHaveBeenCalled();
68
+ expect(startKeystrokeSpy).not.toHaveBeenCalled();
69
+ expect(startClickSpy).not.toHaveBeenCalled();
70
+ expect(startTouchSpy).not.toHaveBeenCalled();
71
+ });
72
+
73
+ it('should initialize WASM if wasmPath is configured', () => {
74
+ ClientLibrary.initializeClient({ wasmPath: '/fp.js' });
75
+
76
+ expect(initWasmSpy).toHaveBeenCalledWith('/fp.js');
77
+ });
78
+
79
+ it('should disable phantom traps when configured', () => {
80
+ ClientLibrary.initializeClient({ phantomTraps: false });
81
+
82
+ expect(injectPhantomTrapsSpy).not.toHaveBeenCalled();
83
+ });
84
+
85
+ it('should initialize honeypots with the provided field names', () => {
86
+ const honeypotFields = ['email_confirm', 'user_nickname'];
87
+ ClientLibrary.initializeClient({ honeypots: honeypotFields });
88
+
89
+ expect(initHoneypotsSpy).toHaveBeenCalledWith(honeypotFields);
90
+ });
91
+
92
+ it('should not initialize honeypots if the array is empty', () => {
93
+ ClientLibrary.initializeClient({ honeypots: [] });
94
+
95
+ expect(initHoneypotsSpy).not.toHaveBeenCalled();
96
+ });
97
+
98
+ it('should inject trap URLs when provided', () => {
99
+ const urls = ['/trap1?sig=123', '/trap2?sig=456'];
100
+ ClientLibrary.initializeClient({ trapUrls: urls });
101
+
102
+ expect(injectTrapsSpy).toHaveBeenCalledWith(urls);
103
+ });
104
+
105
+ it('should not inject trap URLs if the array is empty', () => {
106
+ ClientLibrary.initializeClient({ trapUrls: [] });
107
+
108
+ expect(injectTrapsSpy).not.toHaveBeenCalled();
109
+ });
110
+
111
+ it('should initialize fetch interception when fetch config is present', () => {
112
+ const targetDomains = ['api.example.com'];
113
+ ClientLibrary.initializeClient({ fetch: { targetDomains } });
114
+
115
+ expect(initFetchSpy).toHaveBeenCalledWith(targetDomains);
116
+ });
117
+
118
+ it('should not initialize fetch interception if fetch config is absent', () => {
119
+ ClientLibrary.initializeClient(); // No fetch config
120
+
121
+ expect(initFetchSpy).not.toHaveBeenCalled();
122
+ });
123
+
124
+ it('should call all initializers correctly when a full config is provided', () => {
125
+ const config = {
126
+ mouse: true,
127
+ keystrokes: true,
128
+ honeypots: ['field1'],
129
+ trapUrls: ['/trap1'],
130
+ fetch: { targetDomains: ['api.com'] }
131
+ };
132
+ ClientLibrary.initializeClient(config);
133
+
134
+ expect(startMouseSpy).toHaveBeenCalled();
135
+ expect(startKeystrokeSpy).toHaveBeenCalled();
136
+ expect(initHoneypotsSpy).toHaveBeenCalledWith(config.honeypots);
137
+ expect(injectTrapsSpy).toHaveBeenCalledWith(config.trapUrls);
138
+ expect(initFetchSpy).toHaveBeenCalledWith(config.fetch.targetDomains);
139
+ });
140
+
120
141
  });