Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
['edit', 'submit'].includes(mw.config.get('wgAction')) &&
[10, 828].includes(mw.config.get('wgNamespaceNumber')) &&
!mw.config.get('wgTitle').endsWith('/doc') &&
$(function autoTestcases() {
	let input = document.querySelector('#wpTemplateSandboxPage > input');
	if (!input || input.value) return;
	let isModule = mw.config.get('wgNamespaceNumber') === 828;
	let title = mw.config.get('wgTitle');
	let ns = mw.config.get('wgFormattedNamespaces');
	if (title.endsWith('/testcases')) {
		if (isModule) input.value = ns[829]   ':'   title;
		return;
	}
	let titles = [], last;
	title.replace(/\/sandbox$/, '').split('/').forEach(s => {
		last = last ? last   '/'   s : s;
		titles.push(ns[10]   ':'   last   '/testcases');
		if (isModule) titles.push(ns[829]   ':'   last   '/testcases');
	});
	mw.loader.using('mediawiki.api').then(() => (
		new mw.Api().post({
			action: 'query',
			titles: titles,
			formatversion: 2
		}, {
			headers: { 'Promise-Non-Write-API-Action': 1 }
		})
	)).then(response => {
		let obj;
		(((response || {}).query || {}).pages || []).forEach(page => {
			if (page.missing ||
				obj && (page.ns < obj.ns || page.title.length < obj.title.length)
			) {
				return;
			}
			obj = page;
		});
		if (obj) input.value = obj.title;
	});
});