Skip to content

Commit

Permalink
feat(web): re-implement timeout error test
Browse files Browse the repository at this point in the history
The previous approach with overriding `onload` no longer works if we use
indirect eval. However, since we now need to use a server anyways instead
of directly loading a file (because of Core), we can add a timeout to
the test server and implement this test in a cleaner way.
  • Loading branch information
ermshiperete committed Oct 1, 2024
1 parent 387a697 commit c05cf0e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 36 deletions.
10 changes: 10 additions & 0 deletions web/src/test/manual/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Manual tests

To run the the manual tests, start the test web server with:

```bash
cd $KEYMAN_ROOT
web/build.sh start
```

Then open <http://localhost:3000> in your browser.
26 changes: 13 additions & 13 deletions web/src/test/manual/web/keyboard-errors/errorhdr.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
// JavaScript Document samplehdr.js: Keyboard management for KeymanWeb demonstration pages

/*
/*
This script is designed to test KeymanWeb error message handling.
*/

function loadKeyboards()
{
function loadKeyboards()
{
var kmw=keyman;

// We start by adding a keyboard correctly. It's best to include a 'control' in our experiment.
kmw.addKeyboards({id:'us',name:'English',languages:{id:'en',name:'English'},
filename:'../us-1.0.js'});

// Insert a keyboard that cannot be found.
kmw.addKeyboards({id:'lao_2008_basic',name:'wrong-filename',
languages:{
id:'lo',name:'debugging',region:'Asia',
font:{family:'LaoWeb',source:['../font/saysettha_web.ttf','../font/saysettha_web.woff','../font/saysettha_web.eot']}
},
filename:'./missing_file.js' // Intentional error - the file doesn't exist, so the <script> tag will raise an error event.
});
// Insert a keyboard that will generate a timing error.
});

// Insert a keyboard that will generate a timing error.
kmw.addKeyboards({id:'unparsable',name:'non-parsable',
languages:{
id:'lo',name:'debugging',region:'Asia',
font:{family:'LaoWeb',source:['../font/saysettha_web.ttf','../font/saysettha_web.woff','../font/saysettha_web.eot']}
},
filename:'./unparsable.js' // Intentional error - the file has no parsable keyboard, so while the <script> tag will load,
// registration will fail.
});
});

// Insert a keyboard that will generate a timing error.
// Insert a keyboard that will generate a timing error. `timeout.js` doesn't
// exist, but the test server (web/src/tools/testing/test-server/index.cjs)
// has special handling for that URL and times out after 10 seconds.
kmw.addKeyboards({id:'timeout',name:'timeout',
languages:{
id:'lo',name:'debugging',region:'Asia',
font:{family:'LaoWeb',source:['../font/saysettha_web.ttf','../font/saysettha_web.woff','../font/saysettha_web.eot']}
},
filename:'./timeout.js' // Intentional (simulated) error - the file never loads, simulating a server timeout.
});
});
}
23 changes: 23 additions & 0 deletions web/src/test/manual/web/keyboard-errors/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,32 @@ <h3>Type in your language in this text area:</h3>
<h3>or in this input field:</h3>
<input class='test' value='' placeholder='or here'/>

<h2>Expected error messages:</h2>
<ul>
<li><i>wrong-filename</i> keyboard: "Unable to download wrong-filename keyboard for debugging"</li>
<li><i>non-parsable</i> keyboard: "Error registering the non-parsable keyboard for debugging; keyboard script at ./unparsable.js may contain an error."</li>
<li><i>timeout</i> keyboard: "Sorry, the timeout keyboard for debugging is not currently available."</li>
</ul>

<h3><a href="../index.html">Return to testing home page</a></h3>
</div>

<script>
if (!window.location.href.startsWith('http://localhost:3000')) {
const body = document.getElementsByTagName('body')[0];
body.innerHTML = `<h1>KeymanWeb Sample Page - Error Testing page</h1>
<h2>Unable to load this test page!</h2>
<p>This page has to be loaded through the local test server.</p>
<p>Do the following:</p>
<ol>
<li>Open a terminal and navigate to the Keyman source root directory</li>
<li>Start the test server with <code>web/build.sh start</code></li>
<li>Open <a href="http://localhost:3000/src/test/manual/web/keyboard-errors/index.html">http://localhost:3000/src/test/manual/web/keyboard-errors/index.html</a> in a browser</li>
</ol>
`;
}
</script>

</body>

<!--
Expand Down
21 changes: 0 additions & 21 deletions web/src/test/manual/web/keyboard-errors/timeout.js

This file was deleted.

17 changes: 15 additions & 2 deletions web/src/tools/testing/test-server/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,21 @@ const port = 3000

app.use(express.static(path.join(__dirname, '../../../../')))

// for testing timeout error in web/src/test/manual/web/keyboard-errors
const router = express.Router()
router.get('/src/test/manual/web/keyboard-errors/timeout.js', async (req, res, next) => {
console.log('timeout.js requested')
return new Promise(() => {
setTimeout(() => {
res.set('Content-Type', 'application/json');
res.status(200);

next();
}, 10500); // > ContextManagerBase.TIMEOUT_THRESHOLD (10 seconds)
});
})
app.use(router)

app.listen(port, () => {
console.log(`Keyman test app listening on port ${port}`)
})


0 comments on commit c05cf0e

Please sign in to comment.