@tech4good/angular-schematics 21.4.0 → 21.5.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/CHANGELOG.md +17 -0
- package/package.json +1 -1
- package/src/ng-add/files/.firebaserc.template +2 -1
- package/src/ng-add/files/bigquery/queries/active_time_by_route.sql.template +34 -0
- package/src/ng-add/files/bigquery/scripts/export_entities.py.template +143 -0
- package/src/ng-add/files/bigquery/scripts/run_query.py.template +167 -0
- package/src/ng-add/files/bigquery/views/events_flat.sql.template +60 -0
- package/src/ng-add/files/bigquery/views/logs_latest.sql.template +34 -0
- package/src/ng-add/files/docs-infra/analytics-guide.md.template +170 -0
- package/src/ng-add/files/firebase.logs.json.template +5 -0
- package/src/ng-add/files/firestore.indexes.json.template +89 -0
- package/src/ng-add/files/firestore.logs.rules.spec.ts.template +100 -0
- package/src/ng-add/files/firestore.logs.rules.template +8 -0
- package/src/ng-add/files/jest.config.ts.template +1 -1
- package/src/ng-add/files/package.json.template +4 -3
- package/src/ng-add/files/src/app/core/analytics/log.service.ts.template +26 -4
- package/src/ng-add/files/src/app/core/analytics/time-analytics.service.spec.ts.template +145 -2
- package/src/ng-add/files/src/app/core/analytics/time-analytics.service.ts.template +355 -149
- package/src/ng-add/files/src/app/core/auth/guards/require-auth-and-consent.guard.ts.template +3 -1
- package/src/ng-add/files/src/app/first-time/consent/consent.component.html.template +172 -48
- package/src/ng-add/files/src/app/first-time/consent/consent.component.scss.template +31 -0
- package/src/ng-add/files/src/app/first-time/consent/consent.component.spec.ts.template +31 -3
- package/src/ng-add/files/src/app/first-time/consent/consent.component.ts.template +12 -11
- package/src/ng-add/files/src/app/first-time/consent/consent.guard.ts.template +0 -6
- package/src/ng-add/files/src/app/first-time/early-access/early-access.guard.ts.template +7 -1
- package/src/ng-add/files/src/app/first-time/first-time-routing.spec.ts.template +7 -6
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{
|
|
2
|
+
"indexes": [
|
|
3
|
+
{
|
|
4
|
+
"collectionGroup": "users",
|
|
5
|
+
"queryScope": "COLLECTION",
|
|
6
|
+
"fields": [
|
|
7
|
+
{
|
|
8
|
+
"fieldPath": "_deleted",
|
|
9
|
+
"order": "ASCENDING"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"fieldPath": "accessState",
|
|
13
|
+
"order": "ASCENDING"
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"fieldPath": "joinedWaitlistAt",
|
|
17
|
+
"order": "ASCENDING"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"fieldPath": "__name__",
|
|
21
|
+
"order": "ASCENDING"
|
|
22
|
+
}
|
|
23
|
+
],
|
|
24
|
+
"density": "SPARSE_ALL"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"collectionGroup": "users",
|
|
28
|
+
"queryScope": "COLLECTION",
|
|
29
|
+
"fields": [
|
|
30
|
+
{
|
|
31
|
+
"fieldPath": "accessState",
|
|
32
|
+
"order": "ASCENDING"
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
"fieldPath": "_createdAt",
|
|
36
|
+
"order": "ASCENDING"
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
"fieldPath": "__name__",
|
|
40
|
+
"order": "ASCENDING"
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
"density": "SPARSE_ALL"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"collectionGroup": "users",
|
|
47
|
+
"queryScope": "COLLECTION",
|
|
48
|
+
"fields": [
|
|
49
|
+
{
|
|
50
|
+
"fieldPath": "accessState",
|
|
51
|
+
"order": "ASCENDING"
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
"fieldPath": "_updatedAt",
|
|
55
|
+
"order": "DESCENDING"
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
"fieldPath": "__name__",
|
|
59
|
+
"order": "DESCENDING"
|
|
60
|
+
}
|
|
61
|
+
],
|
|
62
|
+
"density": "SPARSE_ALL"
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"collectionGroup": "users",
|
|
66
|
+
"queryScope": "COLLECTION",
|
|
67
|
+
"fields": [
|
|
68
|
+
{
|
|
69
|
+
"fieldPath": "__id",
|
|
70
|
+
"order": "ASCENDING"
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
"fieldPath": "_deleted",
|
|
74
|
+
"order": "ASCENDING"
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
"fieldPath": "_updatedAt",
|
|
78
|
+
"order": "ASCENDING"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"fieldPath": "__name__",
|
|
82
|
+
"order": "ASCENDING"
|
|
83
|
+
}
|
|
84
|
+
],
|
|
85
|
+
"density": "SPARSE_ALL"
|
|
86
|
+
}
|
|
87
|
+
],
|
|
88
|
+
"fieldOverrides": []
|
|
89
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment node
|
|
3
|
+
*/
|
|
4
|
+
import {
|
|
5
|
+
assertFails,
|
|
6
|
+
assertSucceeds,
|
|
7
|
+
initializeTestEnvironment,
|
|
8
|
+
RulesTestEnvironment,
|
|
9
|
+
} from '@firebase/rules-unit-testing';
|
|
10
|
+
import { readFileSync } from 'fs';
|
|
11
|
+
import { doc, setDoc, updateDoc, getDoc, deleteDoc } from 'firebase/firestore';
|
|
12
|
+
|
|
13
|
+
let testEnv: RulesTestEnvironment;
|
|
14
|
+
|
|
15
|
+
beforeAll(async () => {
|
|
16
|
+
testEnv = await initializeTestEnvironment({
|
|
17
|
+
projectId: 'demo-logs-rules-test',
|
|
18
|
+
firestore: {
|
|
19
|
+
rules: readFileSync('firestore.logs.rules', 'utf8'),
|
|
20
|
+
host: '127.0.0.1',
|
|
21
|
+
port: 8080,
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
await testEnv.clearFirestore();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
afterAll(async () => {
|
|
31
|
+
await testEnv.cleanup();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('Firestore Logs Security Rules', () => {
|
|
35
|
+
it('should allow anyone (unauthenticated) to create and update logs', async () => {
|
|
36
|
+
const unauthedDb = testEnv.unauthenticatedContext().firestore();
|
|
37
|
+
const logDoc = doc(unauthedDb, 'logs/log_123');
|
|
38
|
+
|
|
39
|
+
// Create log
|
|
40
|
+
await assertSucceeds(
|
|
41
|
+
setDoc(logDoc, {
|
|
42
|
+
__sessionId: 'session_abc',
|
|
43
|
+
route: '/s/session_1',
|
|
44
|
+
startTime: Date.now(),
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
// Update log
|
|
49
|
+
await assertSucceeds(
|
|
50
|
+
updateDoc(logDoc, {
|
|
51
|
+
_updatedAt: Date.now(),
|
|
52
|
+
})
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should allow authenticated users to create and update logs', async () => {
|
|
57
|
+
const authDb = testEnv.authenticatedContext('student_1').firestore();
|
|
58
|
+
const logDoc = doc(authDb, 'logs/log_456');
|
|
59
|
+
|
|
60
|
+
// Create log
|
|
61
|
+
await assertSucceeds(
|
|
62
|
+
setDoc(logDoc, {
|
|
63
|
+
__sessionId: 'session_abc',
|
|
64
|
+
route: '/s/session_1',
|
|
65
|
+
startTime: Date.now(),
|
|
66
|
+
})
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
// Update log
|
|
70
|
+
await assertSucceeds(
|
|
71
|
+
updateDoc(logDoc, {
|
|
72
|
+
_updatedAt: Date.now(),
|
|
73
|
+
})
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should deny anyone from reading logs', async () => {
|
|
78
|
+
const unauthedDb = testEnv.unauthenticatedContext().firestore();
|
|
79
|
+
const authDb = testEnv.authenticatedContext('student_1').firestore();
|
|
80
|
+
|
|
81
|
+
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
82
|
+
await setDoc(doc(context.firestore(), 'logs/log_123'), { data: 'private' });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
await assertFails(getDoc(doc(unauthedDb, 'logs/log_123')));
|
|
86
|
+
await assertFails(getDoc(doc(authDb, 'logs/log_123')));
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should deny anyone from deleting logs', async () => {
|
|
90
|
+
const unauthedDb = testEnv.unauthenticatedContext().firestore();
|
|
91
|
+
const authDb = testEnv.authenticatedContext('student_1').firestore();
|
|
92
|
+
|
|
93
|
+
await testEnv.withSecurityRulesDisabled(async (context) => {
|
|
94
|
+
await setDoc(doc(context.firestore(), 'logs/log_123'), { data: 'private' });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
await assertFails(deleteDoc(doc(unauthedDb, 'logs/log_123')));
|
|
98
|
+
await assertFails(deleteDoc(doc(authDb, 'logs/log_123')));
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -60,7 +60,7 @@ const config: Config = {
|
|
|
60
60
|
'/node_modules/',
|
|
61
61
|
'/dist/',
|
|
62
62
|
'<rootDir>/src/environments/environment.test.ts',
|
|
63
|
-
...(process.env['JEST_RULES'] ? [] : ['<rootDir>/firestore.rules.spec.ts'])
|
|
63
|
+
...(process.env['JEST_RULES'] ? [] : ['<rootDir>/firestore.rules.spec.ts', '<rootDir>/firestore.logs.rules.spec.ts'])
|
|
64
64
|
],
|
|
65
65
|
collectCoverageFrom: [
|
|
66
66
|
'src/app/**/*.ts',
|
|
@@ -10,13 +10,14 @@
|
|
|
10
10
|
"test": "jest",
|
|
11
11
|
"test:watch": "jest --watch",
|
|
12
12
|
"test:coverage": "jest --coverage",
|
|
13
|
-
"test:rules": "firebase emulators:exec --only firestore 'JEST_RULES=1 jest firestore.rules.spec.ts'",
|
|
13
|
+
"test:rules": "firebase emulators:exec --only firestore 'JEST_RULES=1 jest firestore.rules.spec.ts firestore.logs.rules.spec.ts'",
|
|
14
14
|
"lint": "ng lint",
|
|
15
15
|
"lintfix": "ng lint --fix",
|
|
16
16
|
"code-check": "ngc --noEmit && sass src:dist/css-check --no-source-map && rm -rf dist/css-check && npm run lintfix",
|
|
17
17
|
"analyze": "ng build --stats-json && esbuild-visualizer --metadata dist/<%= dasherize(project) %>/stats.json --filename dist/<%= dasherize(project) %>/bundle-analysis.html && open dist/<%= dasherize(project) %>/bundle-analysis.html",
|
|
18
18
|
"deploy:dev": "ng build --configuration development && firebase deploy --project dev",
|
|
19
|
-
"deploy:prod": "ng build --configuration production && firebase deploy --project prod"
|
|
19
|
+
"deploy:prod": "ng build --configuration production && firebase deploy --project prod",
|
|
20
|
+
"deploy:logs": "firebase deploy --only firestore:rules --project log --config firebase.logs.json"
|
|
20
21
|
},
|
|
21
22
|
"private": true,
|
|
22
23
|
"dependencies": {
|
|
@@ -55,7 +56,7 @@
|
|
|
55
56
|
"@firebase/rules-unit-testing": "^5.0.1",
|
|
56
57
|
"firebase-tools": "^14.27.0",
|
|
57
58
|
"@ngx-env/builder": "^21.0.1",
|
|
58
|
-
"@tech4good/angular-schematics": "^21.
|
|
59
|
+
"@tech4good/angular-schematics": "^21.5.0",
|
|
59
60
|
"@types/estree": "^1.0.0",
|
|
60
61
|
"@types/jest": "^30.0.0",
|
|
61
62
|
"@types/node": "^18.19.47",
|
|
@@ -6,6 +6,8 @@ import { TimeAtRoute } from './time-analytics.service';
|
|
|
6
6
|
|
|
7
7
|
interface LogData extends TimeAtRoute {
|
|
8
8
|
interactions?: Array<{ 0: number; 1: string; 2: string; 3: string; }>;
|
|
9
|
+
_createdAt?: number;
|
|
10
|
+
_updatedAt?: number;
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
/**
|
|
@@ -37,7 +39,12 @@ export class LogInDevService {
|
|
|
37
39
|
rawLog(data: LogData) {
|
|
38
40
|
if (this.LOG_IN_DEV) {
|
|
39
41
|
const logId = getLogId(data);
|
|
40
|
-
|
|
42
|
+
const updatedData = {
|
|
43
|
+
...data,
|
|
44
|
+
_createdAt: data._createdAt || data.startTime,
|
|
45
|
+
_updatedAt: Date.now(),
|
|
46
|
+
};
|
|
47
|
+
console.log('LOGGING', logId, updatedData);
|
|
41
48
|
}
|
|
42
49
|
}
|
|
43
50
|
|
|
@@ -45,7 +52,12 @@ export class LogInDevService {
|
|
|
45
52
|
if (this.LOG_IN_DEV) {
|
|
46
53
|
const logId = getLogId(data);
|
|
47
54
|
if (data !== null) {
|
|
48
|
-
|
|
55
|
+
const updatedData = {
|
|
56
|
+
...data,
|
|
57
|
+
_createdAt: data._createdAt || data.startTime,
|
|
58
|
+
_updatedAt: Date.now(),
|
|
59
|
+
};
|
|
60
|
+
console.log('BEACON LOGGING', logId, updatedData);
|
|
49
61
|
}
|
|
50
62
|
}
|
|
51
63
|
}
|
|
@@ -130,7 +142,12 @@ export class LogDBService {
|
|
|
130
142
|
// e.g. for storing things in localStorage and saving next time we load
|
|
131
143
|
// the browser
|
|
132
144
|
const docRef = doc(this.log, logType, logId);
|
|
133
|
-
|
|
145
|
+
const updatedData = {
|
|
146
|
+
...data,
|
|
147
|
+
_createdAt: data._createdAt || data.startTime,
|
|
148
|
+
_updatedAt: Date.now(),
|
|
149
|
+
};
|
|
150
|
+
setDoc(docRef, updatedData);
|
|
134
151
|
}
|
|
135
152
|
}
|
|
136
153
|
|
|
@@ -140,9 +157,14 @@ export class LogDBService {
|
|
|
140
157
|
|
|
141
158
|
if (data !== null) {
|
|
142
159
|
if (!isDevMode() && environment['firebaseLogs'] && environment['firebaseLogs']['projectId']) {
|
|
160
|
+
const updatedData = {
|
|
161
|
+
...data,
|
|
162
|
+
_createdAt: data._createdAt || data.startTime,
|
|
163
|
+
_updatedAt: Date.now(),
|
|
164
|
+
};
|
|
143
165
|
navigator.sendBeacon(
|
|
144
166
|
`https://firestore.googleapis.com/v1/projects/${environment['firebaseLogs']['projectId']}/databases/(default)/documents:commit`,
|
|
145
|
-
reformatLogForBeacon(
|
|
167
|
+
reformatLogForBeacon(updatedData, logType, logId),
|
|
146
168
|
);
|
|
147
169
|
}
|
|
148
170
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TestBed } from '@angular/core/testing';
|
|
2
|
+
import { signal, Injector } from '@angular/core';
|
|
2
3
|
import { Router, NavigationEnd, Event as RouterEvent } from '@angular/router';
|
|
3
|
-
import { Subject, of } from 'rxjs';
|
|
4
|
+
import { Subject, BehaviorSubject, of } from 'rxjs';
|
|
4
5
|
import { TimeAnalyticsService } from './time-analytics.service';
|
|
5
6
|
import { DATABASE_SERVICE } from '../firebase/database.service';
|
|
6
7
|
import { LOG_SERVICE } from './log.service';
|
|
@@ -136,5 +137,147 @@ describe('TimeAnalyticsService', () => {
|
|
|
136
137
|
// Cleanup
|
|
137
138
|
dateSpy.mockRestore();
|
|
138
139
|
});
|
|
139
|
-
});
|
|
140
140
|
|
|
141
|
+
it('should serialize objects as JSON strings when tracking interactions', () => {
|
|
142
|
+
let currentTime = 1000;
|
|
143
|
+
const dateSpy = jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
|
|
144
|
+
const logService = TestBed.inject(LOG_SERVICE) as any;
|
|
145
|
+
|
|
146
|
+
// Start logging
|
|
147
|
+
service.startTimeLogging();
|
|
148
|
+
|
|
149
|
+
// 1. User routes to /home
|
|
150
|
+
currentTime = 1000;
|
|
151
|
+
routerEvents$.next(new NavigationEnd(1, '/home', '/home'));
|
|
152
|
+
document.dispatchEvent(new Event('click'));
|
|
153
|
+
|
|
154
|
+
// 2. User interacts again on /home at T=2000 to accumulate active time
|
|
155
|
+
currentTime = 2000;
|
|
156
|
+
document.dispatchEvent(new Event('click'));
|
|
157
|
+
|
|
158
|
+
// 3. User tracks interaction with object payload
|
|
159
|
+
currentTime = 2500;
|
|
160
|
+
const payload = { key: 'value', count: 123 };
|
|
161
|
+
service.trackInteraction('category', 'action', payload);
|
|
162
|
+
|
|
163
|
+
// 4. User interacts again on /home at T=3000
|
|
164
|
+
currentTime = 3000;
|
|
165
|
+
document.dispatchEvent(new Event('click'));
|
|
166
|
+
|
|
167
|
+
// 5. User routes away to save the log
|
|
168
|
+
currentTime = 4000;
|
|
169
|
+
routerEvents$.next(new NavigationEnd(2, '/profile', '/profile'));
|
|
170
|
+
document.dispatchEvent(new Event('click'));
|
|
171
|
+
|
|
172
|
+
// Verify interaction was serialized
|
|
173
|
+
expect(logService.rawLog).toHaveBeenCalledTimes(1);
|
|
174
|
+
const savedLog = logService.rawLog.mock.calls[0][0];
|
|
175
|
+
|
|
176
|
+
expect(savedLog.interactions).toBeDefined();
|
|
177
|
+
expect(savedLog.interactions.length).toEqual(1);
|
|
178
|
+
expect(savedLog.interactions[0]).toEqual({
|
|
179
|
+
0: 2500,
|
|
180
|
+
1: 'category',
|
|
181
|
+
2: 'action',
|
|
182
|
+
3: JSON.stringify(payload),
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
dateSpy.mockRestore();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should track active time per registered state signal and flush log to DB when state changes', () => {
|
|
189
|
+
let currentTime = 1000;
|
|
190
|
+
const dateSpy = jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
|
|
191
|
+
const logService = TestBed.inject(LOG_SERVICE) as any;
|
|
192
|
+
|
|
193
|
+
service.startTimeLogging();
|
|
194
|
+
|
|
195
|
+
// 1. User routes to /practice at T=1000
|
|
196
|
+
currentTime = 1000;
|
|
197
|
+
routerEvents$.next(new NavigationEnd(1, '/practice', '/practice'));
|
|
198
|
+
|
|
199
|
+
const problemSignal = signal<string | null>('prob-1');
|
|
200
|
+
service.registerState('problemId', problemSignal, TestBed.inject(Injector));
|
|
201
|
+
TestBed.flushEffects();
|
|
202
|
+
|
|
203
|
+
// Initial click at T=1000
|
|
204
|
+
document.dispatchEvent(new Event('click'));
|
|
205
|
+
|
|
206
|
+
// 2. User interacts again on prob-1 at T=2000
|
|
207
|
+
currentTime = 2000;
|
|
208
|
+
document.dispatchEvent(new Event('click'));
|
|
209
|
+
|
|
210
|
+
// 3. User changes state to prob-2 at T=3000
|
|
211
|
+
currentTime = 3000;
|
|
212
|
+
problemSignal.set('prob-2');
|
|
213
|
+
TestBed.flushEffects();
|
|
214
|
+
document.dispatchEvent(new Event('click'));
|
|
215
|
+
|
|
216
|
+
// rawLog should be called for prob-1
|
|
217
|
+
expect(logService.rawLog).toHaveBeenCalledTimes(1);
|
|
218
|
+
const prob1Log = logService.rawLog.mock.calls[0][0];
|
|
219
|
+
expect(prob1Log.route).toEqual('/practice');
|
|
220
|
+
expect(prob1Log.state).toEqual({ problemId: 'prob-1' });
|
|
221
|
+
expect(prob1Log.time.find((t: any) => t.type === 'active').total).toEqual(1000);
|
|
222
|
+
|
|
223
|
+
// 4. User interacts again on prob-2 at T=4000
|
|
224
|
+
currentTime = 4000;
|
|
225
|
+
document.dispatchEvent(new Event('click'));
|
|
226
|
+
|
|
227
|
+
// 5. User routes away to /profile at T=5000
|
|
228
|
+
currentTime = 5000;
|
|
229
|
+
routerEvents$.next(new NavigationEnd(2, '/profile', '/profile'));
|
|
230
|
+
document.dispatchEvent(new Event('click'));
|
|
231
|
+
|
|
232
|
+
// rawLog should be called for prob-2
|
|
233
|
+
expect(logService.rawLog).toHaveBeenCalledTimes(2);
|
|
234
|
+
const prob2Log = logService.rawLog.mock.calls[1][0];
|
|
235
|
+
expect(prob2Log.route).toEqual('/practice');
|
|
236
|
+
expect(prob2Log.state).toEqual({ problemId: 'prob-2' });
|
|
237
|
+
expect(prob2Log.time.find((t: any) => t.type === 'active').total).toEqual(2000);
|
|
238
|
+
|
|
239
|
+
dateSpy.mockRestore();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('should support registering Angular signals via registerState', () => {
|
|
243
|
+
let currentTime = 1000;
|
|
244
|
+
const dateSpy = jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
|
|
245
|
+
const logService = TestBed.inject(LOG_SERVICE) as any;
|
|
246
|
+
|
|
247
|
+
service.startTimeLogging();
|
|
248
|
+
|
|
249
|
+
currentTime = 1000;
|
|
250
|
+
routerEvents$.next(new NavigationEnd(1, '/practice', '/practice'));
|
|
251
|
+
|
|
252
|
+
const problemSignal = signal<string | null>('prob-100');
|
|
253
|
+
service.registerState('problemId', problemSignal, TestBed.inject(Injector));
|
|
254
|
+
TestBed.flushEffects();
|
|
255
|
+
|
|
256
|
+
document.dispatchEvent(new Event('click'));
|
|
257
|
+
|
|
258
|
+
currentTime = 2000;
|
|
259
|
+
document.dispatchEvent(new Event('click'));
|
|
260
|
+
|
|
261
|
+
// Update signal value and flush Angular signal effects
|
|
262
|
+
currentTime = 3000;
|
|
263
|
+
problemSignal.set('prob-200');
|
|
264
|
+
TestBed.flushEffects();
|
|
265
|
+
document.dispatchEvent(new Event('click'));
|
|
266
|
+
|
|
267
|
+
// Should flush log for prob-100
|
|
268
|
+
expect(logService.rawLog).toHaveBeenCalledTimes(1);
|
|
269
|
+
const log1 = logService.rawLog.mock.calls[0][0];
|
|
270
|
+
expect(log1.state).toEqual({ problemId: 'prob-100' });
|
|
271
|
+
|
|
272
|
+
currentTime = 4000;
|
|
273
|
+
routerEvents$.next(new NavigationEnd(2, '/profile', '/profile'));
|
|
274
|
+
document.dispatchEvent(new Event('click'));
|
|
275
|
+
|
|
276
|
+
// Should flush log for prob-200
|
|
277
|
+
expect(logService.rawLog).toHaveBeenCalledTimes(2);
|
|
278
|
+
const log2 = logService.rawLog.mock.calls[1][0];
|
|
279
|
+
expect(log2.state).toEqual({ problemId: 'prob-200' });
|
|
280
|
+
|
|
281
|
+
dateSpy.mockRestore();
|
|
282
|
+
});
|
|
283
|
+
});
|