-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathutils.test.ts
More file actions
127 lines (108 loc) · 4.19 KB
/
utils.test.ts
File metadata and controls
127 lines (108 loc) · 4.19 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
import { expect } from 'chai';
import Int64 from 'node-int64';
import { TJobExecutionStatus, TProgressUpdateResp } from '../../../thrift/TCLIService_types';
import { buildUserAgentString, definedOrError, formatProgress, ProgressUpdateTransformer } from '../../../lib/utils';
const progressUpdateResponseStub: TProgressUpdateResp = {
headerNames: [],
rows: [],
progressedPercentage: 0,
status: TJobExecutionStatus.NOT_AVAILABLE,
footerSummary: '',
startTime: new Int64(0),
};
describe('buildUserAgentString', () => {
// It should follow https://www.rfc-editor.org/rfc/rfc7231#section-5.5.3 and
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent
//
// UserAgent ::= <ProductName> '/' <ProductVersion> '(' <Comment> ')'
// where:
// ProductName ::= 'NodejsDatabricksSqlConnector'
// ProductVersion ::= three period-separated digits
// <Comment> ::= [ <userAgentEntry> ';' ] 'Node.js' <NodeJsVersion> ';' <OSPlatform> <OSVersion>
//
// Examples:
// - with <userAgentEntry> provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (<userAgentEntry>; Node.js 16.13.1; Darwin 21.5.0)
// - without <userAgentEntry> provided: NodejsDatabricksSqlConnector/0.1.8-beta.1 (Node.js 16.13.1; Darwin 21.5.0)
function checkUserAgentString(ua: string, userAgentEntry?: string) {
// Prefix: 'NodejsDatabricksSqlConnector/'
// Version: three period-separated digits and optional suffix
const re =
/^(?<productName>NodejsDatabricksSqlConnector)\/(?<productVersion>\d+\.\d+\.\d+(-[^(]+)?)\s*\((?<comment>[^)]+)\)(\s+agent\/[a-z-]+)?$/i;
const match = re.exec(ua);
expect(match).to.not.be.eq(null);
const { comment } = match?.groups ?? {};
const parts = comment.split(';').map((s) => s.trim());
expect(parts.length).to.be.gte(2); // at least Node and OS version should be there
if (userAgentEntry) {
expect(comment.trim()).to.satisfy((s: string) => s.startsWith(`${userAgentEntry};`));
}
}
it('matches pattern with userAgentEntry', () => {
const userAgentEntry = 'Some user agent';
const ua = buildUserAgentString(userAgentEntry);
checkUserAgentString(ua, userAgentEntry);
});
it('matches pattern without userAgentEntry', () => {
const ua = buildUserAgentString();
checkUserAgentString(ua);
});
it('should redact internal token in userAgentEntry', () => {
const userAgentEntry = 'dkea-internal-token';
const userAgentString = buildUserAgentString(userAgentEntry);
expect(userAgentString).to.include('<REDACTED>');
});
it('appends agent suffix when agent env var is set', () => {
const orig = process.env.CLAUDECODE;
try {
process.env.CLAUDECODE = '1';
const ua = buildUserAgentString();
expect(ua).to.include('agent/claude-code');
} finally {
if (orig === undefined) {
delete process.env.CLAUDECODE;
} else {
process.env.CLAUDECODE = orig;
}
}
});
});
describe('formatProgress', () => {
it('formats progress', () => {
const result = formatProgress(progressUpdateResponseStub);
expect(result).to.be.eq('\n');
});
});
describe('ProgressUpdateTransformer', () => {
it('should have equal columns', () => {
const t = new ProgressUpdateTransformer(progressUpdateResponseStub);
expect(t.formatRow(['Column 1', 'Column 2'])).to.be.eq('Column 1 |Column 2 ');
});
it('should format response as table', () => {
const t = new ProgressUpdateTransformer({
...progressUpdateResponseStub,
headerNames: ['Column 1', 'Column 2'],
rows: [
['value 1.1', 'value 1.2'],
['value 2.1', 'value 2.2'],
],
footerSummary: 'footer',
});
expect(String(t)).to.be.eq(
'Column 1 |Column 2 \n' + 'value 1.1 |value 1.2 \n' + 'value 2.1 |value 2.2 \n' + 'footer',
);
});
});
describe('definedOrError', () => {
it('should return value if it is defined', () => {
const values = [null, 0, 3.14, false, true, '', 'Hello, World!', [], {}];
for (const value of values) {
const result = definedOrError(value);
expect(result).to.be.equal(value);
}
});
it('should throw error if value is undefined', () => {
expect(() => {
definedOrError(undefined);
}).to.throw();
});
});