@playwright-orchestrator/mongo 1.1.4 → 1.2.1

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.
@@ -1,23 +1,29 @@
1
- import { TestItem, TestRunInfo, Adapter, TestRunConfig } from '@playwright-orchestrator/core';
1
+ import { TestItem, Adapter, TestRunConfig, TestRunReport, ResultTestParams, SaveTestRunParams } from '@playwright-orchestrator/core';
2
2
  import { CreateArgs } from './create-args.js';
3
3
  export declare class MongoDbAdapter extends Adapter {
4
4
  private readonly runsCollection;
5
5
  private readonly testsCollection;
6
+ private readonly testsInfoCollection;
6
7
  private readonly debug;
7
8
  private readonly db;
8
9
  private readonly client;
9
10
  constructor(args: CreateArgs);
10
11
  getNextTest(runId: string, config: TestRunConfig): Promise<TestItem | undefined>;
11
- finishTest(runId: string, test: TestItem): Promise<void>;
12
- failTest(runId: string, test: TestItem): Promise<void>;
13
- private updateTestStatus;
14
- saveTestRun(runId: string, testRun: TestRunInfo, args: string[]): Promise<void>;
12
+ finishTest(params: ResultTestParams): Promise<void>;
13
+ failTest(params: ResultTestParams): Promise<void>;
14
+ private updateTest;
15
+ saveTestRun({ testRun, runId, args, historyWindow }: SaveTestRunParams): Promise<void>;
16
+ private loadTestInfo;
15
17
  initialize(): Promise<void>;
16
18
  startShard(runId: string): Promise<TestRunConfig>;
19
+ private generateTestIdQuery;
20
+ private mapDbToTestRunConfig;
17
21
  finishShard(runId: string): Promise<void>;
18
22
  dispose(): Promise<void>;
23
+ getReportData(runId: string): Promise<TestRunReport>;
19
24
  private get runs();
20
25
  private get tests();
26
+ private get testInfo();
21
27
  private generateTestId;
22
28
  private generateRunId;
23
29
  private parseTestId;
@@ -5,6 +5,7 @@ const MAX_ORDER = 0b1111111111111111;
5
5
  export class MongoDbAdapter extends Adapter {
6
6
  runsCollection;
7
7
  testsCollection;
8
+ testsInfoCollection;
8
9
  debug;
9
10
  db;
10
11
  client;
@@ -24,16 +25,11 @@ export class MongoDbAdapter extends Adapter {
24
25
  this.db = this.client.db(db);
25
26
  this.runsCollection = `${collectionNamePrefix}_test_runs`;
26
27
  this.testsCollection = `${collectionNamePrefix}_tests`;
28
+ this.testsInfoCollection = `${collectionNamePrefix}_tests_info`;
27
29
  this.debug = debug ?? false;
28
30
  }
29
31
  async getNextTest(runId, config) {
30
- const result = await this.tests.findOneAndUpdate({
31
- _id: {
32
- $gte: this.generateTestId(runId, 0),
33
- $lt: this.generateTestId(runId, MAX_ORDER),
34
- },
35
- status: { $in: [TestStatus.Ready] },
36
- }, {
32
+ const result = await this.tests.findOneAndUpdate(this.generateTestIdQuery(runId, TestStatus.Ready), {
37
33
  $set: {
38
34
  updated: new Date(),
39
35
  status: TestStatus.Ongoing,
@@ -51,29 +47,60 @@ export class MongoDbAdapter extends Adapter {
51
47
  order,
52
48
  };
53
49
  }
54
- async finishTest(runId, test) {
55
- await this.updateTestStatus(runId, test, TestStatus.Passed);
50
+ async finishTest(params) {
51
+ await this.updateTest(TestStatus.Passed, params);
56
52
  }
57
- async failTest(runId, test) {
58
- await this.updateTestStatus(runId, test, TestStatus.Failed);
53
+ async failTest(params) {
54
+ await this.updateTest(TestStatus.Failed, params);
59
55
  }
60
- async updateTestStatus(runId, test, status) {
56
+ async updateTest(status, { test, runId, testResult, config }) {
61
57
  const { order } = test;
62
- const id = this.generateTestId(runId, order);
63
- await this.tests.updateOne({ _id: id }, {
58
+ const { duration, title } = testResult;
59
+ const testId = this.generateTestId(runId, order);
60
+ const testInfoId = this.getTestId({ ...test, ...testResult });
61
+ const testInfo = (await this.testInfo.findOne({ _id: testInfoId }));
62
+ this.testInfo.updateOne({ _id: testInfoId }, {
63
+ $set: {
64
+ ema: this.calculateEMA(duration, testInfo.ema, config.historyWindow),
65
+ },
66
+ // @ts-ignore
67
+ $push: {
68
+ history: {
69
+ $each: [
70
+ {
71
+ duration,
72
+ status,
73
+ updated: new Date(),
74
+ },
75
+ ],
76
+ $slice: -config.historyWindow,
77
+ },
78
+ },
79
+ });
80
+ await this.tests.updateOne({ _id: testId }, {
64
81
  $set: {
65
82
  status,
66
83
  updated: new Date(),
84
+ report: {
85
+ duration: duration,
86
+ title: title,
87
+ ema: testInfo.ema,
88
+ fails: testInfo.history.filter((h) => h.status === TestStatus.Failed).length,
89
+ lastSuccessfulRun: testInfo.history.findLast((h) => h.status === TestStatus.Passed)?.updated,
90
+ },
67
91
  },
68
92
  });
69
93
  }
70
- async saveTestRun(runId, testRun, args) {
71
- const tests = this.flattenTestRun(testRun.testRun);
94
+ async saveTestRun({ testRun, runId, args, historyWindow }) {
95
+ let tests = this.transformTestRunToItems(testRun.testRun);
96
+ const testInfos = await this.loadTestInfo(tests);
97
+ tests = this.sortTests(tests, testInfos, { historyWindow });
72
98
  const run = {
73
99
  _id: this.generateRunId(runId),
74
100
  status: RunStatus.Created,
75
101
  config: testRun.config,
76
102
  args,
103
+ historyWindow,
77
104
  updated: new Date(),
78
105
  };
79
106
  await this.runs.insertOne(run);
@@ -92,15 +119,39 @@ export class MongoDbAdapter extends Adapter {
92
119
  };
93
120
  }));
94
121
  }
122
+ async loadTestInfo(tests) {
123
+ const testInfoMap = new Map();
124
+ for (const { testId } of tests) {
125
+ if (!testInfoMap.has(testId)) {
126
+ const item = await this.testInfo.findOneAndUpdate({ _id: testId }, {
127
+ $setOnInsert: {
128
+ _id: testId,
129
+ create: new Date(),
130
+ ema: 0,
131
+ history: [],
132
+ },
133
+ }, { upsert: true, returnDocument: 'after' });
134
+ testInfoMap.set(testId, {
135
+ ema: item.ema,
136
+ fails: item.history.filter((h) => h.status === TestStatus.Failed).length,
137
+ });
138
+ }
139
+ }
140
+ return testInfoMap;
141
+ }
95
142
  async initialize() {
96
143
  const collections = await this.db.collections();
97
- if (!collections.some((c) => c.collectionName === this.runsCollection)) {
144
+ const set = new Set(collections.map((c) => c.collectionName));
145
+ if (!set.has(this.runsCollection)) {
98
146
  await this.db.createCollection(this.runsCollection);
99
147
  }
100
- if (!collections.some((c) => c.collectionName === this.testsCollection)) {
148
+ if (!set.has(this.testsCollection)) {
101
149
  const collection = await this.db.createCollection(this.testsCollection);
102
150
  await collection.createIndex({ status: 1 });
103
151
  }
152
+ if (!set.has(this.testsInfoCollection)) {
153
+ await this.db.createCollection(this.testsInfoCollection);
154
+ }
104
155
  }
105
156
  async startShard(runId) {
106
157
  const now = new Date();
@@ -123,21 +174,27 @@ export class MongoDbAdapter extends Adapter {
123
174
  }
124
175
  const { status: statusBefore } = run;
125
176
  if (statusBefore === RunStatus.Created || statusBefore === RunStatus.Finished) {
126
- await this.tests.updateMany({
127
- _id: {
128
- $gte: this.generateTestId(runId, 0),
129
- $lt: this.generateTestId(runId, MAX_ORDER),
130
- },
131
- status: TestStatus.Failed,
132
- }, {
177
+ await this.tests.updateMany(this.generateTestIdQuery(runId, TestStatus.Failed), {
133
178
  $set: {
134
179
  status: TestStatus.Ready,
135
180
  updated: now,
136
181
  },
137
182
  });
138
183
  }
139
- const { args, config, status, updated } = run;
140
- return { ...config, args, status, updated: updated.getTime() };
184
+ return this.mapDbToTestRunConfig(run);
185
+ }
186
+ generateTestIdQuery(runId, ...statuses) {
187
+ return {
188
+ _id: {
189
+ $gte: this.generateTestId(runId, 0),
190
+ $lt: this.generateTestId(runId, MAX_ORDER),
191
+ },
192
+ status: { $in: statuses },
193
+ };
194
+ }
195
+ mapDbToTestRunConfig(run) {
196
+ const { args, config, status, updated, historyWindow } = run;
197
+ return { ...config, args, historyWindow, status, updated: updated.getTime() };
141
198
  }
142
199
  async finishShard(runId) {
143
200
  await this.runs.updateOne({ _id: uuid.parse(runId) }, {
@@ -150,12 +207,44 @@ export class MongoDbAdapter extends Adapter {
150
207
  async dispose() {
151
208
  await this.client.close();
152
209
  }
210
+ async getReportData(runId) {
211
+ const run = await this.runs.findOne({ _id: this.generateRunId(runId) });
212
+ if (!run) {
213
+ throw new Error(`Run ${runId} not found`);
214
+ }
215
+ const config = this.mapDbToTestRunConfig(run);
216
+ const tests = await this.tests
217
+ .find(this.generateTestIdQuery(runId, TestStatus.Failed, TestStatus.Passed))
218
+ .toArray();
219
+ return {
220
+ runId,
221
+ config,
222
+ tests: tests.map(({ file, line, column, project, status, report }) => {
223
+ const position = `${line}:${column}`;
224
+ const { duration, fails, title, lastSuccessfulRun, ema } = report;
225
+ return {
226
+ file,
227
+ position,
228
+ project,
229
+ status,
230
+ title,
231
+ duration,
232
+ fails,
233
+ lastSuccessfulRunTimestamp: lastSuccessfulRun?.getTime(),
234
+ averageDuration: ema,
235
+ };
236
+ }),
237
+ };
238
+ }
153
239
  get runs() {
154
240
  return this.db.collection(this.runsCollection);
155
241
  }
156
242
  get tests() {
157
243
  return this.db.collection(this.testsCollection);
158
244
  }
245
+ get testInfo() {
246
+ return this.db.collection(this.testsInfoCollection);
247
+ }
159
248
  generateTestId(runId, order) {
160
249
  const binaryRunId = uuid.parse(runId);
161
250
  const orderBytes = new Uint8Array(2);
@@ -0,0 +1,37 @@
1
+ import { RunStatus, TestConfig, TestItem, TestStatus } from '@playwright-orchestrator/core';
2
+ import { Binary, Document } from 'mongodb';
3
+ export interface TestRunDocument extends Document {
4
+ _id: Binary;
5
+ status: RunStatus;
6
+ config: TestConfig;
7
+ args: string[];
8
+ updated: Date;
9
+ historyWindow: number;
10
+ }
11
+ export interface TestDocument extends Document, Omit<TestItem, 'order' | 'position'> {
12
+ _id: Binary;
13
+ runId?: string;
14
+ order?: number;
15
+ line: number;
16
+ column: number;
17
+ status: TestStatus;
18
+ updated: Date;
19
+ report?: TestItemReport;
20
+ }
21
+ export interface TestItemReport {
22
+ duration: number;
23
+ title: string;
24
+ fails: number;
25
+ ema: number;
26
+ lastSuccessfulRun?: Date;
27
+ }
28
+ export interface TestInfoDocument extends Document {
29
+ _id: string;
30
+ create: Date;
31
+ ema: number;
32
+ history: {
33
+ duration: number;
34
+ status: TestStatus;
35
+ updated: Date;
36
+ }[];
37
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@playwright-orchestrator/mongo",
3
- "version": "1.1.4",
3
+ "version": "1.2.1",
4
4
  "keywords": [],
5
5
  "author": "Rostyslav Kudrevatykh",
6
6
  "license": "Apache-2.0",