forked from libapps/libapps-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhterm_preference_manager_tests.js
More file actions
150 lines (137 loc) · 5.13 KB
/
hterm_preference_manager_tests.js
File metadata and controls
150 lines (137 loc) · 5.13 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
// Copyright 2017 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview Preference manager tests.
*/
import {lib} from '../../libdot/index.js';
import {hterm} from '../index.js';
/**
* The main body of translations.
*/
const nasshMessagesPromise = fetch('../../nassh/_locales/en/messages.json');
/**
* Make sure hterm translations are kept in sync with nassh.
*/
it('pref-messages-sync', async function() {
const toMsgId = (id) => id.replace(/-/g, '_').toUpperCase();
const fromMsgId = (id) => id.replace(/_/g, '-').toLowerCase();
const helpIdToMsgId = (id) => `PREF_${toMsgId(id)}`;
const msgIdToHelpId = (id) => fromMsgId(id.substr(11));
const nameIdToMsgId = (id) => `NAME_PREF_${toMsgId(id)}`;
const msgIdToNameId = (id) => fromMsgId(id.substr(16));
const titleIdToMsgId = (id) => `TITLE_PREF_${toMsgId(id)}`;
// Load the translations database from nassh.
const td = new TextDecoder();
const response = await nasshMessagesPromise;
if (!response.ok) {
console.warn('Unable to load nassh translations');
this.skip();
return;
}
const messages = /** @type {!lib.MessageManager.Messages} */
(JSON.parse(td.decode(await response.arrayBuffer())));
hterm.messageManager.addMessages(messages);
// Walk the loaded message ids and check for stale entries.
/** @suppress {visibility} */
const loadedMessages = hterm.messageManager.messages_;
Object.entries(loadedMessages)
.forEach(([msgId, nasshMsg]) => {
if (msgId.startsWith('HTERM_PREF_')) {
const key = msgIdToHelpId(msgId);
assert.property(hterm.PreferenceManager.defaultPreferences, key,
`stale ${msgId} help translation for key ${key}`);
}
if (msgId.startsWith('HTERM_TITLE_PREF_')) {
let found = false;
hterm.PreferenceManager.categoryDefinitions.forEach((def) => {
if (msgId == 'HTERM_' + titleIdToMsgId(def.id)) {
found = true;
}
});
assert.isTrue(found, `stale ${msgId} translation for category`);
}
if (msgId.startsWith('HTERM_NAME_PREF_')) {
const key = msgIdToNameId(msgId);
assert.property(hterm.PreferenceManager.defaultPreferences, key,
`stale ${msgId} name translation for key ${key}`);
}
});
// Walk the local hterm prefs and make sure they match the nassh copy.
Object.entries(hterm.PreferenceManager.defaultPreferences).forEach(
([key, entry]) => {
// Check the pref name text.
const nameId = nameIdToMsgId(key);
const htermNameMsg = entry['name'];
const nasshNameMsg = hterm.msg(nameId);
assert.equal(htermNameMsg, nasshNameMsg, nameId);
// Check the help text.
const helpId = helpIdToMsgId(key);
const htermHelpMsg = entry['help'];
const nasshHelpMsg = hterm.msg(helpId);
assert.equal(htermHelpMsg, nasshHelpMsg, helpId);
});
// Walk the hterm categories and make sure they match the nassh copy.
hterm.PreferenceManager.categoryDefinitions.forEach((def) => {
const msgId = titleIdToMsgId(def.id);
const htermMsg = def.text;
const nasshMsg = hterm.msg(msgId);
assert.equal(htermMsg, nasshMsg, msgId);
});
});
/**
* Make sure default values can be parsed correctly.
*/
it('parse-defaults', () => {
Object.entries(hterm.PreferenceManager.defaultPreferences)
.forEach(([key, pref]) => {
if (Array.isArray(pref.type)) {
assert.isTrue(
pref.type.indexOf(pref.default) !== -1,
`invalid array pref ${key}, default ` +
`${pref.default} not in pref ${JSON.stringify(pref.type)}`);
return;
}
const msg = `invalid ${pref.type} pref ${key}: ${pref.default}`;
switch (pref.type) {
case 'bool':
assert.typeOf(pref.default, 'boolean', msg);
break;
case 'color': {
const rgba = lib.colors.normalizeCSS(pref.default);
assert.isNotNull(rgba, msg);
assert.isNotNull(lib.colors.crackRGB(lib.notNull(rgba)), msg);
break;
}
case 'int':
assert.isTrue(Number.isInteger(pref.default), msg);
break;
case 'multiline-string':
assert.typeOf(pref.default, 'string', msg);
break;
case 'string':
assert.typeOf(pref.default, 'string', msg);
break;
case 'tristate':
assert.isTrue(
typeof pref.default === 'boolean' || pref.default === null,
msg);
break;
case 'url':
try {
if (pref.default !== '') {
// eslint-disable-next-line no-new
new URL(pref.default);
}
} catch (e) {
assert.fail(msg);
}
break;
case 'value':
// Anything goes for 'value'.
break;
default:
assert.fail(`invalid pref ${key} unknown type: ${pref.type}`);
}
});
});