Skip to content

Commit

Permalink
feat(message-system): target user by app revision
Browse files Browse the repository at this point in the history
(cherry picked from commit 0f26ab7)
  • Loading branch information
tomasklim authored and vdovhanych committed Apr 26, 2022
1 parent 1a1af36 commit 33df244
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 24 deletions.
6 changes: 4 additions & 2 deletions docs/misc/message_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Structure of config, types and optionality of specific keys can be found in the
},
/*
For os, environment, browser and transport.
- Values can be array, string or null.
- All values (except for revision in environment) are version definitions
- Semver npm library is used for working with versions https://www.npmjs.com/package/semver.
- "*" = all versions; "!" = not for this type of browser/os/...
- Options: gte, lt, ranges, tildes, carets,... are supported, see semver lib for more info.
Expand All @@ -125,10 +125,12 @@ Structure of config, types and optionality of specific keys can be found in the
"ios": "13",
"chromeos": "*"
},
// revision is optional
"environment": {
"desktop": "<21.5",
"mobile": "!",
"web": "<22"
"web": "<22",
"revision": "7281ac61483e38d974625c2505bfe5efd519aacb"
},
"browser": {
"firefox": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@
},
"web": {
"$ref": "#/definitions/version"
},
"revision": {
"type": "string"
}
},
"additionalProperties": {
"$ref": "#/definitions/version"
}
},
"browser": {
Expand Down
51 changes: 47 additions & 4 deletions packages/suite/src/utils/suite/__fixtures__/messageSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,11 @@ export const validateVersionCompatibility = [
version: '34.0.2',
result: true,
},
];

export const validateEnvironmentCompatibility = [
{
description: 'environment validateVersionCompatibility case 1',
description: 'validateEnvironmentCompatibility case 1',
condition: {
web: '',
desktop: '0',
Expand All @@ -385,7 +388,7 @@ export const validateVersionCompatibility = [
result: true,
},
{
description: 'environment validateVersionCompatibility case 2',
description: 'validateEnvironmentCompatibility case 2',
condition: {
web: '',
desktop: '0',
Expand All @@ -396,18 +399,19 @@ export const validateVersionCompatibility = [
result: false,
},
{
description: 'environment validateVersionCompatibility case 3',
description: 'validateEnvironmentCompatibility case 3',
condition: {
web: '',
desktop: '0',
mobile: '!',
},
commitHash: 'fa8eab',
type: 'desktop',
version: '0.0.1',
result: true,
},
{
description: 'environment validateVersionCompatibility case 4',
description: 'validateEnvironmentCompatibility case 4',
condition: {
web: '',
desktop: '0',
Expand All @@ -417,6 +421,45 @@ export const validateVersionCompatibility = [
version: '0.0.1',
result: false,
},
{
description: 'validateEnvironmentCompatibility case 5',
condition: {
web: '*',
desktop: '!',
mobile: '!',
revision: 'fa8eab',
},
commitHash: 'fa8eab',
type: 'web',
version: '22.2.2',
result: true,
},
{
description: 'validateEnvironmentCompatibility case 6',
condition: {
web: '*',
desktop: '!',
mobile: '!',
revision: 'fa8eab',
},
commitHash: 'hahhah',
type: 'web',
version: '22.2.2',
result: false,
},
{
description: 'validateEnvironmentCompatibility case 7',
condition: {
web: '*',
desktop: '!',
mobile: '!',
revision: 'fa8eab',
},
commitHash: undefined,
type: 'web',
version: '22.2.2',
result: false,
},
];

export const validateTransportCompatibility = [
Expand Down
26 changes: 26 additions & 0 deletions packages/suite/src/utils/suite/__tests__/messageSystem.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ describe('Message system utils', () => {
});
});

describe('validateEnvironmentCompatibility', () => {
const OLD_ENV = { ...process.env };

afterEach(() => {
jest.resetModules();
process.env = OLD_ENV;
});

fixtures.validateEnvironmentCompatibility.forEach(f => {
it(f.description, () => {
process.env.COMMITHASH = f.commitHash;

expect(
// @ts-ignore
messageSystem.validateEnvironmentCompatibility(
f.condition,
// @ts-ignore
f.type,
f.version,
f.commitHash,
),
).toEqual(f.result);
});
});
});

describe('validateTransportCompatibility', () => {
fixtures.validateTransportCompatibility.forEach(f => {
it(f.description, () => {
Expand Down
40 changes: 25 additions & 15 deletions packages/suite/src/utils/suite/messageSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import { getDeviceModel, getFwVersion, isBitcoinOnly } from './device';
import type { TransportInfo } from 'trezor-connect';

import type { Network } from '@wallet-types';
import type { TrezorDevice, EnvironmentType } from '@suite-types';
import type { EnvironmentType, TrezorDevice } from '@suite-types';
import type {
Duration,
MessageSystem,
Message,
Version,
OperatingSystem,
Settings,
Transport,
Browser,
Device,
Environment,
} from '@suite-types/messageSystem';
Expand Down Expand Up @@ -69,8 +67,8 @@ export const validateDurationCompatibility = (durationCondition: Duration): bool
};

export const validateVersionCompatibility = (
condition: Browser | OperatingSystem | Environment | Transport,
type: string | EnvironmentType,
condition: { [key: string]: Version | undefined },
type: string,
version: string,
): boolean => {
const conditionVersion = createVersionRange(condition[type]);
Expand Down Expand Up @@ -130,15 +128,25 @@ export const validateDeviceCompatibility = (
const deviceFwVariant = isBitcoinOnly(device) ? 'bitcoin-only' : 'regular';
const model = getDeviceModel(device);
const { vendor } = device.features;

return deviceConditions.some(
deviceCondition =>
deviceCondition.model.toLowerCase() === model.toLowerCase() &&
(deviceCondition.vendor.toLowerCase() === vendor.toLowerCase() ||
deviceCondition.vendor === '*') &&
semver.satisfies(deviceFwVersion, createVersionRange(deviceCondition.firmware)!) &&
(deviceCondition.variant.toLowerCase() === deviceFwVariant ||
deviceCondition.variant === '*'),
export const validateEnvironmentCompatibility = (
environmentCondition: Environment,
environment: EnvironmentType,
suiteVersion: string,
commitHash: string | undefined,
) => {
const { revision, desktop, web, mobile } = environmentCondition;

return (
validateVersionCompatibility(
{
desktop,
web,
mobile,
},
environment,
suiteVersion,
) &&
(revision === commitHash || revision === '*' || revision === undefined)
);
};

Expand All @@ -157,6 +165,7 @@ export const getValidMessages = (config: MessageSystem | null, options: Options)

const environment = getEnvironment();
const suiteVersion = transformVersionToSemverFormat(process.env.VERSION);
const commitHash = process.env.COMMITHASH;

return config.actions
.filter(
Expand All @@ -179,10 +188,11 @@ export const getValidMessages = (config: MessageSystem | null, options: Options)

if (
environmentCondition &&
!validateVersionCompatibility(
!validateEnvironmentCompatibility(
environmentCondition,
environment,
suiteVersion,
commitHash,
)
) {
return false;
Expand Down

0 comments on commit 33df244

Please sign in to comment.