Ghost/ghost/core/test/unit/shared/labs.test.js

146 lines
4.8 KiB
JavaScript
Raw Normal View History

const assert = require('assert/strict');
const sinon = require('sinon');
const configUtils = require('../../utils/configUtils');
const labs = require('../../../core/shared/labs');
const settingsCache = require('../../../core/shared/settings-cache');
function expectedLabsObject(obj) {
let enabledFlags = {};
labs.GA_KEYS.forEach((key) => {
enabledFlags[key] = true;
});
enabledFlags = Object.assign(enabledFlags, obj);
return enabledFlags;
}
describe('Labs Service', function () {
Fixed configUtils and adapter cache issues in E2E tests (#16167) no issue There are a couple of issues with resetting the Ghost instance between E2E test files: These issues came to the surface because of new tests written in https://github.com/TryGhost/Ghost/pull/16117 **1. configUtils.restore does not work correctly** `config.reset()` is a callback based method. On top of that, it doesn't really work reliably (https://github.com/indexzero/nconf/issues/93) What kinda happens, is that you first call `config.reset` but immediately after you correcty reset the config using the `config.set` calls afterwards. But since `config.reset` is async, that reset will happen after all those sets, and the end result is that it isn't reset correctly. This mainly caused issues in the new updated images tests, which were updating the config `imageOptimization.contentImageSizes`, which is a deeply nested config value. Maybe some references to objects are reused in nconf that cause this issue? Wrapping `config.reset()` in a promise does fix the issue. **2. Adapters cache not reset between tests** At the start of each test, we set `paths:contentPath` to a nice new temporary directory. But if a previous test already requests a localStorage adapter, that adapter would have been created and in the constructor `paths:contentPath` would have been passed. That same instance will be reused in the next test run. So it won't read the new config again. To fix this, we need to reset the adapter instances between E2E tests. How was this visible? Test uploads were stored in the actual git repository, and not in a temporary directory. When writing the new image upload tests, this also resulted in unreliable test runs because some image names were already taken (from previous test runs). **3. Old 2E2 test Ghost server not stopped** Sometimes we still need access to the frontend test server using `getAgentsWithFrontend`. But that does start a new Ghost server which is actually listening for HTTP traffic. This could result in a fatal error in tests because the port is already in use. The issue is that old E2E tests also start a HTTP server, but they don't stop the server. When you used the old `startGhost` util, it would check if a server was already running and stop it first. The new `getAgentsWithFrontend` now also has the same functionality to fix that issue.
2023-01-30 16:06:20 +03:00
afterEach(async function () {
sinon.restore();
Fixed configUtils and adapter cache issues in E2E tests (#16167) no issue There are a couple of issues with resetting the Ghost instance between E2E test files: These issues came to the surface because of new tests written in https://github.com/TryGhost/Ghost/pull/16117 **1. configUtils.restore does not work correctly** `config.reset()` is a callback based method. On top of that, it doesn't really work reliably (https://github.com/indexzero/nconf/issues/93) What kinda happens, is that you first call `config.reset` but immediately after you correcty reset the config using the `config.set` calls afterwards. But since `config.reset` is async, that reset will happen after all those sets, and the end result is that it isn't reset correctly. This mainly caused issues in the new updated images tests, which were updating the config `imageOptimization.contentImageSizes`, which is a deeply nested config value. Maybe some references to objects are reused in nconf that cause this issue? Wrapping `config.reset()` in a promise does fix the issue. **2. Adapters cache not reset between tests** At the start of each test, we set `paths:contentPath` to a nice new temporary directory. But if a previous test already requests a localStorage adapter, that adapter would have been created and in the constructor `paths:contentPath` would have been passed. That same instance will be reused in the next test run. So it won't read the new config again. To fix this, we need to reset the adapter instances between E2E tests. How was this visible? Test uploads were stored in the actual git repository, and not in a temporary directory. When writing the new image upload tests, this also resulted in unreliable test runs because some image names were already taken (from previous test runs). **3. Old 2E2 test Ghost server not stopped** Sometimes we still need access to the frontend test server using `getAgentsWithFrontend`. But that does start a new Ghost server which is actually listening for HTTP traffic. This could result in a fatal error in tests because the port is already in use. The issue is that old E2E tests also start a HTTP server, but they don't stop the server. When you used the old `startGhost` util, it would check if a server was already running and stop it first. The new `getAgentsWithFrontend` now also has the same functionality to fix that issue.
2023-01-30 16:06:20 +03:00
await configUtils.restore();
});
it('can getAll, even if empty with enabled members', function () {
assert.deepEqual(labs.getAll(), expectedLabsObject({
members: true
}));
});
it('returns an alpha flag when dev experiments in toggled', function () {
configUtils.set('enableDeveloperExperiments', true);
sinon.stub(process.env, 'NODE_ENV').value('production');
sinon.stub(settingsCache, 'get');
settingsCache.get.withArgs('labs').returns({
urlCache: true
});
// NOTE: this test should be rewritten to test the alpha flag independently of the internal ALPHA_FEATURES list
// otherwise we end up in the endless maintenance loop and need to update it every time a feature graduates from alpha
assert.deepEqual(labs.getAll(), expectedLabsObject({
urlCache: true,
members: true
}));
assert.equal(labs.isSet('members'), true);
assert.equal(labs.isSet('urlCache'), true);
});
it('returns a falsy alpha flag when dev experiments in NOT toggled', function () {
configUtils.set('enableDeveloperExperiments', false);
sinon.stub(process.env, 'NODE_ENV').value('production');
sinon.stub(settingsCache, 'get');
settingsCache.get.withArgs('labs').returns({
urlCache: true
});
// NOTE: this test should be rewritten to test the alpha flag independently of the internal ALPHA_FEATURES list
// otherwise we end up in the endless maintenance loop and need to update it every time a feature graduates from alpha
assert.deepEqual(labs.getAll(), expectedLabsObject({
members: true
}));
assert.equal(labs.isSet('members'), true);
assert.equal(labs.isSet('urlCache'), false);
});
it('respects the value in config over settings', function () {
configUtils.set('labs', {
collections: false
});
sinon.stub(settingsCache, 'get');
settingsCache.get.withArgs('labs').returns({
collections: true,
members: true
});
assert.deepEqual(labs.getAll(), expectedLabsObject({
collections: false,
members: true
}));
assert.equal(labs.isSet('collections'), false);
});
it('respects the value in config over GA keys', function () {
configUtils.set('labs', {
audienceFeedback: false
});
assert.deepEqual(labs.getAll(), expectedLabsObject({
audienceFeedback: false,
members: true
}));
assert.equal(labs.isSet('audienceFeedback'), false);
});
it('members flag is true when members_signup_access setting is "all"', function () {
sinon.stub(settingsCache, 'get');
settingsCache.get.withArgs('members_signup_access').returns('all');
assert.deepEqual(labs.getAll(), expectedLabsObject({
members: true
}));
assert.equal(labs.isSet('members'), true);
});
it('returns other allowlisted flags along with members', function () {
sinon.stub(settingsCache, 'get');
settingsCache.get.withArgs('members_signup_access').returns('all');
settingsCache.get.withArgs('labs').returns({
activitypub: false
});
assert.deepEqual(labs.getAll(), expectedLabsObject({
members: true,
activitypub: false
}));
assert.equal(labs.isSet('members'), true);
assert.equal(labs.isSet('activitypub'), false);
});
it('members flag is false when members_signup_access setting is "none"', function () {
sinon.stub(settingsCache, 'get');
settingsCache.get.withArgs('members_signup_access').returns('none');
assert.deepEqual(labs.getAll(), expectedLabsObject({
members: false
}));
assert.equal(labs.isSet('members'), false);
});
it('isSet returns false for undefined', function () {
assert.equal(labs.isSet('bar'), false);
});
it('isSet always returns false for deprecated', function () {
assert.equal(labs.isSet('subscribers'), false);
assert.equal(labs.isSet('publicAPI'), false);
});
});