1 |
|
2 |
|
3 | import _ from 'lodash'
|
4 | import fs from 'fs-extra'
|
5 | import path from 'path'
|
6 | import temp from 'temp'
|
7 |
|
8 | import { File } from '../src/main'
|
9 | import type { Event } from '../src/types'
|
10 |
|
11 | export async function cloneFixtures () {
|
12 | const tempPath = fs.realpathSync(temp.mkdirSync('dicy'))
|
13 | let fixturesPath = path.resolve(__dirname, 'fixtures')
|
14 | await File.copy(fixturesPath, tempPath)
|
15 | return tempPath
|
16 | }
|
17 |
|
18 | function formatMessage (event: Event) {
|
19 | switch (event.type) {
|
20 | case 'command':
|
21 | return ` [${event.rule}] Executing command \`${event.command}\``
|
22 | case 'action':
|
23 | const triggerText = event.triggers.length === 0 ? '' : ` triggered by ${event.triggers.join(', ')}`
|
24 | return ` [${event.rule}] Evaluating ${event.action}${triggerText}`
|
25 | case 'log':
|
26 | const parts = []
|
27 |
|
28 | if (event.name) parts.push(`[${event.name}]`)
|
29 | if (event.category) parts.push(`${event.category}:`)
|
30 | parts.push(event.text)
|
31 |
|
32 | return ` ${parts.join(' ')}`
|
33 | }
|
34 | }
|
35 |
|
36 | function constructMessage (found: Array<Event>, missing: Array<Event>) {
|
37 | const lines = []
|
38 | if (found.length !== 0) {
|
39 | lines.push('Did not expect the following events:', ...found.map(formatMessage))
|
40 | }
|
41 | if (missing.length !== 0) {
|
42 | lines.push('Expected the following events:', ...missing.map(formatMessage))
|
43 | }
|
44 | return lines.join('\n')
|
45 | }
|
46 |
|
47 | function compareFilePaths (x: string, y: string): boolean {
|
48 | return path.normalize(x) === path.normalize(y) || ((path.isAbsolute(x) || path.isAbsolute(y)) && path.basename(x) === path.basename(y))
|
49 | }
|
50 |
|
51 | function stringCompare (x: string, y: string): boolean {
|
52 | return x.replace(/[/\\'"^]/g, '') === y.replace(/[/\\'"^]/g, '')
|
53 | }
|
54 |
|
55 | export function partitionMessages (received: Array<Event>, expected: Array<Event>) {
|
56 | let proper = []
|
57 | let improper = []
|
58 | let missing = _.uniqWith(expected, _.isEqual)
|
59 |
|
60 | for (const event of _.uniqWith(received, _.isEqual)) {
|
61 | let index = missing.findIndex(candidate =>
|
62 | _.isMatchWith(event, candidate, (x, y, key) => key === 'file'
|
63 | ? compareFilePaths(x, y)
|
64 | : ((typeof x === 'string' && typeof y === 'string')
|
65 | ? stringCompare(x, y)
|
66 | : undefined)))
|
67 | if (index === -1) {
|
68 | improper.push(event)
|
69 | } else {
|
70 | proper.push(event)
|
71 | missing.splice(index, 1)
|
72 | }
|
73 | }
|
74 |
|
75 | return { proper, improper, missing }
|
76 | }
|
77 |
|
78 | export const customMatchers = {
|
79 | toReceiveEvents (util: Object, customEqualityTesters: Object) {
|
80 | return {
|
81 | compare: function (received: Array<Event>, expected: Array<Event>) {
|
82 | const { proper, improper, missing } = partitionMessages(received, expected)
|
83 |
|
84 | const pass = improper.length === 0 && missing.length === 0
|
85 | const message = pass
|
86 | ? constructMessage(proper, [])
|
87 | : constructMessage(improper, missing)
|
88 |
|
89 | return { pass, message }
|
90 | }
|
91 | }
|
92 | }
|
93 | }
|