forks/sorcery
git clone https://git.gae.moe/forks/sorcery
7e9177b
typst_support
1import { assertEquals } from "jsr:@std/assert@1" ; 2import { canonical , pageAt , pageView , viewAt , withinRef } from "./route.ts" ; 3 4const tip = "a" . repeat ( 40 ); 5const old = "b" . repeat ( 40 ); 6 7Deno . test ( "accepts SHA-1 and SHA-256 object ids" , () => { 8const page = pageAt ( "/u/r/" , "main" , tip ); 9const sha256 = "c" . repeat ( 64 ); 10assertEquals ( viewAt ( page , `#commit/ ${ sha256 } ` ), { kind :"commit" , oid :sha256 , path :[] }); 11assertEquals ( viewAt ( page , `#commit/ ${ "c" . repeat ( 48 )} ` ), pageView ( page )); 12}); 13 14Deno . test ( "pathname parses to a page" , () => { 15assertEquals ( pageAt ( "/u/r/" , "main" , tip ), { base :"/u/r" , rootBase :"/u/r/" , refBase :"/u/r/ref/main/" , tip, kind :"tree" , path :[] }); 16assertEquals ( pageAt ( "/u/r/refs" , "main" , tip ). rootBase , "/u/r/" ); 17assertEquals ( pageAt ( "/u/r/refs" , "main" , tip ). tip , null ); 18assertEquals ( pageAt ( "/u/r/ref/main/" , "main" , tip ). refBase , "/u/r/ref/main/" ); 19assertEquals ( pageAt ( "/u/r/ref/feat/x/tree/src/a%20b/" , "feat/x" , tip ), { 20base :"/u/r" , 21rootBase :"/u/r/ref/feat/x/" , 22refBase :"/u/r/ref/feat/x/" , 23 tip, 24kind :"tree" , 25path :[ "src" , "a b" ], 26}); 27assertEquals ( pageAt ( "/u/r/ref/main/blob/src/lib.rs" , "main" , tip ). kind , "blob" ); 28}); 29 30Deno . test ( "hash is a delta on the page" , () => { 31const page = pageAt ( "/u/r/ref/main/blob/src/lib.rs" , "main" , tip ); 32const own = { kind :"blob" , oid :tip , path :[ "src" , "lib.rs" ] }; 33assertEquals ( viewAt ( page , "" ), own ); 34assertEquals ( viewAt ( page , "#L12" ), own ); 35assertEquals ( viewAt ( pageAt ( "/u/r/ref/main/tree/src/" , "main" , tip ), "" ), { kind :"tree" , oid :tip , path :[ "src" ] }); 36assertEquals ( viewAt ( pageAt ( "/u/r/" , "main" , null ), "" ), null ); 37assertEquals ( viewAt ( page , `# ${ old } ` ), { kind :"blob" , oid :old , path :[ "src" , "lib.rs" ] }); 38assertEquals ( viewAt ( page , "#history" ), { kind :"history" , oid :tip , path :[ "src" , "lib.rs" ] }); 39assertEquals ( viewAt ( page , `#history/ ${ old } ` ), { kind :"history" , oid :old , path :[ "src" , "lib.rs" ] }); 40assertEquals ( viewAt ( page , `#commit/ ${ old } ` ), { kind :"commit" , oid :old , path :[] }); 41assertEquals ( viewAt ( page , "#language/rust" ), { kind :"language" , oid :tip , path :[ "rust" ] }); 42assertEquals ( viewAt ( page , `#tree/ ${ old } /a%20b/c` ), { kind :"tree" , oid :old , path :[ "a b" , "c" ] }); 43assertEquals ( viewAt ( page , `#tree/ ${ old } ` ), { kind :"tree" , oid :old , path :[] }); 44assertEquals ( viewAt ( pageAt ( "/u/r/" , "main" , null ), "#history" ), null ); 45}); 46 47Deno . test ( "the tip speaks for the ref's pages only" , () => { 48const root = pageAt ( "/u/r/" , "main" , tip ); 49assertEquals ( withinRef ( root , "/u/r/" ), true ); 50assertEquals ( withinRef ( root , "/u/r/ref/main/tree/src/" ), true ); 51assertEquals ( withinRef ( root , "/u/r/ref/other/" ), false ); 52assertEquals ( withinRef ( root , "/u/r/refs" ), false ); 53// a ref page cannot know whether the root page is its own 54assertEquals ( withinRef ( pageAt ( "/u/r/ref/main/" , "main" , tip ), "/u/r/" ), false ); 55assertEquals ( withinRef ( pageAt ( "/u/r/" , "main" , null ), "/u/r/" ), false ); 56}); 57 58Deno . test ( "canonical picks the shortest valid form" , () => { 59const page = pageAt ( "/u/r/ref/main/tree/src/" , "main" , tip ); 60const lib = [ "src" , "lib.rs" ]; 61assertEquals ( canonical ( page , { kind :"blob" , oid :tip , path :lib }, "blob" ), "/u/r/ref/main/blob/src/lib.rs" ); 62assertEquals ( canonical ( page , { kind :"blob" , oid :old , path :lib }, "blob" ), `/u/r/ref/main/blob/src/lib.rs# ${ old } ` ); 63assertEquals ( canonical ( page , { kind :"blob" , oid :old , path :lib }, null ), `#blob/ ${ old } /src/lib.rs` ); 64assertEquals ( canonical ( page , { kind :"blob" , oid :old , path :lib }, "tree" ), `#blob/ ${ old } /src/lib.rs` ); 65assertEquals ( canonical ( page , { kind :"tree" , oid :old , path :[] }, "tree" ), `/u/r/ref/main/# ${ old } ` ); 66assertEquals ( canonical ( page , { kind :"history" , oid :tip , path :lib }, "blob" ), "/u/r/ref/main/blob/src/lib.rs#history" ); 67assertEquals ( canonical ( page , { kind :"history" , oid :old , path :[] }, "tree" ), `/u/r/ref/main/#history/ ${ old } ` ); 68assertEquals ( canonical ( page , { kind :"history" , oid :old , path :lib }, null ), `#history/ ${ old } /src/lib.rs` ); 69assertEquals ( canonical ( page , { kind :"commit" , oid :tip , path :[] }, null ), `#commit/ ${ tip } ` ); 70assertEquals ( canonical ( page , { kind :"language" , oid :tip , path :[ "rust" ] }, null ), "/u/r/ref/main/#language/rust" ); 71assertEquals ( canonical ( page , { kind :"language" , oid :old , path :[ "rust" ] }, null ), `/u/r/ref/main/#language/rust/ ${ old } ` ); 72}); 73 74Deno . test ( "canonical round-trips through viewAt" , () => { 75const page = pageAt ( "/u/r/" , "main" , tip ); 76for ( const view of [ 77{ kind :"blob" as const , oid :old , path :[ "a b" , "c.rs" ] }, 78{ kind :"history" as const , oid :old , path :[ "a b" ] }, 79{ kind :"tree" as const , oid :old , path :[ "a b" ] }, 80]) { 81const at = view . kind === "blob" ?"blob" :"tree" ; 82const url = new URL ( canonical ( page , view , at ), "http://x/u/r/" ); 83assertEquals ( viewAt ( pageAt ( url . pathname , "main" , tip ), url . hash ), view ); 84} 85});