@antelopejs/dms-api 0.2.0 → 0.4.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/README.md +7 -4
- package/dist/pages/request-table.js +1 -1
- package/dist/pages/request-table.js.map +1 -1
- package/frontend-vue/app/components/blocks/HealthHero.vue +11 -5
- package/frontend-vue/app/components/blocks/LiveTraffic.vue +3 -2
- package/frontend-vue/app/components/blocks/RequestDetail.vue +35 -33
- package/frontend-vue/app/components/blocks/RetentionInput.vue +51 -20
- package/frontend-vue/app/components/blocks/RouteTester.vue +86 -28
- package/frontend-vue/app/components/blocks/RouteTree.vue +9 -9
- package/frontend-vue/app/components/blocks/ScopeChip.vue +11 -7
- package/frontend-vue/app/composables/useClientValue.ts +18 -0
- package/frontend-vue/app/composables/useRouteDetail.ts +22 -106
- package/frontend-vue/app/utils/http.ts +10 -0
- package/frontend-vue/app/utils/testerRequest.ts +9 -7
- package/frontend-vue/dms.server.ts +9 -0
- package/frontend-vue/package.json +1 -1
- package/frontend-vue/server/tester.ts +266 -0
- package/frontend-vue/tests/clientValue.test.ts +64 -0
- package/frontend-vue/tests/dmsServer.test.ts +44 -0
- package/frontend-vue/tests/testerRequest.test.ts +1 -1
- package/frontend-vue/tests/testerRoute.test.ts +299 -0
- package/package.json +3 -3
- package/frontend-vue/app/composables/useApiResource.ts +0 -92
- package/frontend-vue/tests/tooltipTriggers.test.ts +0 -51
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { once } from 'node:events'
|
|
2
|
+
import {
|
|
3
|
+
createServer,
|
|
4
|
+
type IncomingHttpHeaders,
|
|
5
|
+
type IncomingMessage,
|
|
6
|
+
type Server,
|
|
7
|
+
type ServerResponse,
|
|
8
|
+
} from 'node:http'
|
|
9
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'
|
|
10
|
+
import type { DmsServerEvent } from '#dms/server'
|
|
11
|
+
import { testerRoute } from '../server/tester'
|
|
12
|
+
|
|
13
|
+
// The route's own checks, on the event the frontend server hands it. The
|
|
14
|
+
// server's (same-origin, the 64 KiB body limit, the session cookie) are its
|
|
15
|
+
// own and tested there.
|
|
16
|
+
|
|
17
|
+
const ACCESS_TOKEN = 'private/owner"access?token'
|
|
18
|
+
const REFRESH_TOKEN = 'private\\refresh"token'
|
|
19
|
+
const BODY_LIMIT = 64 * 1024
|
|
20
|
+
|
|
21
|
+
interface Call {
|
|
22
|
+
path: string
|
|
23
|
+
method: string
|
|
24
|
+
headers: IncomingHttpHeaders
|
|
25
|
+
body: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const calls: Call[] = []
|
|
29
|
+
let backend = ''
|
|
30
|
+
let server: Server
|
|
31
|
+
let owner: unknown = true
|
|
32
|
+
let ownerStatus = 200
|
|
33
|
+
|
|
34
|
+
async function fixture(request: IncomingMessage, response: ServerResponse) {
|
|
35
|
+
const chunks: Buffer[] = []
|
|
36
|
+
for await (const chunk of request) chunks.push(chunk as Buffer)
|
|
37
|
+
calls.push({
|
|
38
|
+
path: request.url ?? '',
|
|
39
|
+
method: request.method ?? '',
|
|
40
|
+
headers: request.headers,
|
|
41
|
+
body: Buffer.concat(chunks).toString(),
|
|
42
|
+
})
|
|
43
|
+
if (request.url === '/api/auth/me') {
|
|
44
|
+
response.writeHead(ownerStatus, { location: '/unexpected-owner-redirect' })
|
|
45
|
+
return response.end(JSON.stringify({ owner }))
|
|
46
|
+
}
|
|
47
|
+
if (request.url === '/api/network-error') return request.socket.destroy()
|
|
48
|
+
if (request.url === '/api/large')
|
|
49
|
+
return response.end('a'.repeat(BODY_LIMIT + 1))
|
|
50
|
+
if (request.url === '/api/redirect') {
|
|
51
|
+
response.writeHead(302, {
|
|
52
|
+
location: 'https://dms.example/unexpected-redirect',
|
|
53
|
+
'set-cookie': 'upstream=secret',
|
|
54
|
+
})
|
|
55
|
+
return response.end('redirect response')
|
|
56
|
+
}
|
|
57
|
+
response.writeHead(422, {
|
|
58
|
+
'x-result': 'fixture',
|
|
59
|
+
'set-cookie': 'upstream=secret',
|
|
60
|
+
'x-reflected': ACCESS_TOKEN,
|
|
61
|
+
'x-reflected-uri': encodeURIComponent(ACCESS_TOKEN),
|
|
62
|
+
'x-reflected-json': JSON.stringify(REFRESH_TOKEN).slice(1, -1),
|
|
63
|
+
connection: 'x-hop',
|
|
64
|
+
'x-hop': 'hidden',
|
|
65
|
+
})
|
|
66
|
+
response.end(
|
|
67
|
+
[
|
|
68
|
+
request.method,
|
|
69
|
+
request.url,
|
|
70
|
+
Buffer.concat(chunks),
|
|
71
|
+
ACCESS_TOKEN,
|
|
72
|
+
REFRESH_TOKEN,
|
|
73
|
+
encodeURIComponent(ACCESS_TOKEN),
|
|
74
|
+
JSON.stringify(REFRESH_TOKEN).slice(1, -1),
|
|
75
|
+
].join(':'),
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The URL the frontend server's `backendUrl` gives: always on its origin. */
|
|
80
|
+
function backendUrl(path: string): URL {
|
|
81
|
+
const { pathname, search, hash } = new URL(path, 'http://frontend.local')
|
|
82
|
+
const url = new URL(backend)
|
|
83
|
+
url.pathname = pathname
|
|
84
|
+
url.search = search
|
|
85
|
+
url.hash = hash
|
|
86
|
+
return url
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const signedIn = { accessToken: ACCESS_TOKEN, refreshToken: REFRESH_TOKEN }
|
|
90
|
+
|
|
91
|
+
async function invoke(
|
|
92
|
+
input: Record<string, unknown> = {},
|
|
93
|
+
session: unknown = signedIn,
|
|
94
|
+
readJson?: () => Promise<unknown>,
|
|
95
|
+
) {
|
|
96
|
+
const event = {
|
|
97
|
+
session: { read: () => session },
|
|
98
|
+
readJson:
|
|
99
|
+
readJson ??
|
|
100
|
+
(async () => ({
|
|
101
|
+
path: '/api/echo',
|
|
102
|
+
method: 'GET',
|
|
103
|
+
headers: {},
|
|
104
|
+
useSession: true,
|
|
105
|
+
...input,
|
|
106
|
+
})),
|
|
107
|
+
backendUrl,
|
|
108
|
+
} as unknown as DmsServerEvent
|
|
109
|
+
const response = await testerRoute(event)
|
|
110
|
+
return { response, data: await response.json() }
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
beforeAll(async () => {
|
|
114
|
+
server = createServer((request, response) => void fixture(request, response))
|
|
115
|
+
server.listen(0, '127.0.0.1')
|
|
116
|
+
await once(server, 'listening')
|
|
117
|
+
const address = server.address()
|
|
118
|
+
if (!address || typeof address !== 'object') throw new Error('no address')
|
|
119
|
+
backend = `http://127.0.0.1:${address.port}`
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
afterAll(async () => {
|
|
123
|
+
server.closeAllConnections()
|
|
124
|
+
await new Promise((settle) => server.close(settle))
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
beforeEach(() => {
|
|
128
|
+
calls.length = 0
|
|
129
|
+
owner = true
|
|
130
|
+
ownerStatus = 200
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
describe('the tester route', () => {
|
|
134
|
+
it('injects the session only when enabled and redacts reflected credentials', async () => {
|
|
135
|
+
const { response, data } = await invoke()
|
|
136
|
+
expect(response.status).toBe(200)
|
|
137
|
+
expect(calls[0].headers.authorization).toBe(`Bearer ${ACCESS_TOKEN}`)
|
|
138
|
+
expect(calls[1].headers.authorization).toBe(`Bearer ${ACCESS_TOKEN}`)
|
|
139
|
+
expect(calls[1].headers['x-antelopejs-namespace']).toBe('default')
|
|
140
|
+
expect(data.status).toBe(422)
|
|
141
|
+
expect(data.body).toBe(
|
|
142
|
+
'GET:/api/echo::[REDACTED]:[REDACTED]:[REDACTED]:[REDACTED]',
|
|
143
|
+
)
|
|
144
|
+
expect(data.headers['x-result']).toBe('fixture')
|
|
145
|
+
expect(data.headers['x-reflected']).toBe('[REDACTED]')
|
|
146
|
+
expect(data.headers['x-reflected-uri']).toBe('[REDACTED]')
|
|
147
|
+
expect(data.headers['x-reflected-json']).toBe('[REDACTED]')
|
|
148
|
+
expect(data.headers['set-cookie']).toBeUndefined()
|
|
149
|
+
expect(data.headers['x-hop']).toBeUndefined()
|
|
150
|
+
expect(response.headers.get('set-cookie')).toBeNull()
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('still checks the owner with the session when forwarding anonymously', async () => {
|
|
154
|
+
const { response } = await invoke({ useSession: false })
|
|
155
|
+
expect(response.status).toBe(200)
|
|
156
|
+
expect(calls[0].headers.authorization).toBe(`Bearer ${ACCESS_TOKEN}`)
|
|
157
|
+
expect(calls[1].headers.authorization).toBeUndefined()
|
|
158
|
+
expect(calls[1].headers['x-antelopejs-namespace']).toBeUndefined()
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
it('lets a manual authorization, even empty, override the session', async () => {
|
|
162
|
+
for (const useSession of [true, false]) {
|
|
163
|
+
for (const authorization of ['Bearer manual', '']) {
|
|
164
|
+
calls.length = 0
|
|
165
|
+
await invoke({
|
|
166
|
+
useSession,
|
|
167
|
+
headers: {
|
|
168
|
+
AuThOrIzAtIoN: authorization,
|
|
169
|
+
'X-Antelopejs-Namespace': 'custom',
|
|
170
|
+
},
|
|
171
|
+
})
|
|
172
|
+
expect(calls[1].headers.authorization).toBe(authorization)
|
|
173
|
+
expect(calls[1].headers['x-antelopejs-namespace']).toBe('custom')
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
it('refuses a missing session, a non-owner and a revoked one', async () => {
|
|
179
|
+
expect((await invoke({}, null)).response.status).toBe(401)
|
|
180
|
+
expect(calls).toHaveLength(0)
|
|
181
|
+
for (const value of [false, 'true', 1, null]) {
|
|
182
|
+
owner = value
|
|
183
|
+
expect((await invoke({ useSession: false })).response.status).toBe(403)
|
|
184
|
+
}
|
|
185
|
+
owner = true
|
|
186
|
+
ownerStatus = 401
|
|
187
|
+
expect((await invoke()).response.status).toBe(403)
|
|
188
|
+
ownerStatus = 302
|
|
189
|
+
expect((await invoke()).response.status).toBe(403)
|
|
190
|
+
expect(calls.every((call) => call.path === '/api/auth/me')).toBe(true)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it('refuses malicious paths before any backend call', async () => {
|
|
194
|
+
for (const path of [
|
|
195
|
+
'https://attacker.invalid/api',
|
|
196
|
+
'//attacker.invalid',
|
|
197
|
+
'/\\attacker',
|
|
198
|
+
'/api#fragment',
|
|
199
|
+
'/api/../escape',
|
|
200
|
+
'/api/%2e%2e/escape',
|
|
201
|
+
'/%252f%252fattacker',
|
|
202
|
+
'/api/%255cescape',
|
|
203
|
+
'/api/%00',
|
|
204
|
+
'/api/\n',
|
|
205
|
+
'/api/%zz',
|
|
206
|
+
'api/echo',
|
|
207
|
+
'/api/%23fragment',
|
|
208
|
+
])
|
|
209
|
+
expect((await invoke({ path })).response.status, path).toBe(400)
|
|
210
|
+
expect(calls).toHaveLength(0)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
it('forwards any backend path with its query, method and body', async () => {
|
|
214
|
+
for (const path of [
|
|
215
|
+
'/settings',
|
|
216
|
+
'/dms/page',
|
|
217
|
+
'/custom/action?next=https%3A%2F%2Fexample.org%2F%23section%25done',
|
|
218
|
+
]) {
|
|
219
|
+
const { response } = await invoke({
|
|
220
|
+
path,
|
|
221
|
+
method: 'PATCH',
|
|
222
|
+
body: 'asymmetric body',
|
|
223
|
+
headers: { 'Content-Type': 'text/plain' },
|
|
224
|
+
})
|
|
225
|
+
expect(response.status).toBe(200)
|
|
226
|
+
expect(calls.at(-1)).toMatchObject({
|
|
227
|
+
path,
|
|
228
|
+
method: 'PATCH',
|
|
229
|
+
body: 'asymmetric body',
|
|
230
|
+
})
|
|
231
|
+
}
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('strips transport, cookie, control and connection-nominated headers', async () => {
|
|
235
|
+
const headers = {
|
|
236
|
+
hOsT: 'attacker.invalid',
|
|
237
|
+
CoOkIe: 'secret=value',
|
|
238
|
+
CoNnEcTiOn: 'X-Nominated',
|
|
239
|
+
'X-Nominated': 'hidden',
|
|
240
|
+
'Proxy-Authorization': 'private',
|
|
241
|
+
'X-Forwarded-Host': 'attacker.invalid',
|
|
242
|
+
'X-Dms-Service-Token': 'privileged',
|
|
243
|
+
'Sec-Fetch-Site': 'same-origin',
|
|
244
|
+
'X-Http-Method-Override': 'DELETE',
|
|
245
|
+
'X-Custom': 'retained',
|
|
246
|
+
}
|
|
247
|
+
expect((await invoke({ headers, useSession: false })).response.status).toBe(
|
|
248
|
+
200,
|
|
249
|
+
)
|
|
250
|
+
const forwarded = calls.at(-1)!.headers
|
|
251
|
+
expect(forwarded.host).toBe(new URL(backend).host)
|
|
252
|
+
expect(forwarded['x-custom']).toBe('retained')
|
|
253
|
+
for (const name of Object.keys(headers)
|
|
254
|
+
.map((name) => name.toLowerCase())
|
|
255
|
+
.filter((name) => !['host', 'connection', 'x-custom'].includes(name)))
|
|
256
|
+
expect(forwarded[name], name).toBeUndefined()
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
it('returns a redirect as it is, without following it', async () => {
|
|
260
|
+
const { data } = await invoke({ path: '/api/redirect' })
|
|
261
|
+
expect(data).toMatchObject({
|
|
262
|
+
status: 302,
|
|
263
|
+
headers: { location: 'https://dms.example/unexpected-redirect' },
|
|
264
|
+
body: 'redirect response',
|
|
265
|
+
})
|
|
266
|
+
expect(data.headers['set-cookie']).toBeUndefined()
|
|
267
|
+
expect(calls).toHaveLength(2)
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
it('answers a body past the server limit with 413, and network trouble with a generic 502', async () => {
|
|
271
|
+
const tooLarge = Object.assign(new Error('Request body too large'), {
|
|
272
|
+
status: 413,
|
|
273
|
+
})
|
|
274
|
+
const { response } = await invoke({}, signedIn, () =>
|
|
275
|
+
Promise.reject(tooLarge),
|
|
276
|
+
)
|
|
277
|
+
expect(response.status).toBe(413)
|
|
278
|
+
expect(calls).toHaveLength(0)
|
|
279
|
+
for (const path of ['/api/large', '/api/network-error']) {
|
|
280
|
+
const { response, data } = await invoke({ path })
|
|
281
|
+
expect(response.status).toBe(502)
|
|
282
|
+
expect(data).toEqual({ error: 'DMS tester request failed' })
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
it('validates the method, the header values and the body before any backend call', async () => {
|
|
287
|
+
for (const input of [
|
|
288
|
+
{ method: 'CONNECT' },
|
|
289
|
+
{ useSession: 'false' },
|
|
290
|
+
{ headers: [] },
|
|
291
|
+
{ headers: { x: 1 } },
|
|
292
|
+
{ headers: { x: 'bad\r\nheader' } },
|
|
293
|
+
{ body: 'GET body' },
|
|
294
|
+
{ method: 'POST', body: {} },
|
|
295
|
+
])
|
|
296
|
+
expect((await invoke(input)).response.status).toBe(400)
|
|
297
|
+
expect(calls).toHaveLength(0)
|
|
298
|
+
})
|
|
299
|
+
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antelopejs/dms-api",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"dev": "ajs project dev -w -p playground",
|
|
22
22
|
"lint:fix": "oxlint --fix --max-warnings=1000000 && oxfmt . && pnpm --dir frontend-vue lint:fix",
|
|
23
23
|
"lint": "oxlint && oxfmt --check . && pnpm --dir frontend-vue lint && antelopejs-check-interface-ranges",
|
|
24
|
-
"typecheck": "tsc
|
|
24
|
+
"typecheck": "tsc -p tests",
|
|
25
25
|
"test": "vitest run && pnpm --dir frontend-vue test",
|
|
26
26
|
"format": "oxfmt . && pnpm --dir frontend-vue format",
|
|
27
27
|
"frontend:dev": "cd playground && pnpm frontend:dev",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"@antelopejs/interface-core": ">=0.1.1 <1.0.0",
|
|
37
37
|
"@antelopejs/interface-database": ">=0.1.8 <1.0.0",
|
|
38
38
|
"@antelopejs/interface-database-decorators": ">=0.1.7 <1.0.0",
|
|
39
|
-
"@antelopejs/interface-dms": ">=0.
|
|
39
|
+
"@antelopejs/interface-dms": ">=0.7.0 <1.0.0",
|
|
40
40
|
"node-cron": "^4.2.1",
|
|
41
41
|
"zod": "^3.24.2"
|
|
42
42
|
},
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { onBeforeUnmount, ref, shallowRef, watch, type Ref } from 'vue'
|
|
2
|
-
|
|
3
|
-
export interface ApiResource<T> {
|
|
4
|
-
data: Ref<T | null>
|
|
5
|
-
error: Ref<unknown>
|
|
6
|
-
/** First load still running: nothing to show yet. */
|
|
7
|
-
pending: Ref<boolean>
|
|
8
|
-
/** A refresh is running over data already on screen. */
|
|
9
|
-
refreshing: Ref<boolean>
|
|
10
|
-
/** When the data on screen was received. */
|
|
11
|
-
receivedAt: Ref<number | null>
|
|
12
|
-
refresh: () => Promise<void>
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* A backend payload that follows a URL. A failed refresh keeps the last data
|
|
17
|
-
* on screen and says so through `error`, instead of replacing the page with
|
|
18
|
-
* an error box; a newer request cancels the one still running.
|
|
19
|
-
*/
|
|
20
|
-
export function useApiResource<T>(
|
|
21
|
-
url: () => string | null | undefined,
|
|
22
|
-
): ApiResource<T> {
|
|
23
|
-
const { $authFetch } = useAuthFetch()
|
|
24
|
-
const data = shallowRef<T | null>(null)
|
|
25
|
-
const error = ref<unknown>(null)
|
|
26
|
-
const pending = ref(true)
|
|
27
|
-
const refreshing = ref(false)
|
|
28
|
-
const receivedAt = ref<number | null>(null)
|
|
29
|
-
let controller: AbortController | null = null
|
|
30
|
-
|
|
31
|
-
async function load(target: string | null | undefined) {
|
|
32
|
-
controller?.abort()
|
|
33
|
-
if (!target) {
|
|
34
|
-
data.value = null
|
|
35
|
-
pending.value = false
|
|
36
|
-
return
|
|
37
|
-
}
|
|
38
|
-
const current = new AbortController()
|
|
39
|
-
controller = current
|
|
40
|
-
refreshing.value = data.value !== null
|
|
41
|
-
try {
|
|
42
|
-
const response = await $authFetch<T>(target, { signal: current.signal })
|
|
43
|
-
if (controller !== current) return
|
|
44
|
-
data.value = response
|
|
45
|
-
error.value = null
|
|
46
|
-
receivedAt.value = Date.now()
|
|
47
|
-
} catch (cause) {
|
|
48
|
-
if (controller !== current) return
|
|
49
|
-
if ((cause as { name?: string })?.name !== 'AbortError')
|
|
50
|
-
error.value = cause
|
|
51
|
-
} finally {
|
|
52
|
-
if (controller === current) {
|
|
53
|
-
pending.value = false
|
|
54
|
-
refreshing.value = false
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
watch(url, (target, previous) => {
|
|
60
|
-
if (target === previous) return
|
|
61
|
-
pending.value = true
|
|
62
|
-
data.value = null
|
|
63
|
-
void load(target)
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
if (import.meta.env.SSR) {
|
|
67
|
-
pending.value = true
|
|
68
|
-
} else {
|
|
69
|
-
void load(url())
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
onBeforeUnmount(() => controller?.abort())
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
data,
|
|
76
|
-
error,
|
|
77
|
-
pending,
|
|
78
|
-
refreshing,
|
|
79
|
-
receivedAt,
|
|
80
|
-
refresh: () => load(url()),
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** The HTTP status of a failed request, when it has one. */
|
|
85
|
-
export function errorStatus(error: unknown): number | undefined {
|
|
86
|
-
const value = error as {
|
|
87
|
-
statusCode?: number
|
|
88
|
-
status?: number
|
|
89
|
-
response?: { status?: number }
|
|
90
|
-
}
|
|
91
|
-
return value?.statusCode ?? value?.status ?? value?.response?.status
|
|
92
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { readdirSync, readFileSync } from 'node:fs'
|
|
2
|
-
import { join } from 'node:path'
|
|
3
|
-
import { expect, it } from 'vitest'
|
|
4
|
-
|
|
5
|
-
// A tooltip anchors on its first child (Reka `asChild`). The DMS components
|
|
6
|
-
// load asynchronously: one still loading has no element yet, and the
|
|
7
|
-
// tooltip reading it throws "Cannot read properties of null (reading
|
|
8
|
-
// 'nodeName')", which replaced the whole Routes page on a direct load of
|
|
9
|
-
// the Tester (RT3-1). A tooltip wraps a plain element or a Nuxt UI one.
|
|
10
|
-
const COMPONENTS = join(__dirname, '../app/components')
|
|
11
|
-
|
|
12
|
-
function vueFiles(dir: string): string[] {
|
|
13
|
-
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) =>
|
|
14
|
-
entry.isDirectory()
|
|
15
|
-
? vueFiles(join(dir, entry.name))
|
|
16
|
-
: entry.name.endsWith('.vue')
|
|
17
|
-
? [join(dir, entry.name)]
|
|
18
|
-
: [],
|
|
19
|
-
)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/** The tag of the first element each `<UTooltip>` of `source` wraps. */
|
|
23
|
-
function tooltipTriggers(source: string): string[] {
|
|
24
|
-
const triggers: string[] = []
|
|
25
|
-
const open = /<UTooltip\b(?:[^>"']|"[^"]*"|'[^']*')*>/g
|
|
26
|
-
for (let match = open.exec(source); match; match = open.exec(source)) {
|
|
27
|
-
const rest = source.slice(match.index + match[0].length)
|
|
28
|
-
const child = /<(?!!--)([A-Za-z][\w-]*)/.exec(
|
|
29
|
-
rest.replace(/<!--[\s\S]*?-->/g, ''),
|
|
30
|
-
)
|
|
31
|
-
if (child) triggers.push(child[1] as string)
|
|
32
|
-
}
|
|
33
|
-
return triggers
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
it('reads the trigger each tooltip wraps', () => {
|
|
37
|
-
expect(
|
|
38
|
-
tooltipTriggers(
|
|
39
|
-
'<UTooltip :text="a > b ? \'x\' : \'y\'">\n<!-- <DmsCard> -->\n<span tabindex="0"><DmsStatusPill /></span></UTooltip>',
|
|
40
|
-
),
|
|
41
|
-
).toEqual(['span'])
|
|
42
|
-
})
|
|
43
|
-
|
|
44
|
-
it('anchors no tooltip on a DMS component, which loads asynchronously', () => {
|
|
45
|
-
const offenders = vueFiles(COMPONENTS).flatMap((file) =>
|
|
46
|
-
tooltipTriggers(readFileSync(file, 'utf8'))
|
|
47
|
-
.filter((tag) => /^Dms/.test(tag))
|
|
48
|
-
.map((tag) => `${file}: <${tag}>`),
|
|
49
|
-
)
|
|
50
|
-
expect(offenders).toEqual([])
|
|
51
|
-
})
|