@linked.cm/capacitor 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +35 -0
- package/lib/esm/hooks/useAutomaticUpdates.d.ts +11 -1
- package/lib/esm/hooks/useAutomaticUpdates.js +35 -5
- package/lib/esm/hooks/useAutomaticUpdates.js.map +1 -1
- package/lib/esm/hooks/useMandatoryNativeUpdate.d.ts +47 -0
- package/lib/esm/hooks/useMandatoryNativeUpdate.js +160 -0
- package/lib/esm/hooks/useMandatoryNativeUpdate.js.map +1 -0
- package/lib/esm/hooks/useReleaseGate.d.ts +39 -0
- package/lib/esm/hooks/useReleaseGate.js +116 -0
- package/lib/esm/hooks/useReleaseGate.js.map +1 -0
- package/lib/esm/index.d.ts +5 -0
- package/lib/esm/index.js +5 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/utils/releaseStatus.d.ts +34 -0
- package/lib/esm/utils/releaseStatus.js +81 -0
- package/lib/esm/utils/releaseStatus.js.map +1 -0
- package/lib/esm/utils/releaseStatusCache.d.ts +31 -0
- package/lib/esm/utils/releaseStatusCache.js +48 -0
- package/lib/esm/utils/releaseStatusCache.js.map +1 -0
- package/lib/esm/utils/releaseStatusClient.d.ts +16 -0
- package/lib/esm/utils/releaseStatusClient.js +46 -0
- package/lib/esm/utils/releaseStatusClient.js.map +1 -0
- package/package.json +7 -3
- package/readme.md +97 -0
- package/tests/automatic-updates-gating.test.tsx +68 -0
- package/tests/mandatory-native-update-hook.test.tsx +179 -0
- package/tests/mandatory-native-update.test.mjs +94 -0
- package/tests/release-gate.test.tsx +182 -0
- package/tests/release-status.test.mjs +154 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { renderHook, waitFor } from '@testing-library/react';
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
const mocks = vi.hoisted(() => ({
|
|
6
|
+
notifyAppReady: vi.fn(async () => undefined),
|
|
7
|
+
getChannel: vi.fn(async () => ({ channel: 'production', allowSet: false })),
|
|
8
|
+
setChannel: vi.fn(async () => undefined),
|
|
9
|
+
getLatest: vi.fn(async () => ({ error: 'no new version available' })),
|
|
10
|
+
getAppUpdateInfo: vi.fn(),
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
vi.mock('@capacitor/core', () => ({
|
|
14
|
+
Capacitor: {
|
|
15
|
+
getPlatform: () => 'ios',
|
|
16
|
+
isNativePlatform: () => true,
|
|
17
|
+
},
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock('@capgo/capacitor-updater', () => ({
|
|
21
|
+
CapacitorUpdater: {
|
|
22
|
+
notifyAppReady: (...args: unknown[]) => mocks.notifyAppReady(...args),
|
|
23
|
+
getChannel: (...args: unknown[]) => mocks.getChannel(...args),
|
|
24
|
+
setChannel: (...args: unknown[]) => mocks.setChannel(...args),
|
|
25
|
+
getLatest: (...args: unknown[]) => mocks.getLatest(...args),
|
|
26
|
+
addListener: vi.fn(async () => ({ remove: vi.fn() })),
|
|
27
|
+
download: vi.fn(),
|
|
28
|
+
set: vi.fn(),
|
|
29
|
+
reload: vi.fn(),
|
|
30
|
+
},
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
vi.mock('@capawesome/capacitor-app-update', () => ({
|
|
34
|
+
AppUpdate: {
|
|
35
|
+
getAppUpdateInfo: (...args: unknown[]) => mocks.getAppUpdateInfo(...args),
|
|
36
|
+
performImmediateUpdate: vi.fn(),
|
|
37
|
+
openAppStore: vi.fn(),
|
|
38
|
+
},
|
|
39
|
+
AppUpdateAvailability: { UPDATE_AVAILABLE: 2 },
|
|
40
|
+
AppUpdateResultCode: { OK: 0 },
|
|
41
|
+
}));
|
|
42
|
+
|
|
43
|
+
vi.mock('@capacitor/dialog', () => ({
|
|
44
|
+
Dialog: { confirm: vi.fn() },
|
|
45
|
+
}));
|
|
46
|
+
|
|
47
|
+
import { useAutomaticUpdates } from '../src/hooks/useAutomaticUpdates.js';
|
|
48
|
+
|
|
49
|
+
describe('useAutomaticUpdates gating', () => {
|
|
50
|
+
beforeEach(() => {
|
|
51
|
+
vi.clearAllMocks();
|
|
52
|
+
process.env.SITE_ROOT = 'https://example.test';
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('still confirms Capgo readiness when OTA work is paused', async () => {
|
|
56
|
+
renderHook(() =>
|
|
57
|
+
useAutomaticUpdates({
|
|
58
|
+
enabled: false,
|
|
59
|
+
checkNativeStoreVersion: false,
|
|
60
|
+
})
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
await waitFor(() => expect(mocks.notifyAppReady).toHaveBeenCalledOnce());
|
|
64
|
+
expect(mocks.getChannel).not.toHaveBeenCalled();
|
|
65
|
+
expect(mocks.getLatest).not.toHaveBeenCalled();
|
|
66
|
+
expect(mocks.getAppUpdateInfo).not.toHaveBeenCalled();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
import { useMandatoryNativeUpdate } from '../src/hooks/useMandatoryNativeUpdate.js';
|
|
5
|
+
|
|
6
|
+
const runtime = vi.hoisted(() => ({
|
|
7
|
+
platform: 'ios' as 'web' | 'ios' | 'android',
|
|
8
|
+
version: '4.3.6',
|
|
9
|
+
listeners: [] as Array<(event: { isActive: boolean }) => void>,
|
|
10
|
+
getInfo: vi.fn(async () => ({ version: '4.3.6' })),
|
|
11
|
+
openAppStore: vi.fn(),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
vi.mock('@capacitor/core', () => ({
|
|
15
|
+
Capacitor: {
|
|
16
|
+
getPlatform: () => runtime.platform,
|
|
17
|
+
isNativePlatform: () => runtime.platform !== 'web',
|
|
18
|
+
},
|
|
19
|
+
}));
|
|
20
|
+
|
|
21
|
+
vi.mock('@capacitor/app', () => ({
|
|
22
|
+
App: {
|
|
23
|
+
getInfo: () => runtime.getInfo(),
|
|
24
|
+
addListener: vi.fn(async (_event, listener) => {
|
|
25
|
+
runtime.listeners.push(listener);
|
|
26
|
+
return {
|
|
27
|
+
remove: async () => {
|
|
28
|
+
runtime.listeners = runtime.listeners.filter(
|
|
29
|
+
(candidate) => candidate !== listener
|
|
30
|
+
);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}),
|
|
34
|
+
},
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
vi.mock('@capawesome/capacitor-app-update', () => ({
|
|
38
|
+
AppUpdate: {
|
|
39
|
+
getAppUpdateInfo: vi.fn(),
|
|
40
|
+
performImmediateUpdate: vi.fn(),
|
|
41
|
+
openAppStore: (...args: unknown[]) => runtime.openAppStore(...args),
|
|
42
|
+
},
|
|
43
|
+
AppUpdateResultCode: { OK: 0 },
|
|
44
|
+
}));
|
|
45
|
+
|
|
46
|
+
describe('useMandatoryNativeUpdate lifecycle', () => {
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
runtime.platform = 'ios';
|
|
49
|
+
runtime.version = '4.3.6';
|
|
50
|
+
runtime.listeners = [];
|
|
51
|
+
runtime.getInfo.mockReset();
|
|
52
|
+
runtime.getInfo.mockImplementation(async () => ({
|
|
53
|
+
version: runtime.version,
|
|
54
|
+
}));
|
|
55
|
+
runtime.openAppStore.mockReset();
|
|
56
|
+
vi.clearAllMocks();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('reads App.getInfo on mount and stays not-native on web', async () => {
|
|
60
|
+
runtime.platform = 'web';
|
|
61
|
+
const { result } = renderHook(() =>
|
|
62
|
+
useMandatoryNativeUpdate({ fallbackMinimumVersion: '4.3.4' })
|
|
63
|
+
);
|
|
64
|
+
expect(result.current.state.status).toBe('not-native');
|
|
65
|
+
expect(runtime.getInfo).not.toHaveBeenCalled();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('marks an outdated installed binary as required', async () => {
|
|
69
|
+
runtime.version = '4.3.5';
|
|
70
|
+
const { result } = renderHook(() =>
|
|
71
|
+
useMandatoryNativeUpdate({
|
|
72
|
+
fallbackMinimumVersion: '4.3.4',
|
|
73
|
+
minimumVersion: '4.4.0',
|
|
74
|
+
iosAppStoreId: '1234567890',
|
|
75
|
+
})
|
|
76
|
+
);
|
|
77
|
+
await waitFor(() => expect(result.current.state.status).toBe('required'));
|
|
78
|
+
expect(runtime.getInfo).toHaveBeenCalled();
|
|
79
|
+
expect(result.current.state).toMatchObject({
|
|
80
|
+
currentVersion: '4.3.5',
|
|
81
|
+
minimumVersion: '4.4.0',
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('rechecks on foreground and when the minimum changes', async () => {
|
|
86
|
+
runtime.version = '4.3.6';
|
|
87
|
+
const { result, rerender } = renderHook(
|
|
88
|
+
({ minimumVersion }) =>
|
|
89
|
+
useMandatoryNativeUpdate({
|
|
90
|
+
fallbackMinimumVersion: '4.3.4',
|
|
91
|
+
minimumVersion,
|
|
92
|
+
iosAppStoreId: '1234567890',
|
|
93
|
+
}),
|
|
94
|
+
{ initialProps: { minimumVersion: '4.3.4' } }
|
|
95
|
+
);
|
|
96
|
+
await waitFor(() => expect(result.current.state.status).toBe('supported'));
|
|
97
|
+
|
|
98
|
+
runtime.version = '4.3.6';
|
|
99
|
+
rerender({ minimumVersion: '4.4.0' });
|
|
100
|
+
await waitFor(() => expect(result.current.state.status).toBe('required'));
|
|
101
|
+
|
|
102
|
+
runtime.version = '4.4.0';
|
|
103
|
+
await act(async () => {
|
|
104
|
+
runtime.listeners.forEach((listener) => listener({ isActive: true }));
|
|
105
|
+
});
|
|
106
|
+
await waitFor(() => expect(result.current.state.status).toBe('supported'));
|
|
107
|
+
expect(runtime.getInfo.mock.calls.length).toBeGreaterThanOrEqual(3);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('ignores a stale App.getInfo result after a newer check', async () => {
|
|
111
|
+
let resolveOld!: (value: { version: string }) => void;
|
|
112
|
+
let resolveNew!: (value: { version: string }) => void;
|
|
113
|
+
runtime.getInfo
|
|
114
|
+
.mockImplementationOnce(
|
|
115
|
+
() =>
|
|
116
|
+
new Promise((resolve) => {
|
|
117
|
+
resolveOld = resolve;
|
|
118
|
+
})
|
|
119
|
+
)
|
|
120
|
+
.mockImplementationOnce(
|
|
121
|
+
() =>
|
|
122
|
+
new Promise((resolve) => {
|
|
123
|
+
resolveNew = resolve;
|
|
124
|
+
})
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const { result } = renderHook(() =>
|
|
128
|
+
useMandatoryNativeUpdate({
|
|
129
|
+
fallbackMinimumVersion: '4.3.4',
|
|
130
|
+
minimumVersion: '4.4.0',
|
|
131
|
+
iosAppStoreId: '1234567890',
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
await act(async () => {
|
|
135
|
+
result.current.retryCheck();
|
|
136
|
+
});
|
|
137
|
+
await act(async () => {
|
|
138
|
+
resolveNew({ version: '4.4.0' });
|
|
139
|
+
});
|
|
140
|
+
await waitFor(() => expect(result.current.state.status).toBe('supported'));
|
|
141
|
+
await act(async () => {
|
|
142
|
+
resolveOld({ version: '4.3.0' });
|
|
143
|
+
});
|
|
144
|
+
expect(result.current.state.status).toBe('supported');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('surfaces getInfo failures and recovers through retry', async () => {
|
|
148
|
+
runtime.getInfo
|
|
149
|
+
.mockRejectedValueOnce(new Error('unavailable'))
|
|
150
|
+
.mockResolvedValueOnce({ version: '4.4.0' });
|
|
151
|
+
const { result } = renderHook(() =>
|
|
152
|
+
useMandatoryNativeUpdate({
|
|
153
|
+
fallbackMinimumVersion: '4.3.4',
|
|
154
|
+
minimumVersion: '4.4.0',
|
|
155
|
+
})
|
|
156
|
+
);
|
|
157
|
+
await waitFor(() => expect(result.current.state.status).toBe('error'));
|
|
158
|
+
await act(async () => {
|
|
159
|
+
await result.current.retryCheck();
|
|
160
|
+
});
|
|
161
|
+
await waitFor(() => expect(result.current.state.status).toBe('supported'));
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('opens the App Store for a required iOS update', async () => {
|
|
165
|
+
runtime.version = '4.3.5';
|
|
166
|
+
const { result } = renderHook(() =>
|
|
167
|
+
useMandatoryNativeUpdate({
|
|
168
|
+
fallbackMinimumVersion: '4.3.4',
|
|
169
|
+
minimumVersion: '4.4.0',
|
|
170
|
+
iosAppStoreId: '1234567890',
|
|
171
|
+
})
|
|
172
|
+
);
|
|
173
|
+
await waitFor(() => expect(result.current.state.status).toBe('required'));
|
|
174
|
+
await act(async () => {
|
|
175
|
+
await result.current.requestUpdate();
|
|
176
|
+
});
|
|
177
|
+
expect(runtime.openAppStore).toHaveBeenCalledWith({ appId: '1234567890' });
|
|
178
|
+
});
|
|
179
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
compareNativeVersions,
|
|
6
|
+
effectiveMinimumVersion,
|
|
7
|
+
nativeUpdateRequirement,
|
|
8
|
+
openRequiredNativeUpdate,
|
|
9
|
+
} from '../lib/esm/hooks/useMandatoryNativeUpdate.js';
|
|
10
|
+
import { resolveAutomaticUpdateOptions } from '../lib/esm/hooks/useAutomaticUpdates.js';
|
|
11
|
+
|
|
12
|
+
test('compares stable native versions numerically', () => {
|
|
13
|
+
assert.equal(compareNativeVersions('4.10.0', '4.9.9'), 1);
|
|
14
|
+
assert.equal(compareNativeVersions('4.3.6', '4.3.6'), 0);
|
|
15
|
+
assert.equal(compareNativeVersions('4.3.5', '4.3.6'), -1);
|
|
16
|
+
assert.throws(
|
|
17
|
+
() => compareNativeVersions('4.3.6-beta.1', '4.3.6'),
|
|
18
|
+
/stable X\.Y\.Z/
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('server minimum cannot lower the compiled fallback', () => {
|
|
23
|
+
assert.equal(effectiveMinimumVersion('4.3.4', '4.3.0'), '4.3.4');
|
|
24
|
+
assert.equal(effectiveMinimumVersion('4.3.4', '4.4.0'), '4.4.0');
|
|
25
|
+
assert.equal(effectiveMinimumVersion('4.3.4'), '4.3.4');
|
|
26
|
+
assert.equal(effectiveMinimumVersion('4.3.4', 'invalid'), '4.3.4');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('installed binary is checked against the effective minimum', () => {
|
|
30
|
+
assert.deepEqual(nativeUpdateRequirement('4.3.6', '4.3.4', '4.4.0'), {
|
|
31
|
+
supported: false,
|
|
32
|
+
minimumVersion: '4.4.0',
|
|
33
|
+
});
|
|
34
|
+
assert.deepEqual(nativeUpdateRequirement('4.3.6', '4.3.4', '4.3.0'), {
|
|
35
|
+
supported: true,
|
|
36
|
+
minimumVersion: '4.3.4',
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('automatic updates preserve defaults and accept explicit gating', () => {
|
|
41
|
+
assert.deepEqual(resolveAutomaticUpdateOptions(), {
|
|
42
|
+
enabled: true,
|
|
43
|
+
checkNativeStoreVersion: true,
|
|
44
|
+
});
|
|
45
|
+
assert.deepEqual(
|
|
46
|
+
resolveAutomaticUpdateOptions({
|
|
47
|
+
enabled: false,
|
|
48
|
+
checkNativeStoreVersion: false,
|
|
49
|
+
}),
|
|
50
|
+
{ enabled: false, checkNativeStoreVersion: false }
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test('Android immediate update avoids opening Google Play when successful', async () => {
|
|
55
|
+
const calls = [];
|
|
56
|
+
await openRequiredNativeUpdate(
|
|
57
|
+
{ platform: 'android', androidPackageName: 'example.app' },
|
|
58
|
+
{
|
|
59
|
+
getAppUpdateInfo: async () => ({ immediateUpdateAllowed: true }),
|
|
60
|
+
performImmediateUpdate: async () => {
|
|
61
|
+
calls.push('immediate');
|
|
62
|
+
return { code: 0 };
|
|
63
|
+
},
|
|
64
|
+
openAppStore: async () => calls.push('store'),
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
assert.deepEqual(calls, ['immediate']);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('Android cancellation falls back to Google Play', async () => {
|
|
71
|
+
const stores = [];
|
|
72
|
+
await openRequiredNativeUpdate(
|
|
73
|
+
{ platform: 'android', androidPackageName: 'example.app' },
|
|
74
|
+
{
|
|
75
|
+
getAppUpdateInfo: async () => ({ immediateUpdateAllowed: true }),
|
|
76
|
+
performImmediateUpdate: async () => ({ code: 1 }),
|
|
77
|
+
openAppStore: async (options) => stores.push(options),
|
|
78
|
+
}
|
|
79
|
+
);
|
|
80
|
+
assert.deepEqual(stores, [{ androidPackageName: 'example.app' }]);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('iOS opens the configured numeric App Store id', async () => {
|
|
84
|
+
const stores = [];
|
|
85
|
+
await openRequiredNativeUpdate(
|
|
86
|
+
{ platform: 'ios', iosAppStoreId: '1234567890' },
|
|
87
|
+
{
|
|
88
|
+
getAppUpdateInfo: async () => ({}),
|
|
89
|
+
performImmediateUpdate: async () => ({ code: 0 }),
|
|
90
|
+
openAppStore: async (options) => stores.push(options),
|
|
91
|
+
}
|
|
92
|
+
);
|
|
93
|
+
assert.deepEqual(stores, [{ appId: '1234567890' }]);
|
|
94
|
+
});
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
3
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
import { useReleaseGate } from '../src/hooks/useReleaseGate.js';
|
|
5
|
+
import type { ReleaseStatus } from '../src/utils/releaseStatus.js';
|
|
6
|
+
|
|
7
|
+
const runtime = vi.hoisted(() => ({
|
|
8
|
+
platform: 'web' as 'web' | 'ios' | 'android',
|
|
9
|
+
version: '4.3.6',
|
|
10
|
+
listeners: [] as Array<(event: { isActive: boolean }) => void>,
|
|
11
|
+
}));
|
|
12
|
+
|
|
13
|
+
vi.mock('@capacitor/core', () => ({
|
|
14
|
+
Capacitor: {
|
|
15
|
+
getPlatform: () => runtime.platform,
|
|
16
|
+
isNativePlatform: () => runtime.platform !== 'web',
|
|
17
|
+
},
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
vi.mock('@capacitor/app', () => ({
|
|
21
|
+
App: {
|
|
22
|
+
getInfo: vi.fn(async () => ({ version: runtime.version })),
|
|
23
|
+
addListener: vi.fn(async (_event, listener) => {
|
|
24
|
+
runtime.listeners.push(listener);
|
|
25
|
+
return {
|
|
26
|
+
remove: async () => {
|
|
27
|
+
runtime.listeners = runtime.listeners.filter(
|
|
28
|
+
(candidate) => candidate !== listener
|
|
29
|
+
);
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}),
|
|
33
|
+
},
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
vi.mock('@capawesome/capacitor-app-update', () => ({
|
|
37
|
+
AppUpdate: {
|
|
38
|
+
getAppUpdateInfo: vi.fn(),
|
|
39
|
+
performImmediateUpdate: vi.fn(),
|
|
40
|
+
openAppStore: vi.fn(),
|
|
41
|
+
},
|
|
42
|
+
AppUpdateResultCode: { OK: 0 },
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
const supported: ReleaseStatus = {
|
|
46
|
+
minimumVersion: '4.3.6',
|
|
47
|
+
maintenanceMode: false,
|
|
48
|
+
maintenancePlatform: 'all',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
function deferred<T>() {
|
|
52
|
+
let resolve!: (value: T) => void;
|
|
53
|
+
let reject!: (reason: unknown) => void;
|
|
54
|
+
const promise = new Promise<T>((res, rej) => {
|
|
55
|
+
resolve = res;
|
|
56
|
+
reject = rej;
|
|
57
|
+
});
|
|
58
|
+
return { promise, resolve, reject };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let warnSpy: ReturnType<typeof vi.spyOn>;
|
|
62
|
+
|
|
63
|
+
beforeAll(() => {
|
|
64
|
+
warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
afterAll(() => {
|
|
68
|
+
warnSpy.mockRestore();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
beforeEach(() => {
|
|
72
|
+
runtime.platform = 'web';
|
|
73
|
+
runtime.version = '4.3.6';
|
|
74
|
+
runtime.listeners = [];
|
|
75
|
+
vi.clearAllMocks();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('useReleaseGate lifecycle', () => {
|
|
79
|
+
it('renders web as supported while status loads, then applies maintenance', async () => {
|
|
80
|
+
const request = deferred<ReleaseStatus>();
|
|
81
|
+
const { result } = renderHook(() =>
|
|
82
|
+
useReleaseGate({
|
|
83
|
+
loadStatus: () => request.promise,
|
|
84
|
+
fallbackMinimumVersion: '4.3.6',
|
|
85
|
+
})
|
|
86
|
+
);
|
|
87
|
+
expect(result.current.state.status).toBe('supported');
|
|
88
|
+
expect(result.current.otaEnabled).toBe(true);
|
|
89
|
+
request.resolve({
|
|
90
|
+
...supported,
|
|
91
|
+
maintenanceMode: true,
|
|
92
|
+
maintenancePlatform: 'web',
|
|
93
|
+
});
|
|
94
|
+
await waitFor(() => expect(result.current.state.status).toBe('maintenance'));
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('prioritizes native maintenance over a required binary update', async () => {
|
|
98
|
+
runtime.platform = 'android';
|
|
99
|
+
runtime.version = '4.3.5';
|
|
100
|
+
const { result } = renderHook(() =>
|
|
101
|
+
useReleaseGate({
|
|
102
|
+
loadStatus: async () => ({
|
|
103
|
+
...supported,
|
|
104
|
+
maintenanceMode: true,
|
|
105
|
+
maintenancePlatform: 'native',
|
|
106
|
+
}),
|
|
107
|
+
fallbackMinimumVersion: '4.3.6',
|
|
108
|
+
})
|
|
109
|
+
);
|
|
110
|
+
await waitFor(() => expect(result.current.state.status).toBe('maintenance'));
|
|
111
|
+
expect(result.current.otaEnabled).toBe(false);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('refreshes status and installed version on native foreground', async () => {
|
|
115
|
+
runtime.platform = 'ios';
|
|
116
|
+
let minimumVersion = '4.3.6';
|
|
117
|
+
const loadStatus = vi.fn(async () => ({ ...supported, minimumVersion }));
|
|
118
|
+
const { result } = renderHook(() =>
|
|
119
|
+
useReleaseGate({
|
|
120
|
+
loadStatus,
|
|
121
|
+
fallbackMinimumVersion: '4.3.6',
|
|
122
|
+
})
|
|
123
|
+
);
|
|
124
|
+
await waitFor(() => expect(result.current.otaEnabled).toBe(true));
|
|
125
|
+
minimumVersion = '4.4.0';
|
|
126
|
+
await act(async () => {
|
|
127
|
+
runtime.listeners.forEach((listener) => listener({ isActive: true }));
|
|
128
|
+
});
|
|
129
|
+
await waitFor(() => expect(result.current.state.status).toBe('required'));
|
|
130
|
+
expect(loadStatus).toHaveBeenCalledTimes(2);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('ignores a stale status request completed after retry', async () => {
|
|
134
|
+
const oldRequest = deferred<ReleaseStatus>();
|
|
135
|
+
const newRequest = deferred<ReleaseStatus>();
|
|
136
|
+
const loadStatus = vi
|
|
137
|
+
.fn<() => Promise<ReleaseStatus>>()
|
|
138
|
+
.mockReturnValueOnce(oldRequest.promise)
|
|
139
|
+
.mockReturnValueOnce(newRequest.promise);
|
|
140
|
+
const { result } = renderHook(() =>
|
|
141
|
+
useReleaseGate({
|
|
142
|
+
loadStatus,
|
|
143
|
+
fallbackMinimumVersion: '4.3.6',
|
|
144
|
+
})
|
|
145
|
+
);
|
|
146
|
+
let retry!: Promise<void>;
|
|
147
|
+
act(() => {
|
|
148
|
+
retry = result.current.retryCheck();
|
|
149
|
+
});
|
|
150
|
+
newRequest.resolve({
|
|
151
|
+
...supported,
|
|
152
|
+
maintenanceMode: true,
|
|
153
|
+
maintenancePlatform: 'web',
|
|
154
|
+
});
|
|
155
|
+
await act(async () => retry);
|
|
156
|
+
expect(result.current.state.status).toBe('maintenance');
|
|
157
|
+
oldRequest.resolve(supported);
|
|
158
|
+
await act(async () => oldRequest.promise);
|
|
159
|
+
expect(result.current.state.status).toBe('maintenance');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('recovers from an initial status error through manual retry', async () => {
|
|
163
|
+
const loadStatus = vi
|
|
164
|
+
.fn<() => Promise<ReleaseStatus>>()
|
|
165
|
+
.mockRejectedValueOnce(new Error('offline'))
|
|
166
|
+
.mockResolvedValueOnce({
|
|
167
|
+
...supported,
|
|
168
|
+
maintenanceMode: true,
|
|
169
|
+
maintenancePlatform: 'web',
|
|
170
|
+
});
|
|
171
|
+
const { result } = renderHook(() =>
|
|
172
|
+
useReleaseGate({
|
|
173
|
+
loadStatus,
|
|
174
|
+
fallbackMinimumVersion: '4.3.6',
|
|
175
|
+
})
|
|
176
|
+
);
|
|
177
|
+
await waitFor(() => expect(loadStatus).toHaveBeenCalledTimes(1));
|
|
178
|
+
await act(async () => result.current.retryCheck());
|
|
179
|
+
expect(result.current.state.status).toBe('maintenance');
|
|
180
|
+
});
|
|
181
|
+
});
|
|
182
|
+
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
isMaintenanceApplicable,
|
|
6
|
+
normalizeReleaseStatus,
|
|
7
|
+
pickReleaseStatus,
|
|
8
|
+
toStatusResponse,
|
|
9
|
+
tryNormalizeReleaseStatus,
|
|
10
|
+
} from '../lib/esm/utils/releaseStatus.js';
|
|
11
|
+
import {
|
|
12
|
+
createReleaseStatusHandler,
|
|
13
|
+
ReleaseStatusCache,
|
|
14
|
+
} from '../lib/esm/utils/releaseStatusCache.js';
|
|
15
|
+
import { ReleaseStatusClient } from '../lib/esm/utils/releaseStatusClient.js';
|
|
16
|
+
|
|
17
|
+
const supported = {
|
|
18
|
+
minimumVersion: '4.3.6',
|
|
19
|
+
maintenanceMode: false,
|
|
20
|
+
maintenancePlatform: 'all',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
test('release status normalizes optional fields and rejects invalid versions', () => {
|
|
24
|
+
assert.deepEqual(normalizeReleaseStatus({ minimumVersion: ' 4.3.6 ' }), supported);
|
|
25
|
+
assert.throws(
|
|
26
|
+
() => normalizeReleaseStatus({ minimumVersion: '4.3.6-beta.1' }),
|
|
27
|
+
/stable X\.Y\.Z/
|
|
28
|
+
);
|
|
29
|
+
assert.throws(
|
|
30
|
+
() =>
|
|
31
|
+
normalizeReleaseStatus(
|
|
32
|
+
{ minimumVersion: null },
|
|
33
|
+
{ defaultMinimumVersion: '4.3.4' }
|
|
34
|
+
),
|
|
35
|
+
/stable X\.Y\.Z/
|
|
36
|
+
);
|
|
37
|
+
assert.deepEqual(
|
|
38
|
+
normalizeReleaseStatus({}, { defaultMinimumVersion: '4.3.4' }),
|
|
39
|
+
{
|
|
40
|
+
minimumVersion: '4.3.4',
|
|
41
|
+
maintenanceMode: false,
|
|
42
|
+
maintenancePlatform: 'all',
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('maintenance targeting distinguishes web, native, and exact platforms', () => {
|
|
48
|
+
assert.equal(
|
|
49
|
+
isMaintenanceApplicable(
|
|
50
|
+
{ maintenanceMode: true, maintenancePlatform: 'native' },
|
|
51
|
+
'web'
|
|
52
|
+
),
|
|
53
|
+
false
|
|
54
|
+
);
|
|
55
|
+
assert.equal(
|
|
56
|
+
isMaintenanceApplicable(
|
|
57
|
+
{ maintenanceMode: true, maintenancePlatform: 'native' },
|
|
58
|
+
'android'
|
|
59
|
+
),
|
|
60
|
+
true
|
|
61
|
+
);
|
|
62
|
+
assert.equal(
|
|
63
|
+
isMaintenanceApplicable(
|
|
64
|
+
{ maintenanceMode: true, maintenancePlatform: 'ios' },
|
|
65
|
+
'android'
|
|
66
|
+
),
|
|
67
|
+
false
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('server cache retains the last valid snapshot and handler disables caching', () => {
|
|
72
|
+
let value = { ...supported, minimumVersion: '4.4.0' };
|
|
73
|
+
const warnings = [];
|
|
74
|
+
const cache = new ReleaseStatusCache({
|
|
75
|
+
initialStatus: supported,
|
|
76
|
+
readStatus: () => value,
|
|
77
|
+
log: (...args) => warnings.push(args),
|
|
78
|
+
});
|
|
79
|
+
assert.equal(cache.load().minimumVersion, '4.4.0');
|
|
80
|
+
value = { ...supported, minimumVersion: 'invalid' };
|
|
81
|
+
assert.equal(cache.load().minimumVersion, '4.4.0');
|
|
82
|
+
assert.equal(warnings.length, 1);
|
|
83
|
+
|
|
84
|
+
const headers = {};
|
|
85
|
+
let statusCode;
|
|
86
|
+
let body;
|
|
87
|
+
createReleaseStatusHandler(cache)(null, {
|
|
88
|
+
setHeader: (name, headerValue) => {
|
|
89
|
+
headers[name] = headerValue;
|
|
90
|
+
},
|
|
91
|
+
status: (code) => {
|
|
92
|
+
statusCode = code;
|
|
93
|
+
return { json: (next) => (body = next) };
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
assert.equal(statusCode, 200);
|
|
97
|
+
assert.equal(headers['Cache-Control'], 'no-store');
|
|
98
|
+
assert.deepEqual(body, cache.getStatus());
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('status client deduplicates concurrent requests and retains valid data', async () => {
|
|
102
|
+
let calls = 0;
|
|
103
|
+
let fail = false;
|
|
104
|
+
const client = new ReleaseStatusClient({
|
|
105
|
+
url: '/status',
|
|
106
|
+
fetch: async () => {
|
|
107
|
+
calls += 1;
|
|
108
|
+
if (fail) throw new Error('offline');
|
|
109
|
+
return {
|
|
110
|
+
ok: true,
|
|
111
|
+
json: async () => supported,
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
const [first, second] = await Promise.all([client.load(), client.load()]);
|
|
116
|
+
assert.deepEqual(first, supported);
|
|
117
|
+
assert.deepEqual(second, supported);
|
|
118
|
+
assert.equal(calls, 1);
|
|
119
|
+
fail = true;
|
|
120
|
+
assert.deepEqual(await client.load(), supported);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
test('tryNormalizeReleaseStatus reports validation failures without throwing', () => {
|
|
125
|
+
assert.deepEqual(tryNormalizeReleaseStatus(supported), {
|
|
126
|
+
ok: true,
|
|
127
|
+
value: supported,
|
|
128
|
+
});
|
|
129
|
+
const failed = tryNormalizeReleaseStatus({ minimumVersion: '4' });
|
|
130
|
+
assert.equal(failed.ok, false);
|
|
131
|
+
assert.match(failed.error, /stable X\.Y\.Z/);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test('pickReleaseStatus and toStatusResponse normalize owned fields only', () => {
|
|
135
|
+
assert.deepEqual(
|
|
136
|
+
pickReleaseStatus(
|
|
137
|
+
{
|
|
138
|
+
minimumVersion: '4.3.6',
|
|
139
|
+
maintenanceMode: true,
|
|
140
|
+
maintenancePlatform: 'native',
|
|
141
|
+
maintenanceMessage: ' later ',
|
|
142
|
+
unrelated: true,
|
|
143
|
+
},
|
|
144
|
+
{ defaultMinimumVersion: '4.3.4' }
|
|
145
|
+
),
|
|
146
|
+
{
|
|
147
|
+
minimumVersion: '4.3.6',
|
|
148
|
+
maintenanceMode: true,
|
|
149
|
+
maintenancePlatform: 'native',
|
|
150
|
+
maintenanceMessage: 'later',
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
assert.deepEqual(toStatusResponse(supported), supported);
|
|
154
|
+
});
|