gitea源码

utils.test.ts 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {
  2. dirname, basename, extname, isObject, stripTags, parseIssueHref,
  3. parseUrl, translateMonth, translateDay, blobToDataURI,
  4. toAbsoluteUrl, encodeURLEncodedBase64, decodeURLEncodedBase64, isImageFile, isVideoFile, parseRepoOwnerPathInfo,
  5. } from './utils.ts';
  6. test('dirname', () => {
  7. expect(dirname('/path/to/file.js')).toEqual('/path/to');
  8. expect(dirname('/path/to')).toEqual('/path');
  9. expect(dirname('file.js')).toEqual('');
  10. });
  11. test('basename', () => {
  12. expect(basename('/path/to/file.js')).toEqual('file.js');
  13. expect(basename('/path/to/file')).toEqual('file');
  14. expect(basename('file.js')).toEqual('file.js');
  15. });
  16. test('extname', () => {
  17. expect(extname('/path/to/file.js')).toEqual('.js');
  18. expect(extname('/path/')).toEqual('');
  19. expect(extname('/path')).toEqual('');
  20. expect(extname('file.js')).toEqual('.js');
  21. expect(extname('/my.path/file')).toEqual('');
  22. });
  23. test('isObject', () => {
  24. expect(isObject({})).toBeTruthy();
  25. expect(isObject([])).toBeFalsy();
  26. });
  27. test('stripTags', () => {
  28. expect(stripTags('<a>test</a>')).toEqual('test');
  29. });
  30. test('parseIssueHref', () => {
  31. expect(parseIssueHref('/owner/repo/issues/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  32. expect(parseIssueHref('/owner/repo/pulls/1?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'});
  33. expect(parseIssueHref('/owner/repo/issues/1#hash')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  34. expect(parseIssueHref('/sub/owner/repo/issues/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  35. expect(parseIssueHref('/sub/sub2/owner/repo/pulls/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'});
  36. expect(parseIssueHref('/sub/sub2/owner/repo/issues/1?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  37. expect(parseIssueHref('/sub/sub2/owner/repo/issues/1#hash')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  38. expect(parseIssueHref('https://example.com/owner/repo/issues/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  39. expect(parseIssueHref('https://example.com/owner/repo/pulls/1?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'});
  40. expect(parseIssueHref('https://example.com/owner/repo/issues/1#hash')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  41. expect(parseIssueHref('https://example.com/sub/owner/repo/issues/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  42. expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/pulls/1')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'pulls', indexString: '1'});
  43. expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1?query')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  44. expect(parseIssueHref('https://example.com/sub/sub2/owner/repo/issues/1#hash')).toEqual({ownerName: 'owner', repoName: 'repo', pathType: 'issues', indexString: '1'});
  45. expect(parseIssueHref('')).toEqual({ownerName: undefined, repoName: undefined, type: undefined, index: undefined});
  46. });
  47. test('parseRepoOwnerPathInfo', () => {
  48. expect(parseRepoOwnerPathInfo('/owner/repo/issues/new')).toEqual({ownerName: 'owner', repoName: 'repo'});
  49. expect(parseRepoOwnerPathInfo('/owner/repo/releases')).toEqual({ownerName: 'owner', repoName: 'repo'});
  50. expect(parseRepoOwnerPathInfo('/other')).toEqual({});
  51. window.config.appSubUrl = '/sub';
  52. expect(parseRepoOwnerPathInfo('/sub/owner/repo/issues/new')).toEqual({ownerName: 'owner', repoName: 'repo'});
  53. expect(parseRepoOwnerPathInfo('/sub/owner/repo/compare/feature/branch-1...fix/branch-2')).toEqual({ownerName: 'owner', repoName: 'repo'});
  54. window.config.appSubUrl = '';
  55. });
  56. test('parseUrl', () => {
  57. expect(parseUrl('').pathname).toEqual('/');
  58. expect(parseUrl('/path').pathname).toEqual('/path');
  59. expect(parseUrl('/path?search').pathname).toEqual('/path');
  60. expect(parseUrl('/path?search').search).toEqual('?search');
  61. expect(parseUrl('/path?search#hash').hash).toEqual('#hash');
  62. expect(parseUrl('https://localhost/path').pathname).toEqual('/path');
  63. expect(parseUrl('https://localhost/path?search').pathname).toEqual('/path');
  64. expect(parseUrl('https://localhost/path?search').search).toEqual('?search');
  65. expect(parseUrl('https://localhost/path?search#hash').hash).toEqual('#hash');
  66. });
  67. test('translateMonth', () => {
  68. const originalLang = document.documentElement.lang;
  69. document.documentElement.lang = 'en-US';
  70. expect(translateMonth(0)).toEqual('Jan');
  71. expect(translateMonth(4)).toEqual('May');
  72. document.documentElement.lang = 'es-ES';
  73. expect(translateMonth(5)).toEqual('jun');
  74. expect(translateMonth(6)).toEqual('jul');
  75. document.documentElement.lang = originalLang;
  76. });
  77. test('translateDay', () => {
  78. const originalLang = document.documentElement.lang;
  79. document.documentElement.lang = 'fr-FR';
  80. expect(translateDay(1)).toEqual('lun.');
  81. expect(translateDay(5)).toEqual('ven.');
  82. document.documentElement.lang = 'pl-PL';
  83. expect(translateDay(1)).toEqual('pon.');
  84. expect(translateDay(5)).toEqual('pt.');
  85. document.documentElement.lang = originalLang;
  86. });
  87. test('blobToDataURI', async () => {
  88. const blob = new Blob([JSON.stringify({test: true})], {type: 'application/json'});
  89. expect(await blobToDataURI(blob)).toEqual('data:application/json;base64,eyJ0ZXN0Ijp0cnVlfQ==');
  90. });
  91. test('toAbsoluteUrl', () => {
  92. expect(toAbsoluteUrl('//host/dir')).toEqual('http://host/dir');
  93. expect(toAbsoluteUrl('https://host/dir')).toEqual('https://host/dir');
  94. expect(toAbsoluteUrl('')).toEqual('http://localhost:3000');
  95. expect(toAbsoluteUrl('/user/repo')).toEqual('http://localhost:3000/user/repo');
  96. expect(() => toAbsoluteUrl('path')).toThrowError('unsupported');
  97. });
  98. test('encodeURLEncodedBase64, decodeURLEncodedBase64', () => {
  99. const encoder = new TextEncoder();
  100. const uint8array = encoder.encode.bind(encoder);
  101. expect(encodeURLEncodedBase64(uint8array('AA?'))).toEqual('QUE_'); // standard base64: "QUE/"
  102. expect(encodeURLEncodedBase64(uint8array('AA~'))).toEqual('QUF-'); // standard base64: "QUF+"
  103. expect(new Uint8Array(decodeURLEncodedBase64('QUE/'))).toEqual(uint8array('AA?'));
  104. expect(new Uint8Array(decodeURLEncodedBase64('QUF+'))).toEqual(uint8array('AA~'));
  105. expect(new Uint8Array(decodeURLEncodedBase64('QUE_'))).toEqual(uint8array('AA?'));
  106. expect(new Uint8Array(decodeURLEncodedBase64('QUF-'))).toEqual(uint8array('AA~'));
  107. expect(encodeURLEncodedBase64(uint8array('a'))).toEqual('YQ'); // standard base64: "YQ=="
  108. expect(new Uint8Array(decodeURLEncodedBase64('YQ'))).toEqual(uint8array('a'));
  109. expect(new Uint8Array(decodeURLEncodedBase64('YQ=='))).toEqual(uint8array('a'));
  110. });
  111. test('file detection', () => {
  112. for (const name of ['a.avif', 'a.jpg', '/a.jpeg', '.file.png', '.webp', 'file.svg']) {
  113. expect(isImageFile({name})).toBeTruthy();
  114. }
  115. for (const name of ['', 'a.jpg.x', '/path.png/x', 'webp']) {
  116. expect(isImageFile({name})).toBeFalsy();
  117. }
  118. for (const name of ['a.mpg', '/a.mpeg', '.file.mp4', '.webm', 'file.mkv']) {
  119. expect(isVideoFile({name})).toBeTruthy();
  120. }
  121. for (const name of ['', 'a.mpg.x', '/path.mp4/x', 'webm']) {
  122. expect(isVideoFile({name})).toBeFalsy();
  123. }
  124. });