|
1 | 1 | import Url from "../utils/Url"; |
2 | 2 | import { TestRunner } from "./tester"; |
3 | 3 |
|
4 | | -export async function runUrlTests(writeOutput) { |
5 | | - const runner = new TestRunner("URL / SAF URI Tests"); |
6 | | - |
7 | | - runner.test( |
8 | | - "Android external storage: join active location + index.html", |
9 | | - (test) => { |
10 | | - const folderUrl = |
11 | | - "content://com.android.externalstorage.documents/tree/primary%3ATesthtml"; |
12 | | - const activeLocation = |
13 | | - "content://com.android.externalstorage.documents/tree/primary%3ATesthtml::primary:Testhtml/Styles/"; |
14 | | - const expectedJoined = |
15 | | - "content://com.android.externalstorage.documents/tree/primary%3ATesthtml::primary:Testhtml/Styles/index.html"; |
16 | | - |
17 | | - const joined = Url.join(activeLocation, "index.html"); |
18 | | - |
19 | | - test.assertEqual( |
20 | | - joined, |
21 | | - expectedJoined, |
22 | | - "Joined URL should match expected Android SAF file URI", |
23 | | - ); |
24 | | - test.assert( |
25 | | - !Url.areSame(folderUrl, joined), |
26 | | - "Folder URL and joined file URL should not be considered same", |
27 | | - ); |
28 | | - }, |
| 4 | +const JOIN_CASES = [ |
| 5 | + { |
| 6 | + name: "Android SAF join", |
| 7 | + folderUrl: |
| 8 | + "content://com.android.externalstorage.documents/tree/primary%3ATesthtml", |
| 9 | + activeLocation: |
| 10 | + "content://com.android.externalstorage.documents/tree/primary%3ATesthtml::primary:Testhtml/Styles/", |
| 11 | + expectedJoined: |
| 12 | + "content://com.android.externalstorage.documents/tree/primary%3ATesthtml::primary:Testhtml/Styles/index.html", |
| 13 | + }, |
| 14 | + { |
| 15 | + name: "Termux SAF join", |
| 16 | + folderUrl: |
| 17 | + "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui", |
| 18 | + activeLocation: |
| 19 | + "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui::/data/data/com.termux/files/home/acode-site-ui/", |
| 20 | + expectedJoined: |
| 21 | + "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui::/data/data/com.termux/files/home/acode-site-ui/index.html", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "Acode SAF join", |
| 25 | + folderUrl: |
| 26 | + "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic", |
| 27 | + activeLocation: |
| 28 | + "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic::/data/user/0/com.foxdebug.acode/files/public/", |
| 29 | + expectedJoined: |
| 30 | + "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic::/data/user/0/com.foxdebug.acode/files/public/index.html", |
| 31 | + }, |
| 32 | +]; |
| 33 | + |
| 34 | +const TRAILING_SLASH_CASES = [ |
| 35 | + { |
| 36 | + name: "Android SAF trailing slash", |
| 37 | + a: "content://com.android.externalstorage.documents/tree/primary%3ATesthtml/", |
| 38 | + b: "content://com.android.externalstorage.documents/tree/primary%3ATesthtml", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "Termux SAF trailing slash", |
| 42 | + a: "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui/", |
| 43 | + b: "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "Acode SAF trailing slash", |
| 47 | + a: "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic/", |
| 48 | + b: "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic", |
| 49 | + }, |
| 50 | +]; |
| 51 | + |
| 52 | +function assertJoinCase( |
| 53 | + test, |
| 54 | + { folderUrl, activeLocation, expectedJoined, segment }, |
| 55 | +) { |
| 56 | + const joined = Url.join(activeLocation, segment || "index.html"); |
| 57 | + |
| 58 | + test.assert(joined !== null, "Joining the SAF URL should return a value"); |
| 59 | + test.assertEqual( |
| 60 | + joined, |
| 61 | + expectedJoined, |
| 62 | + "Joined URL should match the expected SAF file URI", |
29 | 63 | ); |
30 | | - |
31 | | - runner.test("Termux SAF: join active location + index.html", (test) => { |
32 | | - const folderUrl = |
33 | | - "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui"; |
34 | | - const activeLocation = |
35 | | - "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui::/data/data/com.termux/files/home/acode-site-ui/"; |
36 | | - const expectedJoined = |
37 | | - "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui::/data/data/com.termux/files/home/acode-site-ui/index.html"; |
38 | | - |
39 | | - const joined = Url.join(activeLocation, "index.html"); |
40 | | - |
41 | | - test.assertEqual( |
42 | | - joined, |
43 | | - expectedJoined, |
44 | | - "Joined URL should match expected Termux SAF file URI", |
45 | | - ); |
46 | | - test.assert( |
47 | | - !Url.areSame(folderUrl, joined), |
48 | | - "Folder URL and joined file URL should not be considered same", |
49 | | - ); |
50 | | - }); |
51 | | - |
52 | | - runner.test( |
53 | | - "Acode terminal SAF: join active location + index.html", |
54 | | - (test) => { |
55 | | - const folderUrl = |
56 | | - "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic"; |
57 | | - const activeLocation = |
58 | | - "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic::/data/user/0/com.foxdebug.acode/files/public/"; |
59 | | - const expectedJoined = |
60 | | - "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic::/data/user/0/com.foxdebug.acode/files/public/index.html"; |
61 | | - |
62 | | - const joined = Url.join(activeLocation, "index.html"); |
63 | | - |
64 | | - test.assertEqual( |
65 | | - joined, |
66 | | - expectedJoined, |
67 | | - "Joined URL should match expected Acode Terminal SAF file URI", |
68 | | - ); |
69 | | - test.assert( |
70 | | - !Url.areSame(folderUrl, joined), |
71 | | - "Folder URL and joined file URL should not be considered same", |
72 | | - ); |
73 | | - }, |
74 | | - ); |
75 | | - |
76 | | - runner.test( |
77 | | - "Android SAF folder URL should match with trailing slash", |
78 | | - (test) => { |
79 | | - const a = |
80 | | - "content://com.android.externalstorage.documents/tree/primary%3ATesthtml/"; |
81 | | - const b = |
82 | | - "content://com.android.externalstorage.documents/tree/primary%3ATesthtml"; |
83 | | - |
84 | | - test.assert( |
85 | | - Url.areSame(a, b), |
86 | | - "Android folder URLs differing only by trailing slash should be same", |
87 | | - ); |
88 | | - }, |
| 64 | + test.assert( |
| 65 | + !Url.areSame(folderUrl, joined), |
| 66 | + "Folder URL and joined file URL should not be considered the same", |
89 | 67 | ); |
| 68 | +} |
90 | 69 |
|
91 | | - runner.test( |
92 | | - "Termux SAF folder URL should match with trailing slash", |
93 | | - (test) => { |
94 | | - const a = |
95 | | - "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui/"; |
96 | | - const b = |
97 | | - "content://com.termux.documents/tree/%2Fdata%2Fdata%2Fcom.termux%2Ffiles%2Fhome%2Facode-site-ui"; |
98 | | - |
99 | | - test.assert( |
100 | | - Url.areSame(a, b), |
101 | | - "Termux folder URLs differing only by trailing slash should be same", |
102 | | - ); |
103 | | - }, |
104 | | - ); |
| 70 | +export async function runUrlTests(writeOutput) { |
| 71 | + const runner = new TestRunner("URL / SAF URIs"); |
105 | 72 |
|
106 | | - runner.test( |
107 | | - "Acode terminal SAF folder URL should match with trailing slash", |
108 | | - (test) => { |
109 | | - const a = |
110 | | - "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic/"; |
111 | | - const b = |
112 | | - "content://com.foxdebug.acode.documents/tree/%2Fdata%2Fuser%2F0%2Fcom.foxdebug.acode%2Ffiles%2Fpublic"; |
| 73 | + for (const joinCase of JOIN_CASES) { |
| 74 | + runner.test(joinCase.name, (test) => { |
| 75 | + assertJoinCase(test, joinCase); |
| 76 | + }); |
| 77 | + } |
113 | 78 |
|
| 79 | + for (const trailingSlashCase of TRAILING_SLASH_CASES) { |
| 80 | + runner.test(trailingSlashCase.name, (test) => { |
114 | 81 | test.assert( |
115 | | - Url.areSame(a, b), |
116 | | - "Acode terminal folder URLs differing only by trailing slash should be same", |
| 82 | + Url.areSame(trailingSlashCase.a, trailingSlashCase.b), |
| 83 | + "Folder URLs differing only by a trailing slash should be same", |
117 | 84 | ); |
118 | | - }, |
119 | | - ); |
120 | | - |
121 | | - runner.test("join should handle leading slash segment", (test) => { |
122 | | - const activeLocation = |
123 | | - "content://com.android.externalstorage.documents/tree/primary%3ATesthtml::primary:Testhtml/Styles/"; |
124 | | - const expectedJoined = |
125 | | - "content://com.android.externalstorage.documents/tree/primary%3ATesthtml::primary:Testhtml/Styles/index.html"; |
126 | | - |
127 | | - const joined = Url.join(activeLocation, "/index.html"); |
128 | | - test.assertEqual( |
129 | | - joined, |
130 | | - expectedJoined, |
131 | | - "Leading slash in joined segment should be normalized", |
132 | | - ); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + runner.test("Android SAF leading slash", (test) => { |
| 89 | + assertJoinCase(test, { |
| 90 | + ...JOIN_CASES[0], |
| 91 | + segment: "/index.html", |
| 92 | + }); |
133 | 93 | }); |
134 | 94 |
|
135 | 95 | return await runner.run(writeOutput); |
|
0 commit comments