User:Jack who built the house/getUrlFromInterwikiLink-tests.js

// jshint esnext: false // jshint esversion: 11 (async => {

const testCases_getUrlFromInterwikiLink = new Map([ // Local  [['test', 'de.wiktionary.org'], '/wiki/test'],  [['Abc %_def', 'de.wiktionary.org'], '/wiki/Abc_%25_def'],  [['namespace:Test', 'de.wiktionary.org'], '/wiki/namespace:Test'],  [['Namespace:Test', 'de.wiktionary.org'], '/wiki/Namespace:Test'],  [['namespace::Test', 'de.wiktionary.org'], '/wiki/namespace::Test'],  [['wmqq:Test', 'de.wiktionary.org'], '/wiki/wmqq:Test'],  [['qq:Test', 'de.wiktionary.org'], '/wiki/qq:Test'],  [['$1', 'de.wiktionary.org'], '/wiki/$1'],  [['$2', 'de.wiktionary.org'], '/wiki/$2'],  [['$3', 'de.wiktionary.org'], '/wiki/$3'],

// Simple prefixes [['s:test', 'de.wiktionary.org'], 'https://de.wikisource.org/wiki/test'], [['en:test', 'de.wiktionary.org'], 'https://en.wiktionary.org/wiki/test'],  [['wikt:test', 'de.wiktionary.org'], 'https://en.wiktionary.org/wiki/test'],  [['d:test', 'de.wiktionary.org'], 'https://www.wikidata.org/wiki/test'],  [['chapter:test', 'de.wiktionary.org'], 'https://de.wikimedia.org/wiki/test'],  [['wm2018:test', 'de.wiktionary.org'], 'https://wikimania2018.wikimedia.org/wiki/test'],  [['wmania:test', 'de.wiktionary.org'], 'https://wikimania.wikimedia.org/wiki/test'],  [['wmet:test', 'de.wiktionary.org'], 'https://ee.wikimedia.org/wiki/test'],  [['wmee:test', 'de.wiktionary.org'], 'https://ee.wikimedia.org/wiki/test'],  [['wmde:test', 'de.wiktionary.org'], 'https://wikimedia.de'],  [['lexemes:test', 'de.wiktionary.org'], 'https://www.wikidata.org/w/index.php?search=test&ns146=1'],  [['jp:test', 'de.wiktionary.org'], 'https://ja.wiktionary.org/wiki/test'],  [['zh-tw:test', 'de.wiktionary.org'], 'https://zh.wiktionary.org/wiki/test'],  [['testwiki:test', 'de.wiktionary.org'], 'https://test.wikipedia.org/wiki/test'],  [['google:test', 'de.wiktionary.org'], 'https://www.google.com/search?q=test'],  [['google:w:test', 'de.wiktionary.org'], 'https://www.google.com/search?q=w:test'],  [['s:', 'de.wiktionary.org'], 'https://de.wikisource.org/wiki/'],  [['en:', 'en.wiktionary.org'], 'https://en.wiktionary.org/wiki/'],

// Tricky prefixes: multiple, mixed with namespaces, in other case, etc. [['wikt:de:test', 'de.wiktionary.org'], 'https://de.wiktionary.org/wiki/test'],  [['en:de:test', 'de.wiktionary.org'], 'https://de.wiktionary.org/wiki/test'],  [['wikt:wikt:test', 'de.wiktionary.org'], 'https://en.wiktionary.org/wiki/test'],  [['ee:chapter:chapter:test', 'de.wiktionary.org'], 'https://ee.wikimedia.org/wiki/chapter:test'],  [['chapter:namespace:test', 'de.wiktionary.org'], 'https://de.wikimedia.org/wiki/namespace:test'],  [['s:w:wikt:test', 'de.wiktionary.org'], 'https://de.wiktionary.org/wiki/test'],  [['s:w:wikt:fr:w:namespace:w:test', 'de.wiktionary.org'], 'https://fr.wikipedia.org/wiki/namespace:w:test'],  [['s:w:wikt:fr:q:namespace:w:test', 'de.wiktionary.org'], 'https://fr.wikiquote.org/wiki/namespace:w:test'],  [['s:w:d:fr:w:namespace:w:test', 'de.wiktionary.org'], 'https://en.wikipedia.org/wiki/namespace:w:test'],  [['wmde:w:test', 'de.wiktionary.org'], 'https://wikimedia.de'],  [[':s:test', 'de.wiktionary.org'], 'https://de.wikisource.org/wiki/test'],  [[':s:en:test', 'de.wiktionary.org'], 'https://en.wikisource.org/wiki/test'],  [['w::test', 'de.wiktionary.org'], 'https://de.wikipedia.org/wiki/test'],  [[':en:w:test', 'de.wiktionary.org'], 'https://en.wikipedia.org/wiki/test'],  [['EN:test', 'de.wiktionary.org'], '/wiki/EN:test'],  [['w:', 'de.wiktionary.org'], 'https://de.wikipedia.org/wiki/'],  [['w:en:Wikt:test', 'de.wiktionary.org'], 'https://en.wiktionary.org/wiki/test'],  [['w:en:Namespace:test', 'de.wiktionary.org'], 'https://en.wikipedia.org/wiki/Namespace:test'],  [['Wikt:test', 'de.wiktionary.org'], '/wiki/Wikt:test'],  [['wikt:de:Abc %_def', 'de.wiktionary.org'], 'https://de.wiktionary.org/wiki/Abc_%25_def'],  [['test#fragment fragment', 'de.wiktionary.org'], '/wiki/test#fragment_fragment'],  [['wikt:de:test#fragment fragment', 'de.wiktionary.org'], 'https://de.wiktionary.org/wiki/test#fragment_fragment'],

// Other base domains [['fr:test', 'www.wikidata.org'], 'https://fr.wikipedia.org/wiki/test'], [['wikt:test', 'www.wikidata.org'], 'https://en.wiktionary.org/wiki/test'],  [['wikt:fr:test', 'www.wikidata.org'], 'https://fr.wiktionary.org/wiki/test'],  [['chapter:test', 'www.wikidata.org'], 'https://en.wikimedia.org/wiki/test'],  [['chapter:ee:test', 'ca.wiktionary.org'], 'https://ee.wikimedia.org/wiki/test'],  [['ca:test', 'commons.wikimedia.org'], 'https://ca.wikipedia.org/wiki/test'],  [['ee:test', 'ca.wikimedia.org'], 'https://ee.wikimedia.org/wiki/test'],  [['wikipedia:test', 'en.wikipedia.org'], '/wiki/wikipedia:test'],  [['EN:test', 'en.wikipedia.org'], 'https://en.wikipedia.org/wiki/test'],  [['commons:test', 'commons.wikimedia.org'], '/wiki/commons:test'],  [['Wikt:test', 'en.wikipedia.org'], 'https://en.wiktionary.org/wiki/test'],  [['D:Wikt:test', 'en.wikipedia.org'], 'https://www.wikidata.org/wiki/Wikt:test'],  [['google:test', 'www.wikidata.org'], 'https://www.google.com/search?q=test'],

// Invalid titles [['', 'de.wiktionary.org'], null], [['::test', 'de.wiktionary.org'], null],  [['w:::test', 'de.wiktionary.org'], null],  [['[|]', 'de.wiktionary.org'], null],  [['google:test[|]', 'de.wiktionary.org'], null], ]);

const testCases_getInterwikiPrefixForHostname = new Map([ [['de.wiktionary.org', 'en.wikipedia.org'], 'wikt:de:'],  [['de.wikipedia.org', 'en.wikipedia.org'], 'de:'],  [['en.wiktionary.org', 'en.wikipedia.org'], 'wikt:'],  [['en.wikipedia.org', 'de.wiktionary.org'], 'w:en:'],  [['en.wikipedia.org', 'de.wikipedia.org'], 'en:'],  [['de.wiktionary.org', 'de.wikipedia.org'], 'wikt:'],  [['www.wikidata.org', 'de.wiktionary.org'], 'd:'],  [['am.wikimedia.org', 'bd.wikimedia.org'], 'am:'],  // This particular one can erroneously return `wmet:` (although both work)  [['ee.wikimedia.org', 'bd.wikimedia.org'], 'ee:'],  [['am.wikimedia.org', 'commons.wikimedia.org'], 'wmam:'],  [['en.wikipedia.org', 'www.wikidata.org'], 'w:'],  [['de.wikipedia.org', 'www.wikidata.org'], 'de:'],  [['en.wiktionary.org', 'www.wikidata.org'], 'wikt:'],  [['de.wiktionary.org', 'www.wikidata.org'], 'wikt:de:'],  [['en.wikipedia.org', 'test.wikipedia.org'], 'w:'],  [['test.wikipedia.org', 'en.wikipedia.org'], 'testwiki:'],  [['en.wikipedia.org', 'en.wikipedia.org'], ],  [['de.wiktionary.org', 'de.wiktionary.org'], ],  [['www.wikidata.org', 'www.wikidata.org'], ''],  [['cz.wikipedia.org', 'en.wikipedia.org'], null],  [['en.wikipedia.org', 'cz.wikipedia.org'], 'w:'],  [['blahblahblah.wikipedia.org', 'en.wikipedia.org'], null],  [['en.blahblahblah.org', 'en.wikipedia.org'], null],  [['en.wikipedia.org', 'someunknowndomain.org'], 'w:'], ]);

async function testMethod(name, testCases) { console.log(`Testing ${name}...`); let passed = 0; let failed = 0; for (const testCase of testCases) { const result = await window[name].apply(null, testCase[0]); if (result === testCase[1]) { console.log('✅', testCase[0], testCase[1]); passed++; } else { console.log('❌', testCase[0]); console.log(`Expected:`, testCase[1]); console.log(`Actual:`, result); failed++; } }  console.log(`${passed} tests passed, ${failed} tests failed`); }

if (mw.config.get('wgServerName') !== 'en.wikipedia.org') { console.warn('Please run this on en.wikipedia.org to get correct results.'); } await testMethod('getUrlFromInterwikiLink', testCases_getUrlFromInterwikiLink); await testMethod('getInterwikiPrefixForHostname', testCases_getInterwikiPrefixForHostname);

});