|
| 1 | +import Url from "../utils/Url"; |
| 2 | +import { TestRunner } from "./tester"; |
| 3 | + |
| 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", |
| 63 | + ); |
| 64 | + test.assert( |
| 65 | + !Url.areSame(folderUrl, joined), |
| 66 | + "Folder URL and joined file URL should not be considered the same", |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +export async function runUrlTests(writeOutput) { |
| 71 | + const runner = new TestRunner("URL / SAF URIs"); |
| 72 | + |
| 73 | + for (const joinCase of JOIN_CASES) { |
| 74 | + runner.test(joinCase.name, (test) => { |
| 75 | + assertJoinCase(test, joinCase); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + for (const trailingSlashCase of TRAILING_SLASH_CASES) { |
| 80 | + runner.test(trailingSlashCase.name, (test) => { |
| 81 | + test.assert( |
| 82 | + Url.areSame(trailingSlashCase.a, trailingSlashCase.b), |
| 83 | + "Folder URLs differing only by a trailing slash should be same", |
| 84 | + ); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + runner.test("Android SAF leading slash", (test) => { |
| 89 | + assertJoinCase(test, { |
| 90 | + ...JOIN_CASES[0], |
| 91 | + segment: "/index.html", |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + return await runner.run(writeOutput); |
| 96 | +} |
0 commit comments