Skip to content
9 changes: 7 additions & 2 deletions blocks/shared/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ const DA_ETC_ENVS = {
local: 'http://localhost:8787',
};

const LOCALHOST_DEFAULTS = { 'da-admin': 'stage' };

function getDaEnv(location, key, envs) {
const { href } = location;
const query = new URL(href).searchParams.get(key);
const url = new URL(href);
const query = url.searchParams.get(key);
if (query && query === 'reset') {
localStorage.removeItem(key);
} else if (query) {
localStorage.setItem(key, query);
}
const env = envs[localStorage.getItem(key) || 'prod'];
const isLocal = url.hostname.includes('local');
const fallback = isLocal ? (LOCALHOST_DEFAULTS[key] || 'prod') : 'prod';
const env = envs[localStorage.getItem(key)] || envs[fallback] || envs.prod;
// TODO: INFRA
return location.origin === 'https://da.page' ? env.replace('.live', '.page') : env;
}
Expand Down
2 changes: 1 addition & 1 deletion blocks/shared/pathDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function getFullDetails({ editor, pathParts, ext }) {
function getExtension(editor, name, isFolder) {
const nameSplit = name.split('.');
if (nameSplit.length >= 2) return nameSplit.pop();
if (isFolder || editor === 'browse') return null;
if (isFolder || editor === 'browse' || editor === 'config') return null;
if (editor === 'sheet' && nameSplit.slice(-1)[0] !== 'json') return 'json';
return 'html';
}
Expand Down
3 changes: 2 additions & 1 deletion test/unit/blocks/edit/prose/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import initProse, {
createAwarenessStatusWidget,
} from '../../../../../blocks/edit/prose/index.js';
import { forceSave } from '../../../../../blocks/shared/forcesave.js';
import { DA_ORIGIN } from '../../../../../blocks/shared/constants.js';

// initProse lazily imports da-library.js, which (a) builds URLs from
// `${getNx()}/...` and (b) calls loadLibrary() at module import time.
Expand Down Expand Up @@ -99,7 +100,7 @@ describe('prose/index createConnection', () => {
expect(result.ydoc).to.exist;
expect(result.wsProvider.maxBackoffTime).to.equal(30000);
// Legacy (non-hlx6) docs keep the admin.da.live collab room.
expect(result.wsProvider.roomname).to.equal('https://admin.da.live/source/org/repo/page.html');
expect(result.wsProvider.roomname).to.equal(`${DA_ORIGIN}/source/org/repo/page.html`);
// Clean up the underlying WS connection
result.wsProvider.disconnect({ data: 'Client navigation' });
result.wsProvider.destroy?.();
Expand Down
26 changes: 23 additions & 3 deletions test/unit/blocks/shared/constants.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import '../../milo.js';
describe('DA Admin', () => {
it('Sets DA admin default', () => {
const env = getDaAdmin();
expect(env).to.equal('https://admin.da.live');
expect(env).to.equal('https://stage-admin.da.live');
});

it('Sets DA admin stage', () => {
Expand All @@ -29,15 +29,35 @@ describe('DA Admin', () => {
expect(env).to.equal('https://stage-admin.da.live');
});

it('Resets DA admin', () => {
it('Resets DA admin to the localhost default', () => {
const env = getDaAdmin({ href: 'http://localhost:3000/?da-admin=reset' });
expect(env).to.equal('https://stage-admin.da.live');
});
});

describe('Local dev defaults to stage', () => {
it('Defaults localhost to stage admin', () => {
localStorage.removeItem('da-admin');
const env = getDaAdmin({ href: 'http://localhost:3000/' });
expect(env).to.equal('https://stage-admin.da.live');
});

it('Keeps prod default for non-local hosts', () => {
localStorage.removeItem('da-admin');
const env = getDaAdmin({ href: 'https://da.live/' });
expect(env).to.equal('https://admin.da.live');
});

it('Honors an explicit override on localhost', () => {
const env = getDaAdmin({ href: 'http://localhost:3000/?da-admin=prod' });
expect(env).to.equal('https://admin.da.live');
getDaAdmin({ href: 'http://localhost:3000/?da-admin=reset' });
});
});

describe('Other origins', () => {
it('Sets DA Origin', () => {
expect(DA_ORIGIN).to.equal('https://admin.da.live');
expect(DA_ORIGIN).to.equal('https://stage-admin.da.live');
});

it('Sets Content Origin', () => {
Expand Down
55 changes: 29 additions & 26 deletions test/unit/blocks/shared/pathDetails.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@esm-bundle/chai';
import '../../milo.js';
import { DA_ORIGIN } from '../../../../blocks/shared/constants.js';

// Dynamic import because Milo dependency
const { default: getPathDetails } = await import('../../../../blocks/shared/pathDetails.js');
Expand Down Expand Up @@ -66,10 +67,10 @@ describe('Path details', () => {
it('Handles folder config (/)', () => {
const loc = { pathname: '/config', hash: '#/adobe/' };
const details = getPathDetails(loc);
expect(details.origin).to.equal('https://admin.da.live');
expect(details.origin).to.equal(DA_ORIGIN);
expect(details.fullpath).to.equal('/adobe/');
expect(details.repo).to.equal(undefined);
expect(details.sourceUrl).to.equal('https://admin.da.live/config/adobe');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/config/adobe`);
expect(details.name).to.equal('config');
expect(details.parent).to.equal('/adobe');
expect(details.parentName).to.equal('adobe');
Expand All @@ -79,16 +80,17 @@ describe('Path details', () => {
const loc = { pathname: '/config', hash: '#/adobe.json' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe.json');
expect(details.sourceUrl).to.equal('https://admin.da.live/config/adobe.json');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/config/adobe.json`);
expect(details.parent).to.equal('/');
expect(details.parentName).to.equal('Root');
});

it('Handles HTML config ()', () => {
it('Handles config without a trailing slash ()', () => {
const loc = { pathname: '/config', hash: '#/adobe' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/config/adobe.html');
expect(details.fullpath).to.equal('/adobe');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/config/adobe`);
expect(details.name).to.equal('config');
});
});

Expand All @@ -97,7 +99,7 @@ describe('Path details', () => {
const loc = { pathname: '/sheet', hash: '#/adobe' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe.json');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe.json');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe.json`);
});
});

Expand All @@ -106,7 +108,7 @@ describe('Path details', () => {
const loc = { pathname: '/edit', hash: '#/adobe' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe.html');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe.html`);
});
});

Expand All @@ -128,7 +130,7 @@ describe('Path details', () => {
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/');
expect(details.repo).to.equal('geometrixx');
expect(details.sourceUrl).to.equal('https://admin.da.live/config/adobe/geometrixx');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/config/adobe/geometrixx`);
expect(details.name).to.equal('geometrixx config');
expect(details.parent).to.equal('/adobe/geometrixx');
expect(details.parentName).to.equal('geometrixx');
Expand All @@ -138,16 +140,17 @@ describe('Path details', () => {
const loc = { pathname: '/config', hash: '#/adobe/geometrixx.json' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx.json');
expect(details.sourceUrl).to.equal('https://admin.da.live/config/adobe/geometrixx.json');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/config/adobe/geometrixx.json`);
expect(details.parent).to.equal('/adobe');
expect(details.parentName).to.equal('adobe');
});

it('Handles HTML config ()', () => {
it('Handles config without a trailing slash ()', () => {
const loc = { pathname: '/config', hash: '#/adobe/geometrixx' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/config/adobe/geometrixx.html');
expect(details.fullpath).to.equal('/adobe/geometrixx');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/config/adobe/geometrixx`);
expect(details.name).to.equal('geometrixx config');
});
});

Expand All @@ -156,7 +159,7 @@ describe('Path details', () => {
const loc = { pathname: '/sheet', hash: '#/adobe/geometrixx' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx.json');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe/geometrixx.json');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe/geometrixx.json`);
});
});

Expand All @@ -165,7 +168,7 @@ describe('Path details', () => {
const loc = { pathname: '/edit', hash: '#/adobe/geometrixx' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe/geometrixx.html');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe/geometrixx.html`);
});
});
});
Expand All @@ -177,7 +180,7 @@ describe('Path details', () => {
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/testing-123/');
expect(details.repo).to.equal('geometrixx');
expect(details.sourceUrl).to.equal('https://admin.da.live/config/adobe/geometrixx/testing-123');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/config/adobe/geometrixx/testing-123`);
expect(details.name).to.equal('config');
expect(details.parent).to.equal('/adobe/geometrixx/testing-123');
expect(details.parentName).to.equal('testing-123');
Expand All @@ -187,16 +190,16 @@ describe('Path details', () => {
const loc = { pathname: '/config', hash: '#/adobe/geometrixx/testing-123.json' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/testing-123.json');
expect(details.sourceUrl).to.equal('https://admin.da.live/config/adobe/geometrixx/testing-123.json');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/config/adobe/geometrixx/testing-123.json`);
expect(details.parent).to.equal('/adobe/geometrixx');
expect(details.parentName).to.equal('geometrixx');
});

it('Handles HTML config ()', () => {
it('Handles config without a trailing slash ()', () => {
const loc = { pathname: '/config', hash: '#/adobe/geometrixx/testing-123' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/testing-123.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/config/adobe/geometrixx/testing-123.html');
expect(details.fullpath).to.equal('/adobe/geometrixx/testing-123');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/config/adobe/geometrixx/testing-123`);
});
});

Expand All @@ -205,7 +208,7 @@ describe('Path details', () => {
const loc = { pathname: '/sheet', hash: '#/adobe/geometrixx/testing-123' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/testing-123.json');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe/geometrixx/testing-123.json');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe/geometrixx/testing-123.json`);
});
});

Expand All @@ -214,35 +217,35 @@ describe('Path details', () => {
const loc = { pathname: '/edit', hash: '#/adobe/geometrixx/testing-123' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/testing-123.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe/geometrixx/testing-123.html');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe/geometrixx/testing-123.html`);
});

it('Handles HTML edit if page has .html extension ()', () => {
const loc = { pathname: '/edit', hash: '#/adobe/geometrixx/testing-123.html' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/testing-123.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe/geometrixx/testing-123.html');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe/geometrixx/testing-123.html`);
});

it('Handles HTML edit if page name is html and no extension ()', () => {
const loc = { pathname: '/edit', hash: '#/adobe/geometrixx/html' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/html.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe/geometrixx/html.html');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe/geometrixx/html.html`);
});

it('Handles HTML edit if page name is "ilikehtml" and no extension ()', () => {
const loc = { pathname: '/edit', hash: '#/adobe/geometrixx/ilikehtml' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/ilikehtml.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe/geometrixx/ilikehtml.html');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe/geometrixx/ilikehtml.html`);
});

it('Handles HTML edit if page name is html and no extension and has .html extension ()', () => {
const loc = { pathname: '/edit', hash: '#/adobe/geometrixx/html.html' };
const details = getPathDetails(loc);
expect(details.fullpath).to.equal('/adobe/geometrixx/html.html');
expect(details.sourceUrl).to.equal('https://admin.da.live/source/adobe/geometrixx/html.html');
expect(details.sourceUrl).to.equal(`${DA_ORIGIN}/source/adobe/geometrixx/html.html`);
});
});
});
Expand Down
Loading