-
Notifications
You must be signed in to change notification settings - Fork 479
/
text_to_speech_test.rb
235 lines (205 loc) · 10.3 KB
/
text_to_speech_test.rb
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
require 'test_helper'
class TextToSpeechTest < ActiveSupport::TestCase
setup do
@level_without_instructions = Level.create
@level_with_instructions = Level.create(
{short_instructions: 'regular instructions that *can* also contain **formatting**'}
)
@level_with_instructions_override = Level.create(
{
short_instructions: 'regular instructions that *can* also contain **formatting**',
tts_short_instructions_override: 'override'
}
)
@level_with_long_instructions = Level.create(
{
long_instructions: '*regular* `markdown_instructions` with _some_ [extra](formatting)'
}
)
@level_with_long_instructions_override = Level.create(
{
long_instructions: 'regular markdown_instructions',
tts_long_instructions_override: 'markdown override'
}
)
@level_with_raw_html = Level.create(
{
long_instructions: 'This should have <br/> no <strong>excess</strong> formatting'
}
)
@level_with_block_html = Level.create(
{
long_instructions: "This block should get stripped:\n\n<div><p>Test</p></div>"
}
)
@level_with_xml = Level.create(
{
long_instructions: "This block should get stripped:\n\n<xml><block type='maze_turn'><title name='DIR'>turnLeft</title></block></xml>"
}
)
end
test 'tts_short_instructions_text' do
assert_equal '', @level_without_instructions.tts_short_instructions_text
assert_equal "regular instructions that can also contain formatting\n", @level_with_instructions.tts_short_instructions_text
assert_equal 'override', @level_with_instructions_override.tts_short_instructions_text
end
test 'tts_long_instructions_text' do
assert_equal '', @level_without_instructions.tts_long_instructions_text
assert_equal "regular markdown_instructions with some \n", @level_with_long_instructions.tts_long_instructions_text
assert_equal 'markdown override', @level_with_long_instructions_override.tts_long_instructions_text
end
test 'tts_short_instructions_audio_file' do
assert_equal 'sharon22k/180/100/d41d8cd98f00b204e9800998ecf8427e/.mp3', @level_without_instructions.tts_path(@level_without_instructions.tts_short_instructions_text)
assert_equal 'sharon22k/180/100/71c7e35e3633b5dfce472bcbed146e9f/.mp3', @level_with_instructions.tts_path(@level_with_instructions.tts_short_instructions_text)
assert_equal 'sharon22k/180/100/e3b3f56615d1e5f2608d2f1130a7ef54/.mp3', @level_with_instructions_override.tts_path(@level_with_instructions_override.tts_short_instructions_text)
end
test 'tts_long_instructions_audio_file' do
assert_equal 'sharon22k/180/100/d41d8cd98f00b204e9800998ecf8427e/.mp3', @level_without_instructions.tts_path(@level_without_instructions.tts_long_instructions_text)
assert_equal 'sharon22k/180/100/30a2253e7b14efb1e925743d4afcf405/.mp3', @level_with_long_instructions.tts_path(@level_with_long_instructions.tts_long_instructions_text)
assert_equal 'sharon22k/180/100/25352162fcc2d1e5c4ea3f91b9b39c3f/.mp3', @level_with_long_instructions_override.tts_path(@level_with_long_instructions_override.tts_long_instructions_text)
end
test 'sanitize html and xml' do
assert_equal "This should have no excess formatting\n", @level_with_raw_html.tts_long_instructions_text
assert_equal "This block should get stripped:\n", @level_with_block_html.tts_long_instructions_text
assert_equal "This block should get stripped:\n", @level_with_xml.tts_long_instructions_text
end
test 'tts_long_instructions_text for contained levels' do
contained_level_freeresponse = create :level, name: 'contained level 1', type: 'FreeResponse', properties: {
long_instructions: "This is contained"
}
outer_level = create :level, name: 'level 1', type: 'Blockly'
outer_level.update(contained_level_names: [contained_level_freeresponse.name])
outer_level_with_instructions = create :level, name: 'level 2', type: 'Blockly', long_instructions: "These aren't displayed"
outer_level_with_instructions.update(contained_level_names: [contained_level_freeresponse.name])
contained_level_freeresponse_2 = create :level, name: 'contained level 2', type: 'FreeResponse', properties: {
long_instructions: "This is also contained"
}
outer_level_with_multiple_contained_levels = create :level, name: 'level 3', type: 'Blockly'
outer_level_with_multiple_contained_levels.update(contained_level_names: [contained_level_freeresponse.name, contained_level_freeresponse_2.name])
contained_level_multi = create :level, name: 'contained level multi', type: 'Multi', properties: {
markdown: 'Contained',
questions: [{text: 'Question text'}],
answers: [
{"text" => "answer 1", "correct" => false},
{"text" => "answer 2", "correct" => true},
{"text" => "answer 3", "correct" => true},
]
}
outer_level_with_contained_multi_level = create :level, name: 'level 4', type: 'Blockly'
outer_level_with_contained_multi_level.update(contained_level_names: [contained_level_multi.name])
assert_equal "This is contained\n", outer_level.tts_long_instructions_text
assert_equal "This is contained\n", outer_level_with_instructions.tts_long_instructions_text
assert_equal "This is contained\nThis is also contained\n", outer_level_with_multiple_contained_levels.tts_long_instructions_text
assert_equal "Contained\nQuestion text\nanswer 1\nanswer 2\nanswer 3\n", outer_level_with_contained_multi_level.tts_long_instructions_text
end
test 'tts works for non-english short instructions' do
translatable_level = create :level, name: 'TTS test Short Instructions',
type: 'Blockly', short_instructions: "regular instructions in English"
test_locale = :"te-ST"
I18n.locale = test_locale
custom_i18n = {
"data" => {
"short_instructions" => {
translatable_level.name => "regular instructions in another language"
}
}
}
I18n.backend.store_translations test_locale, custom_i18n
assert_equal "regular instructions in another language\n", translatable_level.tts_short_instructions_text
end
test 'tts works for non-english long instructions' do
translatable_level = create :level, name: 'TTS test Long Instructions',
type: 'Blockly', long_instructions: "long instructions in English"
test_locale = :"te-ST"
I18n.locale = test_locale
custom_i18n = {
"data" => {
"long_instructions" => {
translatable_level.name => "long instructions in another language"
}
}
}
I18n.backend.store_translations test_locale, custom_i18n
assert_equal "long instructions in another language\n", translatable_level.tts_long_instructions_text
end
test 'tts works for non-english contained levels' do
contained_level = create :level, name: 'contained level multi', type: 'Multi', properties: {
markdown: 'Contained',
questions: [{text: 'Question text'}],
answers: [
{"text" => "answer 1", "correct" => false},
{"text" => "answer 2", "correct" => true},
{"text" => "answer 3", "correct" => true},
]
}
outer_level = create :level, name: 'level 4', type: 'Blockly'
outer_level.update(contained_level_names: [contained_level.name])
test_locale = :"te-ST"
I18n.locale = test_locale
custom_i18n = {
"data" => {
"dsls" => {
contained_level.name => {
questions: [{text: 'texte de la question'}],
answers: [
{"text" => "réponse un"},
{"text" => "réponse deux"},
{"text" => "réponse troi"},
]
}
}
}
}
I18n.backend.store_translations test_locale, custom_i18n
assert_equal "Contained\ntexte de la question\nréponse un\nréponse deux\nréponse troi\n", outer_level.tts_long_instructions_text
end
test 'tts ignores overrides for non-english' do
translatable_level = create :level, name: 'TTS test Short Instructions',
type: 'Blockly', short_instructions: "regular instructions in English",
tts_short_instructions_override: "instructions override"
test_locale = :"te-ST"
I18n.locale = test_locale
custom_i18n = {
"data" => {
"short_instructions" => {
translatable_level.name => "regular instructions in another language"
}
}
}
I18n.backend.store_translations test_locale, custom_i18n
assert_equal "regular instructions in another language\n", translatable_level.tts_short_instructions_text
end
test 'updating the long instructions for a level should cause it to create new long instructions audio' do
level = create :level,
name: 'level 1',
type: 'Blockly',
long_instructions: "test long instructions",
published: true
level.save
level.stubs(:write_to_file?).returns(true)
refute level.tts_should_update_long_instructions?
level.long_instructions = "test long instructions updated"
assert level.tts_should_update_long_instructions?
end
test 'updating the contained level for a level should cause it to create new long instructions audio' do
contained_level_one = create :level, name: 'contained level 1', type: 'FreeResponse', properties: {
long_instructions: "This is the first contained"
}
contained_level_two = create :level, name: 'contained level 2', type: 'FreeResponse', properties: {
long_instructions: "This is the second contained"
}
outer_level = create :level, name: 'level 1', type: 'Blockly', published: true
outer_level.update(contained_level_names: [contained_level_one.name])
outer_level.stubs(:write_to_file?).returns(true)
refute outer_level.tts_should_update_long_instructions?
outer_level.contained_level_names = [contained_level_two.name]
assert outer_level.tts_should_update_long_instructions?
end
test 'tts_url should return nil given locale not supported for TTS' do
assert_nil(@level_without_instructions.tts_url(@level_without_instructions.tts_short_instructions_text, :'te-ST'))
end
test 'tts_url should return a url to a tts file' do
expected_tts_url = 'https://tts.code.org/sharon22k/180/100/71c7e35e3633b5dfce472bcbed146e9f/.mp3'
assert_equal(expected_tts_url, @level_with_instructions.tts_url(@level_with_instructions.tts_short_instructions_text, :'en-US'))
end
end