📄 src/web3/wallet/walletMeta.test.ts
D-OPEN SOVEREIGN
1import { describe, it, expect, afterEach, vi } from 'vitest';
2import { walletMeta } from './walletMeta';
3import { Browser } from './browsers';
4
5afterEach(() => {
6 delete (globalThis as any).window;
7 vi.unstubAllGlobals();
8});
9
10describe('walletMeta.metamask.getInstallUrl', () => {
11 it('returns the Play Store URL on Android, regardless of the customBrowser hint', () => {
12 vi.stubGlobal('navigator', { userAgent: 'Mozilla/5.0 (Linux; Android 13; Pixel 7) Chrome/115.0.0.0 Mobile Safari/537.36' });
13 const url = walletMeta.metamask.getInstallUrl(Browser.firefox);
14 expect(url).toContain('play.google.com');
15 });
16
17 it('returns the App Store URL on iOS', () => {
18 vi.stubGlobal('navigator', { userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_5 like Mac OS X) Version/16.5 Mobile/15E148 Safari/604.1' });
19 const url = walletMeta.metamask.getInstallUrl();
20 expect(url).toContain('apps.apple.com');
21 });
22
23 it('returns the Chrome Web Store URL for chromium', () => {
24 const url = walletMeta.metamask.getInstallUrl(Browser.chromium);
25 expect(url).toContain('chrome.google.com');
26 });
27
28 it('returns the Chrome Web Store URL for brave (shares the chromium store)', () => {
29 const url = walletMeta.metamask.getInstallUrl(Browser.brave);
30 expect(url).toContain('chrome.google.com');
31 });
32
33 it('returns the Firefox add-ons URL for firefox', () => {
34 const url = walletMeta.metamask.getInstallUrl(Browser.firefox);
35 expect(url).toContain('addons.mozilla.org');
36 });
37
38 it('returns the Opera add-ons URL for opera', () => {
39 const url = walletMeta.metamask.getInstallUrl(Browser.opera);
40 expect(url).toContain('addons.opera.com');
41 });
42
43 it('returns the Edge add-ons URL for edge', () => {
44 const url = walletMeta.metamask.getInstallUrl(Browser.edge);
45 expect(url).toContain('microsoftedge.microsoft.com');
46 });
47
48 it('throws for a browser with no known MetaMask install page (e.g. Safari)', () => {
49 expect(() => walletMeta.metamask.getInstallUrl(Browser.safari)).toThrow(
50 /Browser safari is not supported/
51 );
52 });
53
54 it('throws for vivaldi, yandex, and palemoon — no install URL is mapped for them either', () => {
55 expect(() => walletMeta.metamask.getInstallUrl(Browser.vivaldi)).toThrow();
56 expect(() => walletMeta.metamask.getInstallUrl(Browser.yandex)).toThrow();
57 expect(() => walletMeta.metamask.getInstallUrl(Browser.palemoon)).toThrow();
58 });
59
60 it('falls back to the desktop chromium URL for an empty/unrecognized user agent (e.g. SSR/Node)', () => {
61 vi.stubGlobal('navigator', { userAgent: '' });
62 const url = walletMeta.metamask.getInstallUrl();
63 expect(url).toContain('chrome.google.com');
64 });
65});
66
67describe('walletMeta.metamask.isInstalled', () => {
68 it('is false when there is no window at all', () => {
69 delete (globalThis as any).window;
70 expect(walletMeta.metamask.isInstalled()).toBe(false);
71 });
72
73 it('is false when window exists but has no ethereum provider', () => {
74 (globalThis as any).window = {};
75 expect(walletMeta.metamask.isInstalled()).toBe(false);
76 });
77
78 it('is false when an ethereum provider exists but is not MetaMask', () => {
79 (globalThis as any).window = { ethereum: { isMetaMask: false } };
80 expect(walletMeta.metamask.isInstalled()).toBe(false);
81 });
82
83 it('is true when window.ethereum.isMetaMask is true', () => {
84 (globalThis as any).window = { ethereum: { isMetaMask: true } };
85 expect(walletMeta.metamask.isInstalled()).toBe(true);
86 });
87});
88
89describe('walletMeta.metamask.isInstallable', () => {
90 it('is true whenever getInstallUrl() resolves without throwing (the common case, given the chromium default fallback)', () => {
91 expect(walletMeta.metamask.isInstallable()).toBe(true);
92 });
93});
94
95describe('walletMeta.private_key', () => {
96 it('is always "installed" and never "installable" (there is nothing to install)', () => {
97 expect(walletMeta.private_key.isInstalled()).toBe(true);
98 expect(walletMeta.private_key.isInstallable()).toBe(false);
99 });
100
101 it('getInstallUrl returns an empty string', () => {
102 expect(walletMeta.private_key.getInstallUrl()).toBe('');
103 });
104});
105