-
Notifications
You must be signed in to change notification settings - Fork 15
/
embed.js
204 lines (182 loc) · 5.92 KB
/
embed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const express = require('express');
const { resolve } = require('path');
const { create } = require('express-handlebars');
const {
ApolloClient,
gql,
HttpLink,
InMemoryCache
} = require('@apollo/client');
const fetch = require('cross-fetch');
const app = express();
const handlebarsPath =
process.env.ENVIRONMENT === 'dev' ? 'handlebars' : 'server/handlebars';
// handlebars
const hbs = create({
layoutsDir: resolve(`${handlebarsPath}/views/layouts`),
extname: 'hbs',
defaultLayout: 'index',
helpers: require(resolve(`${handlebarsPath}/helpers`))
});
app.engine('hbs', hbs.engine);
app.set('view engine', 'hbs');
app.set('views', resolve(`${handlebarsPath}/views`));
if (process.env.ENVIRONMENT !== 'dev') {
app.enable('view cache');
}
const client = new ApolloClient({
link: new HttpLink({ uri: 'http://localhost:8000/api/graphql', fetch }),
cache: new InMemoryCache()
});
const getLatestReportsForPattern = async pattern => {
const { data } = await client.query({
query: gql`
query {
testPlanReports(statuses: [CANDIDATE, RECOMMENDED]) {
id
metrics
status
at {
id
name
}
browser {
id
name
}
latestAtVersionReleasedAt {
id
name
releasedAt
}
testPlanVersion {
id
title
updatedAt
testPlan {
id
}
}
}
}
`
});
let title;
const testPlanReports = data.testPlanReports.filter(report => {
if (report.testPlanVersion.testPlan.id === pattern) {
title = report.testPlanVersion.title;
return true;
}
});
let allAts = new Set();
let allBrowsers = new Set();
let allAtVersionsByAt = {};
let status = 'RECOMMENDED';
let reportsByAt = {};
let testPlanVersionIds = new Set();
const uniqueReports = [];
let latestReports = [];
testPlanReports.forEach(report => {
allAts.add(report.at.name);
allBrowsers.add(report.browser.name);
if (report.status === 'CANDIDATE') {
status = report.status;
}
if (!allAtVersionsByAt[report.at.name])
allAtVersionsByAt[report.at.name] =
report.latestAtVersionReleasedAt;
else if (
new Date(report.latestAtVersionReleasedAt.releasedAt) >
new Date(allAtVersionsByAt[report.at.name].releasedAt)
) {
allAtVersionsByAt[report.at.name] =
report.latestAtVersionReleasedAt;
}
const sameAtAndBrowserReports = testPlanReports.filter(
r =>
r.at.name === report.at.name &&
r.browser.name === report.browser.name
);
// Only add a group of reports with same
// AT and browser once
if (
!uniqueReports.find(group =>
group.some(
g =>
g.at.name === report.at.name &&
g.browser.name === report.browser.name
)
)
) {
uniqueReports.push(sameAtAndBrowserReports);
}
testPlanVersionIds.add(report.testPlanVersion.id);
});
uniqueReports.forEach(group => {
if (group.length <= 1) {
latestReports.push(group.pop());
} else {
const latestReport = group
.sort(
(a, b) =>
new Date(a.testPlanVersion.updatedAt) -
new Date(b.testPlanVersion.updatedAt)
)
.pop();
latestReports.push(latestReport);
}
});
allBrowsers = Array.from(allBrowsers).sort();
testPlanVersionIds = Array.from(testPlanVersionIds);
const allAtsAlphabetical = Array.from(allAts).sort((a, b) =>
a.localeCompare(b)
);
allAtsAlphabetical.forEach(at => {
reportsByAt[at] = latestReports
.filter(report => report.at.name === at)
.sort((a, b) => a.browser.name.localeCompare(b.browser.name));
});
return {
title,
allBrowsers,
allAtVersionsByAt,
testPlanVersionIds,
status,
reportsByAt
};
};
app.get('/reports/:pattern', async (req, res) => {
// In the instance where an editor doesn't want to display a certain title
// as it has defined when importing into the ARIA-AT database for being too
// verbose, etc. eg. `Link Example 1 (span element with text content)`
// Usage: https://aria-at.w3.org/embed/reports/command-button?title=Link+Example+(span+element+with+text+content)
const queryTitle = req.query.title;
const pattern = req.params.pattern;
const protocol = /dev|vagrant/.test(process.env.ENVIRONMENT)
? 'http://'
: 'https://';
const {
title,
allBrowsers,
allAtVersionsByAt,
testPlanVersionIds,
status,
reportsByAt
} = await getLatestReportsForPattern(pattern);
res.render('main', {
layout: 'index',
dataEmpty: Object.keys(reportsByAt).length === 0,
title: queryTitle || title || 'Pattern Not Found',
pattern,
status,
allBrowsers,
allAtVersionsByAt,
reportsByAt,
completeReportLink: `${protocol}${
req.headers.host
}/report/${testPlanVersionIds.join(',')}`,
embedLink: `${protocol}${req.headers.host}/embed/reports/${pattern}`
});
});
app.use(express.static(resolve(`${handlebarsPath}/public`)));
module.exports = app;