Skip to content

Commit 2f7f68b

Browse files
committed
chore: adjust tests
1 parent 587a124 commit 2f7f68b

File tree

3 files changed

+139
-53
lines changed

3 files changed

+139
-53
lines changed

test/aem-sidekick.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ describe('AEM Sidekick', () => {
7474
})).to.be.true;
7575
});
7676

77+
it('reload shortcut when open: busts cache and reloadPage; when other key: no-op', async () => {
78+
sidekick = sidekickTest.createSidekick();
79+
await sidekickTest.awaitEnvSwitcher();
80+
81+
const sendMessageStub = sidekickTest.sandbox.stub(window.chrome.runtime, 'sendMessage').resolves();
82+
const reloadPageStub = sidekickTest.sandbox.stub(sidekickTest.appStore, 'reloadPage');
83+
84+
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'x', bubbles: true }));
85+
expect(sendMessageStub.called).to.be.false;
86+
expect(reloadPageStub.called).to.be.false;
87+
88+
sidekick.open = true;
89+
await sidekick.updateComplete;
90+
expect(sidekick.open).to.be.true;
91+
92+
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'x', bubbles: true }));
93+
expect(sendMessageStub.called).to.be.false;
94+
expect(reloadPageStub.called).to.be.false;
95+
96+
window.dispatchEvent(new KeyboardEvent('keydown', { key: 'r', metaKey: true, bubbles: true }));
97+
await waitUntil(() => reloadPageStub.calledOnce, 'reloadPage was not called', { timeout: 2000 });
98+
expect(sendMessageStub.calledWith({ action: 'bustCache' })).to.be.true;
99+
}).timeout(5000);
100+
77101
it('dispatches sidekick-ready', async () => {
78102
const readySpy = spy();
79103
document.addEventListener('sidekick-ready', readySpy);

test/auth.test.js

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import sinon from 'sinon';
1919

2020
import {
2121
configureAuthAndCorsHeaders,
22-
getCacheControlRules,
23-
getHostDomain,
2422
setAuthToken,
2523
updateUserAgent,
2624
} from '../src/extension/auth.js';
@@ -56,57 +54,6 @@ describe('Test auth', () => {
5654
await configureAuthAndCorsHeaders();
5755
});
5856

59-
describe('getHostDomain', () => {
60-
it('returns hostname for full URL', () => {
61-
expect(getHostDomain('https://example.com/path')).to.equal('example.com');
62-
expect(getHostDomain('https://sub.example.com:443/')).to.equal('sub.example.com');
63-
});
64-
it('returns input for plain domain', () => {
65-
expect(getHostDomain('example.com')).to.equal('example.com');
66-
expect(getHostDomain('main--repo--owner.aem.live')).to.equal('main--repo--owner.aem.live');
67-
});
68-
it('returns empty string for invalid or empty input', () => {
69-
expect(getHostDomain('')).to.equal('');
70-
expect(getHostDomain(null)).to.equal('');
71-
expect(getHostDomain(undefined)).to.equal('');
72-
expect(getHostDomain(123)).to.equal('');
73-
});
74-
});
75-
76-
describe('getCacheControlRules', () => {
77-
it('returns one rule per unique host', () => {
78-
const configs = [
79-
{ host: 'prod.example.com' },
80-
{ liveHost: 'live.example.com' },
81-
];
82-
const rules = getCacheControlRules(configs);
83-
expect(rules).to.have.lengthOf(2);
84-
expect(rules.every((r) => r.action?.requestHeaders?.some((h) => h.header === 'Cache-Control' && h.value === 'no-cache'))).to.be.true;
85-
expect(rules.every((r) => r.action?.requestHeaders?.some((h) => h.header === 'Pragma' && h.value === 'no-cache'))).to.be.true;
86-
const filters = rules.map((r) => r.condition.regexFilter);
87-
expect(filters).to.include('^https://prod\\.example\\.com/.*');
88-
expect(filters).to.include('^https://live\\.example\\.com/.*');
89-
});
90-
it('deduplicates same host across configs', () => {
91-
const configs = [
92-
{ host: 'same.com', liveHost: 'same.com' },
93-
];
94-
const rules = getCacheControlRules(configs);
95-
expect(rules).to.have.lengthOf(1);
96-
expect(rules[0].condition.regexFilter).to.equal('^https://same\\.com/.*');
97-
});
98-
it('extracts host from full URL in config', () => {
99-
const configs = [{ host: 'https://url-host.com/path' }];
100-
const rules = getCacheControlRules(configs);
101-
expect(rules).to.have.lengthOf(1);
102-
expect(rules[0].condition.regexFilter).to.equal('^https://url-host\\.com/.*');
103-
});
104-
it('returns empty array for empty or no valid hosts', () => {
105-
expect(getCacheControlRules([])).to.deep.equal([]);
106-
expect(getCacheControlRules([{ host: '' }, { liveHost: null }])).to.deep.equal([]);
107-
});
108-
});
109-
11057
it('setAuthToken', async () => {
11158
const updateSessionRules = sandbox.spy(chrome.declarativeNetRequest, 'updateSessionRules');
11259
const getConfig = sandbox.spy(chrome.storage.session, 'get');

test/cache-buster.test.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright 2026 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
/* eslint-disable no-unused-expressions */
13+
14+
import { expect } from '@open-wc/testing';
15+
import sinon from 'sinon';
16+
17+
import { addCacheBusterRule, getHostDomain } from '../src/extension/cache-buster.js';
18+
import chromeMock from './mocks/chrome.js';
19+
import { log } from '../src/extension/log.js';
20+
21+
// @ts-ignore
22+
window.chrome = chromeMock;
23+
24+
describe('cache-buster', () => {
25+
const sandbox = sinon.createSandbox();
26+
27+
afterEach(() => {
28+
sandbox.restore();
29+
});
30+
31+
describe('getHostDomain', () => {
32+
it('returns empty string for null or undefined', () => {
33+
expect(getHostDomain(null)).to.equal('');
34+
expect(getHostDomain(undefined)).to.equal('');
35+
});
36+
37+
it('returns empty string for empty or whitespace-only string', () => {
38+
expect(getHostDomain('')).to.equal('');
39+
expect(getHostDomain(' ')).to.equal('');
40+
});
41+
42+
it('returns hostname for full URL', () => {
43+
expect(getHostDomain('https://example.com/path')).to.equal('example.com');
44+
expect(getHostDomain('https://sub.example.com:443/')).to.equal('sub.example.com');
45+
expect(getHostDomain('http://foo.com')).to.equal('foo.com');
46+
});
47+
48+
it('returns input for plain domain', () => {
49+
expect(getHostDomain('example.com')).to.equal('example.com');
50+
expect(getHostDomain('main--repo--owner.aem.live')).to.equal('main--repo--owner.aem.live');
51+
});
52+
53+
it('trims whitespace', () => {
54+
expect(getHostDomain(' example.com ')).to.equal('example.com');
55+
});
56+
});
57+
58+
describe('addCacheBusterRule', () => {
59+
it('returns false and warns when domain is empty after getHostDomain', async () => {
60+
const logWarn = sandbox.stub(log, 'warn');
61+
expect(await addCacheBusterRule('')).to.equal(false);
62+
expect(await addCacheBusterRule(' ')).to.equal(false);
63+
expect(logWarn.calledWith('addCacheBusterRule: no domain')).to.be.true;
64+
});
65+
66+
it('returns false and warns when domain is null or undefined', async () => {
67+
const logWarn = sandbox.stub(log, 'warn');
68+
expect(await addCacheBusterRule(null)).to.equal(false);
69+
expect(await addCacheBusterRule(undefined)).to.equal(false);
70+
expect(logWarn.calledWith('addCacheBusterRule: no domain')).to.be.true;
71+
});
72+
73+
it('adds rule and schedules removal after 10s', async () => {
74+
const clock = sandbox.useFakeTimers();
75+
const updateSessionRules = sandbox.stub(chrome.declarativeNetRequest, 'updateSessionRules').resolves();
76+
77+
const result = await addCacheBusterRule('example.com');
78+
79+
expect(result).to.equal(true);
80+
expect(updateSessionRules.calledTwice).to.be.false;
81+
expect(updateSessionRules.calledOnce).to.be.true;
82+
const [firstCall] = updateSessionRules.args;
83+
expect(firstCall[0]).to.have.property('addRules');
84+
expect(firstCall[0].addRules).to.have.lengthOf(1);
85+
const rule = firstCall[0].addRules[0];
86+
expect(rule).to.have.property('id');
87+
expect(rule.priority).to.equal(1);
88+
expect(rule.action.type).to.equal('modifyHeaders');
89+
expect(rule.action.requestHeaders).to.deep.include(
90+
{ operation: 'set', header: 'Cache-Control', value: 'no-cache' },
91+
);
92+
expect(rule.action.requestHeaders).to.deep.include(
93+
{ operation: 'set', header: 'Pragma', value: 'no-cache' },
94+
);
95+
expect(rule.condition.regexFilter).to.equal('^https://example\\.com/.*');
96+
expect(rule.condition.requestMethods).to.deep.equal(['get']);
97+
98+
clock.tick(10 * 1000);
99+
expect(updateSessionRules.calledTwice).to.be.true;
100+
const removeCall = updateSessionRules.getCall(1).args[0];
101+
expect(removeCall).to.have.property('removeRuleIds');
102+
expect(removeCall.removeRuleIds).to.deep.equal([rule.id]);
103+
});
104+
105+
it('accepts full URL and uses hostname', async () => {
106+
const updateSessionRules = sandbox.stub(chrome.declarativeNetRequest, 'updateSessionRules').resolves();
107+
108+
const result = await addCacheBusterRule('https://my.site.com/path');
109+
110+
expect(result).to.equal(true);
111+
const rule = updateSessionRules.firstCall.args[0].addRules[0];
112+
expect(rule.condition.regexFilter).to.equal('^https://my\\.site\\.com/.*');
113+
});
114+
});
115+
});

0 commit comments

Comments
 (0)