-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(config-cli): Provide a cli for creating temporary server and dump screenshots. #2236
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
bceabb8
Create a serve and do screenshot on it.
Wentao-Kuang de5fdc0
DefaultTestTiles as const
Wentao-Kuang 116e900
Correct the references
Wentao-Kuang 8c36709
Fix linting
Wentao-Kuang dbbdc7c
Get random port for temporary server, and remove pr tag.
Wentao-Kuang aaf4ac5
Merge to master
Wentao-Kuang e7abe26
Update basemaps config and server dependencies
Wentao-Kuang 82b67f1
Combine screenshot and screenshot-server as a single command.
Wentao-Kuang b2f5e52
waiting the server finished before lanch chromium.
Wentao-Kuang 31a0f37
Minor fix
Wentao-Kuang 735ab70
Make an aync function to start sever.
Wentao-Kuang 0e40185
Add promise for the server lisent
Wentao-Kuang b05c963
export ConfigBundled from config package.
Wentao-Kuang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,13 @@ import { mkdir } from 'fs/promises'; | |
import { Browser, chromium } from 'playwright'; | ||
import { CommandLineAction, CommandLineStringParameter } from '@rushstack/ts-command-line'; | ||
import { z } from 'zod'; | ||
import getPort, { portNumbers } from 'get-port'; | ||
import { createServer } from '@basemaps/server'; | ||
import { FastifyInstance } from 'fastify/types/instance'; | ||
import { ConfigBundled, ConfigProviderMemory } from '@basemaps/config'; | ||
|
||
export const DefaultTestTiles = './test-tiles/default.test.tiles.json'; | ||
export const DefaultHost = 'basemaps.linz.govt.nz'; | ||
|
||
enum TileMatrixIdentifier { | ||
Nztm2000Quad = 'NZTM2000Quad', | ||
|
@@ -26,8 +33,8 @@ const zTileTest = z.object({ | |
export type TileTestSchema = z.infer<typeof zTileTest>; | ||
|
||
export class CommandScreenShot extends CommandLineAction { | ||
private config: CommandLineStringParameter; | ||
private host: CommandLineStringParameter; | ||
private tag: CommandLineStringParameter; | ||
private tiles: CommandLineStringParameter; | ||
|
||
public constructor() { | ||
|
@@ -39,104 +46,100 @@ export class CommandScreenShot extends CommandLineAction { | |
} | ||
|
||
protected onDefineParameters(): void { | ||
this.config = this.defineStringParameter({ | ||
argumentName: 'CONFIG', | ||
parameterLongName: '--config', | ||
description: 'Path of config bundle file, could be both local file or s3.', | ||
}); | ||
|
||
this.host = this.defineStringParameter({ | ||
argumentName: 'HOST', | ||
parameterLongName: '--host', | ||
description: 'Host to use', | ||
defaultValue: 'basemaps.linz.govt.nz', | ||
}); | ||
|
||
this.tag = this.defineStringParameter({ | ||
argumentName: 'TAG', | ||
parameterShortName: '-t', | ||
parameterLongName: '--tag', | ||
description: 'PR tag(PR-number) or "production"', | ||
defaultValue: 'production', | ||
defaultValue: DefaultHost, | ||
}); | ||
|
||
this.tiles = this.defineStringParameter({ | ||
argumentName: 'TILES', | ||
parameterLongName: '--tiles', | ||
description: 'JSON file path for the test tiles', | ||
defaultValue: './test-tiles/default.test.tiles.json', | ||
defaultValue: DefaultTestTiles, | ||
}); | ||
} | ||
|
||
async onExecute(): Promise<void> { | ||
const logger = LogConfig.get(); | ||
const config = this.config.value; | ||
let host = this.host.value ?? DefaultHost; | ||
const tiles = this.tiles.value ?? DefaultTestTiles; | ||
|
||
let BasemapsServer: FastifyInstance | undefined = undefined; | ||
if (config != null) { | ||
const port = await getPort({ port: portNumbers(10000, 11000) }); | ||
host = `http://localhost:${port}`; | ||
BasemapsServer = await startServer(host, port, config, logger); | ||
} | ||
|
||
logger.info('Page:Launch'); | ||
const chrome = await chromium.launch(); | ||
|
||
try { | ||
await this.takeScreenshots(chrome, logger); | ||
await takeScreenshots(host, tiles, chrome, logger); | ||
} finally { | ||
await chrome.close(); | ||
if (BasemapsServer != null) await BasemapsServer.close(); | ||
} | ||
} | ||
} | ||
|
||
async takeScreenshots(chrome: Browser, logger: LogType): Promise<void> { | ||
const host = this.host.value ?? this.host.defaultValue; | ||
const tag = this.tag.value ?? this.tag.defaultValue; | ||
const tiles = this.tiles.value ?? this.tiles.defaultValue; | ||
if (host == null || tag == null || tiles == null) | ||
throw new Error('Missing essential parameter to run the process.'); | ||
|
||
const TestTiles = await fsa.readJson<TileTestSchema[]>(tiles); | ||
for (const test of TestTiles) { | ||
const page = await chrome.newPage(); | ||
|
||
const tileSetId = await this.getTileSetId(test.tileSet, tag); | ||
const styleId = await this.getStyleId(test.style, tag); | ||
export async function takeScreenshots(host: string, tiles: string, chrome: Browser, logger: LogType): Promise<void> { | ||
const TestTiles = await fsa.readJson<TileTestSchema[]>(tiles); | ||
for (const test of TestTiles) { | ||
const page = await chrome.newPage(); | ||
|
||
const searchParam = new URLSearchParams(); | ||
searchParam.set('p', test.tileMatrix); | ||
searchParam.set('i', tileSetId); | ||
if (styleId) searchParam.set('s', styleId); | ||
const searchParam = new URLSearchParams(); | ||
searchParam.set('p', test.tileMatrix); | ||
searchParam.set('i', test.tileSet); | ||
if (test.style) searchParam.set('s', test.style); | ||
|
||
const loc = `@${test.location.lat},${test.location.lng},z${test.location.z}`; | ||
const fileName = '.artifacts/visual-snapshots/' + host + '_' + test.name + '.png'; | ||
const loc = `@${test.location.lat},${test.location.lng},z${test.location.z}`; | ||
const fileName = '.artifacts/visual-snapshots/' + host + '_' + test.name + '.png'; | ||
|
||
await mkdir(`.artifacts/visual-snapshots/`, { recursive: true }); | ||
await mkdir(`.artifacts/visual-snapshots/`, { recursive: true }); | ||
|
||
const url = `https://${host}/?${searchParam.toString()}&debug=true&debug.screenshot=true#${loc}`; | ||
let url = `${host}/?${searchParam.toString()}&debug=true&debug.screenshot=true#${loc}`; | ||
if (!url.startsWith('http')) url = `https"//${url}`; | ||
|
||
logger.info({ url, expected: fileName }, 'Page:Load'); | ||
logger.info({ url, expected: fileName }, 'Page:Load'); | ||
|
||
await page.goto(url); | ||
await page.goto(url); | ||
|
||
try { | ||
await page.waitForSelector('div#map-loaded', { state: 'attached' }); | ||
await page.waitForTimeout(1000); | ||
await page.waitForLoadState('networkidle'); | ||
await page.screenshot({ path: fileName }); | ||
} catch (e) { | ||
await page.screenshot({ path: fileName }); | ||
throw e; | ||
} | ||
logger.info({ url, expected: fileName }, 'Page:Load:Done'); | ||
await page.close(); | ||
try { | ||
await page.waitForSelector('div#map-loaded', { state: 'attached' }); | ||
await page.waitForTimeout(1000); | ||
await page.waitForLoadState('networkidle'); | ||
await page.screenshot({ path: fileName }); | ||
} catch (e) { | ||
await page.screenshot({ path: fileName }); | ||
throw e; | ||
} | ||
logger.info({ url, expected: fileName }, 'Page:Load:Done'); | ||
await page.close(); | ||
} | ||
} | ||
|
||
async getTileSetId(tileSetId: string, tag: string): Promise<string> { | ||
if (tag === 'production') return tileSetId; | ||
|
||
const tileSetTagId = `${tileSetId}@${tag}`; | ||
const dbId = Config.TileSet.id(tileSetTagId); | ||
const tileSet = await Config.TileSet.get(dbId); | ||
|
||
if (tileSet) return tileSetTagId; | ||
return tileSetId; | ||
} | ||
|
||
async getStyleId(styleId: string | undefined, tag: string): Promise<string> { | ||
if (styleId == null) return ''; | ||
if (tag === 'production') return styleId ?? ''; | ||
|
||
const styleIdTagId = `${styleId}@${tag}`; | ||
const dbId = Config.Style.id(styleIdTagId); | ||
const style = await Config.Style.get(dbId); | ||
if (style) return styleIdTagId; | ||
return styleId; | ||
} | ||
async function startServer(host: string, port: number, config: string, logger: LogType): Promise<FastifyInstance> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is host passed in just to log it? could generate host inside the function or log outside the function |
||
// Bundle Config | ||
const configJson = await fsa.readJson<ConfigBundled>(config); | ||
const mem = ConfigProviderMemory.fromJson(configJson); | ||
Config.setConfigProvider(mem); | ||
|
||
// Start server | ||
const server = createServer(logger); | ||
await new Promise<void>((resolve) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
server.listen(port, '0.0.0.0', () => { | ||
logger.info({ url: host }, 'ServerStarted'); | ||
resolve(); | ||
}), | ||
); | ||
return server; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just use any port?