@@ -3,14 +3,17 @@ import { module, test } from 'qunit';
33import {
44 type LooseCardResource ,
55 type RealmResourceIdentifier ,
6- relativeURL ,
6+ maybeRelativeReference ,
7+ relativeReference ,
8+ ri ,
9+ rri ,
710 visitInstanceURLs ,
811} from '@cardstack/runtime-common' ;
912
1013module ( 'Unit | url' , function ( ) {
11- module ( 'relativeURL ' , function ( ) {
14+ module ( 'relativeReference ' , function ( ) {
1215 test ( 'returns undefined for different origins' , function ( assert ) {
13- let result = relativeURL (
16+ let result = relativeReference (
1417 new URL ( 'https://other.example.com/a' ) ,
1518 new URL ( 'https://example.com/a' ) ,
1619 undefined ,
@@ -21,7 +24,7 @@ module('Unit | url', function () {
2124
2225 test ( 'returns undefined when realm URL blocks escaping the realm' , function ( assert ) {
2326 let realm = new URL ( 'https://example.com/realm/' ) ;
24- let result = relativeURL (
27+ let result = relativeReference (
2528 new URL ( 'https://example.com/outside/card' ) ,
2629 new URL ( 'https://example.com/realm/card' ) ,
2730 realm ,
@@ -31,7 +34,7 @@ module('Unit | url', function () {
3134 } ) ;
3235
3336 test ( 'returns absolute path when first path segment differs and relativeTo is nested' , function ( assert ) {
34- let result = relativeURL (
37+ let result = relativeReference (
3538 new URL ( 'https://example.com/d/e' ) ,
3639 new URL ( 'https://example.com/a/b/c' ) ,
3740 undefined ,
@@ -41,7 +44,7 @@ module('Unit | url', function () {
4144 } ) ;
4245
4346 test ( 'creates relative path to upper-level sibling' , function ( assert ) {
44- let result = relativeURL (
47+ let result = relativeReference (
4548 new URL ( 'https://example.com/d' ) ,
4649 new URL ( 'https://example.com/a/b' ) ,
4750 undefined ,
@@ -51,7 +54,7 @@ module('Unit | url', function () {
5154 } ) ;
5255
5356 test ( 'creates sibling-relative path' , function ( assert ) {
54- let result = relativeURL (
57+ let result = relativeReference (
5558 new URL ( 'https://example.com/a/b/other' ) ,
5659 new URL ( 'https://example.com/a/b/file' ) ,
5760 undefined ,
@@ -61,7 +64,7 @@ module('Unit | url', function () {
6164 } ) ;
6265
6366 test ( 'creates parent-relative path' , function ( assert ) {
64- let result = relativeURL (
67+ let result = relativeReference (
6568 new URL ( 'https://example.com/a/b/e' ) ,
6669 new URL ( 'https://example.com/a/b/c/d' ) ,
6770 undefined ,
@@ -71,7 +74,7 @@ module('Unit | url', function () {
7174 } ) ;
7275
7376 test ( 'returns ./file when URL matches relativeTo exactly' , function ( assert ) {
74- let result = relativeURL (
77+ let result = relativeReference (
7578 new URL ( 'https://example.com/a/b' ) ,
7679 new URL ( 'https://example.com/a/b' ) ,
7780 undefined ,
@@ -81,14 +84,86 @@ module('Unit | url', function () {
8184 } ) ;
8285
8386 test ( 'returns ./file when relativeTo is root' , function ( assert ) {
84- let result = relativeURL (
87+ let result = relativeReference (
8588 new URL ( 'https://example.com/b' ) ,
8689 new URL ( 'https://example.com/' ) ,
8790 undefined ,
8891 ) ;
8992
9093 assert . strictEqual ( result , './b' ) ;
9194 } ) ;
95+
96+ test ( 'creates sibling-relative path between two prefix-form RRIs in the same scope' , function ( assert ) {
97+ let result = relativeReference (
98+ rri ( '@cardstack/base/foo' ) ,
99+ rri ( '@cardstack/base/bar' ) ,
100+ undefined ,
101+ ) ;
102+
103+ assert . strictEqual ( result , './foo' ) ;
104+ } ) ;
105+
106+ test ( 'returns undefined for prefix-form RRIs in different scopes' , function ( assert ) {
107+ let result = relativeReference (
108+ rri ( '@cardstack/base/foo' ) ,
109+ rri ( '@cardstack/catalog/bar' ) ,
110+ undefined ,
111+ ) ;
112+
113+ assert . strictEqual ( result , undefined ) ;
114+ } ) ;
115+
116+ test ( 'returns undefined for mixed URL + prefix-form RRI inputs' , function ( assert ) {
117+ let result = relativeReference (
118+ rri ( '@cardstack/base/foo' ) ,
119+ new URL ( 'https://my-realm.com/bar' ) ,
120+ undefined ,
121+ ) ;
122+
123+ assert . strictEqual ( result , undefined ) ;
124+ } ) ;
125+
126+ test ( 'produces a relative path within a prefix-form realm' , function ( assert ) {
127+ let result = relativeReference (
128+ rri ( '@cardstack/base/foo' ) ,
129+ rri ( '@cardstack/base/sub/bar' ) ,
130+ ri ( '@cardstack/base/' ) ,
131+ ) ;
132+
133+ assert . strictEqual ( result , '../foo' ) ;
134+ } ) ;
135+
136+ test ( 'returns undefined when a prefix-form realm blocks escaping' , function ( assert ) {
137+ let result = relativeReference (
138+ rri ( '@cardstack/base/outside' ) ,
139+ rri ( '@cardstack/base/sub/inside' ) ,
140+ ri ( '@cardstack/base/sub/' ) ,
141+ ) ;
142+
143+ assert . strictEqual ( result , undefined ) ;
144+ } ) ;
145+ } ) ;
146+
147+ module ( 'maybeRelativeReference' , function ( ) {
148+ test ( 'falls back to absolute href for un-relativizable URL inputs' , function ( assert ) {
149+ let result = maybeRelativeReference (
150+ new URL ( 'https://a.com/foo' ) ,
151+ new URL ( 'https://b.com/bar' ) ,
152+ undefined ,
153+ ) ;
154+
155+ assert . strictEqual ( result , 'https://a.com/foo' ) ;
156+ } ) ;
157+
158+ test ( 'falls back to the prefix-form RRI as-is for un-relativizable RRI inputs' , function ( assert ) {
159+ let result = maybeRelativeReference (
160+ rri ( '@cardstack/base/foo' ) ,
161+ new URL ( 'https://my-realm.com/bar' ) ,
162+ undefined ,
163+ ) ;
164+
165+ assert . strictEqual ( result , '@cardstack/base/foo' ) ;
166+ } ) ;
92167 } ) ;
93168
94169 module ( 'Unit | document | visitInstanceURLs' , function ( ) {
0 commit comments