-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test: automated test mechanism + first test
- Loading branch information
Showing
11 changed files
with
245 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import fs from 'node:fs/promises' | ||
import assert from 'node:assert/strict' | ||
|
||
import constants from './constants.js' | ||
|
||
export async function servePage(req, res) { | ||
if (req.url == '/tests/instantpage.js') { | ||
return serveInstantpageScript(res) | ||
} | ||
|
||
if (req.url == '/tests/postData') { | ||
return handlePostData(req, res) | ||
} | ||
|
||
const filePath = new URL(`.${req.url}`, import.meta.url) | ||
let fileContent = await fs.readFile(filePath, {encoding: 'utf8'}) | ||
fileContent = await fillConstantsAndEnvironment(req, fileContent) | ||
|
||
const headers = { | ||
'Content-Type': 'text/html', | ||
} | ||
|
||
await exertEnvironment(req, res, headers) | ||
|
||
res.writeHead(200, headers) | ||
res.write(fileContent) | ||
res.end() | ||
} | ||
|
||
async function fillConstantsAndEnvironment(req, pageContent) { | ||
const {environment} = await getConfig(req.url) | ||
const environmentString = JSON.stringify(environment) | ||
const constantsString = JSON.stringify(constants) | ||
const filledPageContent = pageContent.replace( | ||
'async (event, constants, environment) =>', | ||
`async (event, constants = ${constantsString}, environment = ${environmentString}) =>`, | ||
) | ||
return filledPageContent | ||
} | ||
|
||
async function getConfig(reqUrl) { | ||
const urlPath = new URL(`.${reqUrl}`, import.meta.url) | ||
const configPath = new URL('./config.js', urlPath) | ||
const testConfig = await import(configPath) | ||
return testConfig | ||
} | ||
|
||
async function serveInstantpageScript(res) { | ||
const path = new URL('../instantpage.js', import.meta.url) | ||
const content = await fs.readFile(path) | ||
res.writeHead(200, { | ||
'Content-Type': 'text/javascript', | ||
}) | ||
res.end(content) | ||
} | ||
|
||
async function exertEnvironment(req, res, headers) { | ||
const {environment} = await getConfig(req.url) | ||
|
||
if (environment.pageLoadTime) { | ||
await new Promise(_ => setTimeout(_, environment.pageLoadTime)) | ||
} | ||
|
||
if (environment.cacheMaxAge) { | ||
assert.ok(environment.cacheMaxAge <= 20, 'cacheMaxAge > 20 (seconds!)') | ||
headers['Cache-Control'] = `max-age=${environment.cacheMaxAge}` | ||
} | ||
} | ||
|
||
async function handlePostData(req, res) { | ||
const data = await new Promise((resolve) => { | ||
const chunks = [] | ||
req.on('data', (chunk) => { | ||
chunks.push(chunk) | ||
}) | ||
|
||
req.on('end', () => { | ||
const bodyRaw = Buffer.concat(chunks).toString() | ||
const bodyObject = JSON.parse(bodyRaw) | ||
resolve(bodyObject) | ||
}) | ||
}) | ||
|
||
const testPath = new URL(req.headers.referer).pathname | ||
const {checkExpectation} = await getConfig(testPath) | ||
const isResultAsExpected = checkExpectation(data) | ||
if (isResultAsExpected) { | ||
console.log(`✅ ok!`) | ||
} | ||
else { | ||
console.log(`❌ BAD`) | ||
console.log(data) | ||
console.log(checkExpectation.toString()) | ||
} | ||
|
||
res.writeHead(204) | ||
res.end() | ||
} |
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 |
---|---|---|
|
@@ -51,8 +51,6 @@ | |
<inspag-body></inspag-body> | ||
</form> | ||
|
||
<nav> | ||
<u>Launch experimental automated test</u> | ||
</nav> | ||
<nav></nav> | ||
|
||
<main> |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const constants = { | ||
INSTANTPAGE_HOVER_DELAY: 65, | ||
LOAD_TIME_TOLERANCE: 100, // A browser with developer tools open takes a noticeable amount of additional time to load a page | ||
} | ||
|
||
export default constants |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!doctype html> | ||
<meta charset="utf-8"> | ||
<title>.</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
|
||
<script> | ||
addEventListener('load', async (event) => { | ||
// | ||
|
||
await fetch('/tests/postData', { | ||
method: 'POST', | ||
headers: {'Content-Type': 'application/json'}, | ||
body: JSON.stringify({ | ||
// | ||
}), | ||
}) | ||
|
||
location.assign('/') | ||
}) | ||
</script> |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const title = '' | ||
|
||
export const environment = { | ||
//pageLoadTime: 0, | ||
//cacheMaxAge: false, | ||
} | ||
|
||
export const expectation = '' | ||
|
||
export function checkExpectation(data) { | ||
return false | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!doctype html> | ||
<meta charset="utf-8"> | ||
<title>.</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
|
||
<!-- dom --> | ||
|
||
<script> | ||
addEventListener('load', async (event, constants, environment) => { | ||
// | ||
}) | ||
</script> | ||
<script src="../instantpage.js" type="module"></script> |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<!doctype html> | ||
<meta charset="utf-8"> | ||
<title>.</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
|
||
<script> | ||
addEventListener('load', async (event) => { | ||
const navigationPerformanceEntry = performance.getEntriesByType('navigation')[0] | ||
|
||
await fetch('/tests/postData', { | ||
method: 'POST', | ||
headers: {'Content-Type': 'application/json'}, | ||
body: JSON.stringify({ | ||
transferSize: navigationPerformanceEntry.transferSize, | ||
}), | ||
}) | ||
|
||
location.assign('/') | ||
}) | ||
</script> |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const title = 'Hover long enough to prefetch, then click (no cache)' | ||
|
||
export const environment = { | ||
pageLoadTime: 200, | ||
cacheMaxAge: false, | ||
} | ||
|
||
export const expectation = 'Navigated page is from prefetch cache' | ||
|
||
export function checkExpectation(data) { | ||
return data.transferSize === 0 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!doctype html> | ||
<meta charset="utf-8"> | ||
<title>.</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
|
||
<a href="2.html">.</a> | ||
|
||
<script> | ||
addEventListener('load', async (event, constants, environment) => { | ||
const $a = document.querySelector('a') | ||
$a.dispatchEvent(new MouseEvent('mouseover')) | ||
await new Promise(_ => setTimeout(_, constants.INSTANTPAGE_HOVER_DELAY + environment.pageLoadTime + constants.LOAD_TIME_TOLERANCE)) | ||
$a.click() | ||
}) | ||
</script> | ||
<script src="../instantpage.js" type="module"></script> |