Skip to content

Commit

Permalink
Merge pull request #2296 from adobe/detect-h5
Browse files Browse the repository at this point in the history
  • Loading branch information
trieloff authored Jan 23, 2024
2 parents d3bbc5a + f792d83 commit 4980610
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/up.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,39 @@ export default class UpCommand extends AbstractServerCommand {
}

async verifyUrl(gitUrl, inref) {
// check if the site is on helix5
// https://admin.hlx.page/sidekick/adobe/www-aem-live/main/config.json
// {
// "host": "aem.live",
// "liveHost": "main--www-aem-live--adobe.aem.live",
// "plugins": [
// {
// "id": "doc",
// "title": "Documentation",
// "url": "https://www.aem.live/docs/"
// }
// ],
// "previewHost": "main--www-aem-live--adobe.aem.page",
// "project": "Helix Website (AEM Live)",
// "testProperty": "header";
// }
const configUrl = `https://admin.hlx.page/sidekick/${gitUrl.repo}/${gitUrl.owner}/${inref}/config.json`;
const configResp = await getFetch()(configUrl);
let previewHostBase = 'hlx.page';
if (configResp.ok) {
// this is best effort for now
const config = await configResp.json();
const { previewHost } = config;
if (previewHost && previewHost.endsWith('.aem.page')) {
previewHostBase = 'aem.page';
}
}

let ref = inref;

// replace `/` by `-` in ref.
ref = ref.replace(/\//g, '-');
this._url = `https://${ref}--${gitUrl.repo}--${gitUrl.owner}.hlx.page`;
this._url = `https://${ref}--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`;
// check length limit
if (this._url.split('.')
.map((part) => part.replace(/^https:\/\//, ''))
Expand All @@ -125,7 +153,7 @@ export default class UpCommand extends AbstractServerCommand {
this.log.warn(chalk`Unable to verify {yellow main} branch via {blue ${fstabUrl}} (${resp.status}). Maybe not pushed yet?`);
} else {
this.log.warn(chalk`Unable to verify {yellow ${ref}} branch on {blue ${fstabUrl}} (${resp.status}). Fallback to {yellow main} branch.`);
this._url = `https://main--${gitUrl.repo}--${gitUrl.owner}.hlx.page`;
this._url = `https://main--${gitUrl.repo}--${gitUrl.owner}.${previewHostBase}`;
}
}
}
Expand Down
95 changes: 95 additions & 0 deletions test/up-cmd.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,73 @@ describe('Integration test for up command with helix pages', function suite() {
.get('/not-found.txt')
.reply(404);

nock('https://admin.hlx.page:443')
.get('/sidekick/dummy-foo/adobe/master/config.json')
.reply(404);

let port;
await new Promise((resolve, reject) => {
let error = null;
cmd
.on('started', async () => {
try {
port = cmd.project.server.port;
let ret = await assertHttp(`http://127.0.0.1:${port}/index.html`, 200);
assert.strictEqual(ret.trim(), '## Welcome');
ret = await assertHttp(`http://127.0.0.1:${port}/local.txt`, 200);
assert.strictEqual(ret.trim(), 'Hello, world.');
await assertHttp(`http://127.0.0.1:${port}/not-found.txt`, 404);
} catch (e) {
error = e;
}
await cmd.stop();
})
.on('stopped', () => {
if (error) {
reject(error);
}
resolve();
})
.run()
.catch(reject);
});
assert.strictEqual(opened, `http://localhost:${port}/`);
});

it('up command opens browser and delivers correct response on helix 5.', async () => {
let opened;
const MockedCommand = await esmock('../src/up.cmd.js', {
'../src/abstract-server.cmd.js': await esmock('../src/abstract-server.cmd.js', {
open: (url) => {
opened = url;
},
}),
});
initGit(testDir, 'https://github.com/adobe/dummy-foo.git');
const cmd = new MockedCommand()
.withLiveReload(false)
.withDirectory(testDir)
.withOpen('/')
.withHttpPort(0);

nock('https://master--dummy-foo--adobe.aem.page')
.get('/fstab.yaml')
.reply(200, 'dummy')
.get('/index.html')
.reply(200, '## Welcome')
.get('/not-found.txt')
.reply(404);

nock('https://admin.hlx.page:443')
.get('/sidekick/dummy-foo/adobe/master/config.json')
.reply(200, {
host: 'example.com',
liveHost: 'master--dummy-foo--adobe.aem.live',
previewHost: 'master--dummy-foo--adobe..aem.page',
project: 'Example Project on Helix 5',
testProperty: 'header',
});

let port;
await new Promise((resolve, reject) => {
let error = null;
Expand Down Expand Up @@ -116,6 +183,10 @@ describe('Integration test for up command with helix pages', function suite() {
.get('/not-found.txt')
.reply(404);

nock('https://admin.hlx.page:443')
.get('/sidekick/dummy-foo/adobe/tripod/test/config.json')
.reply(404);

cmd
.on('started', async () => {
try {
Expand Down Expand Up @@ -160,6 +231,10 @@ describe('Integration test for up command with helix pages', function suite() {
.get('/fstab.yaml')
.reply(404, 'dummy');

nock('https://admin.hlx.page:443')
.get('/sidekick/dummy-foo/adobe/master/config.json')
.reply(404);

cmd
.on('started', async () => {
try {
Expand Down Expand Up @@ -208,6 +283,14 @@ describe('Integration test for up command with helix pages', function suite() {
.get('/fstab.yaml')
.reply(200, 'yep!');

nock('https://admin.hlx.page:443')
.get('/sidekick/dummy-foo/adobe/master/config.json')
.reply(404);

nock('https://admin.hlx.page:443')
.get('/sidekick/dummy-foo/adobe/new-branch/config.json')
.reply(404);

let timer;
cmd
.on('started', async () => {
Expand Down Expand Up @@ -257,6 +340,14 @@ describe('Integration test for up command with helix pages', function suite() {
.get('/fstab.yaml')
.reply(404, 'dummy');

nock('https://admin.hlx.page:443')
.get('/sidekick/dummy-foo/adobe/master/config.json')
.reply(404);

nock('https://admin.hlx.page:443')
.get('/sidekick/dummy-foo/adobe/new-and-totally-unreasonably-long-in-fact-too-long-branch/config.json')
.reply(404);

let timer;
cmd
.on('started', async () => {
Expand Down Expand Up @@ -336,6 +427,10 @@ describe('Integration test for up command with cache', function suite() {
.get('/head.html')
.reply(200, '<link rel="stylesheet" href="/styles.css"/>');

nock('https://admin.hlx.page:443')
.get('/sidekick/dummy-foo/adobe/master/config.json')
.reply(404);

nock.enableNetConnect(/127.0.0.1/);

cmd
Expand Down

0 comments on commit 4980610

Please sign in to comment.