-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatxp-utils.test.ts
More file actions
201 lines (157 loc) · 6.38 KB
/
Copy pathatxp-utils.test.ts
File metadata and controls
201 lines (157 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { Request } from 'express';
// Mock the ATXP client module
vi.mock('@atxp/client', () => ({
ATXPAccount: vi.fn().mockImplementation(() => ({ accountId: 'test-account' }))
}));
import { getATXPConnectionString, findATXPAccount, validateATXPConnectionString } from './atxp-utils.js';
import { ATXPAccount } from '@atxp/client';
describe('ATXP Utils', () => {
beforeEach(() => {
// Clear environment variables before each test
delete process.env.ATXP_CONNECTION_STRING;
// Reset mocks
vi.clearAllMocks();
});
describe('getATXPConnectionString', () => {
it('should return connection string from header when present', () => {
const mockReq = {
headers: {
'x-atxp-connection-string': 'header-connection-string'
}
} as Partial<Request> as Request;
process.env.ATXP_CONNECTION_STRING = 'env-connection-string';
const result = getATXPConnectionString(mockReq);
expect(result).toBe('header-connection-string');
});
it('should return connection string from environment variable when header is not present', () => {
const mockReq = {
headers: {}
} as Partial<Request> as Request;
process.env.ATXP_CONNECTION_STRING = 'env-connection-string';
const result = getATXPConnectionString(mockReq);
expect(result).toBe('env-connection-string');
});
it('should return connection string from environment variable when header is empty', () => {
const mockReq = {
headers: {
'x-atxp-connection-string': ''
}
} as Partial<Request> as Request;
process.env.ATXP_CONNECTION_STRING = 'env-connection-string';
const result = getATXPConnectionString(mockReq);
expect(result).toBe('env-connection-string');
});
it('should throw error when neither header nor environment variable is present', () => {
const mockReq = {
headers: {}
} as Partial<Request> as Request;
expect(() => getATXPConnectionString(mockReq)).toThrow(
'ATXP connection string not found. Provide either x-atxp-connection-string header or ATXP_CONNECTION_STRING environment variable'
);
});
it('should throw error when header is empty and environment variable is not set', () => {
const mockReq = {
headers: {
'x-atxp-connection-string': ''
}
} as Partial<Request> as Request;
expect(() => getATXPConnectionString(mockReq)).toThrow(
'ATXP connection string not found. Provide either x-atxp-connection-string header or ATXP_CONNECTION_STRING environment variable'
);
});
it('should handle undefined header values', () => {
const mockReq = {
headers: {
'x-atxp-connection-string': undefined
}
} as Partial<Request> as Request;
process.env.ATXP_CONNECTION_STRING = 'env-connection-string';
const result = getATXPConnectionString(mockReq);
expect(result).toBe('env-connection-string');
});
it('should prioritize header over environment variable even when both are set', () => {
const mockReq = {
headers: {
'x-atxp-connection-string': 'header-wins'
}
} as Partial<Request> as Request;
process.env.ATXP_CONNECTION_STRING = 'env-loses';
const result = getATXPConnectionString(mockReq);
expect(result).toBe('header-wins');
});
});
describe('findATXPAccount', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should call ATXPAccount constructor with correct parameters', () => {
const connectionString = 'test-connection-string';
const result = findATXPAccount(connectionString);
expect(ATXPAccount).toHaveBeenCalledWith(connectionString, { network: 'base' });
expect(result).toEqual({ accountId: 'test-account' });
});
it('should return the ATXPAccount instance', () => {
const connectionString = 'any-connection-string';
const result = findATXPAccount(connectionString);
expect(ATXPAccount).toHaveBeenCalledWith(connectionString, { network: 'base' });
expect(result).toEqual({ accountId: 'test-account' });
});
});
describe('validateATXPConnectionString', () => {
beforeEach(() => {
vi.clearAllMocks();
delete process.env.ATXP_CONNECTION_STRING;
});
it('should return valid true when connection string is available and account creation succeeds', () => {
const mockReq = {
headers: {
'x-atxp-connection-string': 'valid-connection-string'
}
} as Partial<Request> as Request;
const result = validateATXPConnectionString(mockReq);
expect(result).toEqual({
isValid: true
});
expect(ATXPAccount).toHaveBeenCalledWith('valid-connection-string', { network: 'base' });
});
it('should return valid true when using environment variable', () => {
process.env.ATXP_CONNECTION_STRING = 'env-connection-string';
const mockReq = {
headers: {}
} as Partial<Request> as Request;
const result = validateATXPConnectionString(mockReq);
expect(result).toEqual({
isValid: true
});
expect(ATXPAccount).toHaveBeenCalledWith('env-connection-string', { network: 'base' });
});
it('should return valid false when no connection string is available', () => {
const mockReq = {
headers: {}
} as Partial<Request> as Request;
const result = validateATXPConnectionString(mockReq);
expect(result).toEqual({
isValid: false,
error: 'ATXP connection string not found. Provide either x-atxp-connection-string header or ATXP_CONNECTION_STRING environment variable'
});
expect(ATXPAccount).not.toHaveBeenCalled();
});
it('should return valid false when ATXPAccount constructor throws an error', () => {
const mockReq = {
headers: {
'x-atxp-connection-string': 'invalid-connection-string'
}
} as Partial<Request> as Request;
// Mock ATXPAccount to throw an error
vi.mocked(ATXPAccount).mockImplementationOnce(() => {
throw new Error('Invalid connection string format');
});
const result = validateATXPConnectionString(mockReq);
expect(result).toEqual({
isValid: false,
error: 'Invalid connection string format'
});
});
});
});