@geolonia/embed 2.1.3 → 2.1.5

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,97 +0,0 @@
1
- const assert = require('assert')
2
- const puppeteer = require('puppeteer')
3
- const fs = require('fs').promises
4
- const path = require('path')
5
- const http = require('http')
6
- const { pngDiff, niceDiff } = require('./util')
7
-
8
- const sleep = msec => new Promise(resolve => setTimeout(resolve, msec))
9
-
10
- describe('Tests for Maps.', () => {
11
- let browser
12
- let page
13
- let embedServer
14
- const capturedErrors = []
15
- const reports = []
16
-
17
- before(async () => {
18
- const embed = (
19
- await fs.readFile(path.resolve(__dirname, '..', 'dist', 'embed.js'))
20
- ).toString()
21
- embedServer = http.createServer((request, response) => {
22
- response.writeHead(200, { 'Content-Type': 'text/javascirpt' })
23
- response.write(embed)
24
- response.end()
25
- })
26
- embedServer.listen(8080)
27
-
28
- // prepare pupeteer
29
- browser = await puppeteer.launch({
30
- headless: true,
31
- args:
32
- process.env.NO_SANDBOX === 'true'
33
- ? ['--no-sandbox', '--disable-setuid-sandbox'] // for Docker env
34
- : [],
35
- })
36
- page = await browser.newPage()
37
-
38
- // navigation
39
- const template = await (
40
- await fs.readFile(path.resolve(__dirname, 'assets', 'map.html.template'))
41
- ).toString()
42
- const content = template
43
- .replace(/%ORIGIN%/g, 'http://127.0.0.1:8080')
44
- .replace(/%API_KEY%/g, process.env.GEOLONIA_API_KEY || 'YOUR-API-KEY')
45
- await page.goto('https://geoloniamaps.github.io/basic/')
46
- page.on('pageerror', error => capturedErrors.push(error))
47
- await page.setContent(content)
48
- await sleep(15000)
49
- })
50
-
51
- after(async () => {
52
- reports.forEach(report => process.stdout.write(`[report] ${report}\n`))
53
- await browser.close()
54
- embedServer.close()
55
- })
56
-
57
- it('should not emit JS errors', () => {
58
- assert.strictEqual(capturedErrors.length, 0)
59
- })
60
-
61
- it('should build DOM as expected', async () => {
62
- const map = await page.$('#map div:first-child')
63
- const className = await (await map.getProperty('className')).jsonValue()
64
- console.log(className)
65
- assert.strictEqual(className.includes('maplibregl-canvas-container'), true)
66
- })
67
-
68
- it('should match the snapshot at >95.00%', async () => {
69
- const basePath = path.resolve('__dirname', '..', 'snapshots')
70
- const snapshotPath = path.resolve(basePath, 'map.png.snapshot')
71
- const tmpSnapshotPath = path.resolve(basePath, 'map.png')
72
- const diffSnapshotPath = path.resolve(basePath, 'diff.png')
73
- const [nextImage, prevImage] = await Promise.all([
74
- page.screenshot(),
75
- fs.readFile(snapshotPath).catch(() => null),
76
- ])
77
- await fs.writeFile(tmpSnapshotPath, nextImage)
78
-
79
- if (process.env.UPDATE_SNAPSHOT === 'true' || !prevImage) {
80
- // Store snapshot
81
- reports.push('A snapshot has been updated.')
82
- await fs.writeFile(snapshotPath, nextImage)
83
- } else {
84
- const [matchRate, diffImage] = await Promise.all([
85
- pngDiff(prevImage, nextImage),
86
- niceDiff(prevImage, nextImage),
87
- ])
88
- await fs.writeFile(diffSnapshotPath, diffImage)
89
-
90
- const matchRateLabel =
91
- ((Math.round(10000 * matchRate) + 0.1) / 100).toString().slice(0, 5) +
92
- '%'
93
- reports.push(`Snapshot matching rate: ${matchRateLabel}`)
94
- assert.strictEqual(matchRate > 0.95, true)
95
- }
96
- })
97
- })
package/test/util.js DELETED
@@ -1,96 +0,0 @@
1
- const { PNG } = require('pngjs')
2
-
3
- const pngDiff = (prevImage, nextImage) => {
4
- return new Promise(resolve => {
5
- const prev = PNG.sync.read(prevImage)
6
- const next = PNG.sync.read(nextImage)
7
- let totalDelta = 0
8
- if (prev.data.length !== next.data.length) {
9
- throw new Error(
10
- `image size mismatch: ${prev.width}x${prev.height} vs. ${next.width}x${next.height}`,
11
- )
12
- }
13
- const byteCount = prev.data.length
14
- const pixelCount = byteCount / 4
15
- for (let index = 0; index < byteCount; index += 4) {
16
- const prevA = prev.data[index + 3] / 255
17
- const nextA = next.data[index + 3] / 255
18
- let delta = 0
19
- delta += Math.abs(
20
- (prev.data[index] * prevA - next.data[index] * nextA) / 255,
21
- )
22
- delta += Math.abs(
23
- (prev.data[index + 1] * prevA - next.data[index + 1] * nextA) / 255,
24
- )
25
- delta += Math.abs(
26
- (prev.data[index + 2] * prevA - next.data[index + 2] * nextA) / 255,
27
- )
28
- totalDelta += delta / 3
29
- }
30
- const matchRate = (pixelCount - totalDelta) / pixelCount
31
- resolve(matchRate)
32
- })
33
- }
34
-
35
- const niceDiff = (img1, img2) => {
36
- return new Promise(resolve => {
37
- const png1 = PNG.sync.read(img1)
38
- const png2 = PNG.sync.read(img2)
39
- if (png1.data.length !== png2.data.length) {
40
- throw new Error(
41
- `image size mismatch: ${png1.width}x${png1.height} vs. ${png2.width}x${png2.height}`,
42
- )
43
- }
44
- const byteCount = png1.data.length
45
- const diff = {
46
- data: [],
47
- width: png1.width,
48
- height: png1.height,
49
- }
50
- for (let index = 0; index < byteCount; index += 4) {
51
- if (
52
- png1.data[index] !== png2.data[index] ||
53
- png1.data[index + 1] !== png2.data[index + 1] ||
54
- png1.data[index + 2] !== png2.data[index + 2]
55
- ) {
56
- // a bright marker
57
- diff.data.push(...[255, 120, 255, 255])
58
- } else {
59
- const darker = [png1.data[index], png1.data[index + 1], png1.data[index + 2]].map(p => Math.floor(p / 5))
60
- diff.data.push(...darker, 255)
61
- }
62
- }
63
- resolve(PNG.sync.write(diff))
64
- })
65
- }
66
-
67
- // eslint-disable-next-line no-unused-vars
68
- const _test = async () => {
69
- const alfa = 255
70
- const img1 = PNG.sync.write({
71
- width: 2,
72
- height: 2,
73
- data: [
74
- [0, 0, 0],
75
- [0, 0, 0],
76
- [0, 0, 0],
77
- [0, 0, 0],
78
- ].flatMap(rgb => [...rgb, alfa]),
79
- })
80
- const img2 = PNG.sync.write({
81
- width: 2,
82
- height: 2,
83
- data: [
84
- [255, 255, 255],
85
- [255, 255, 255],
86
- [255, 255, 255],
87
- [255, 255, 255],
88
- ].flatMap(rgb => [...rgb, alfa]),
89
- })
90
- const matchRate = await pngDiff(img1, img2)
91
- if (matchRate > 0) {
92
- throw new Error(`invalid match rate: ${matchRate}`)
93
- }
94
- }
95
-
96
- module.exports = { pngDiff, niceDiff }