rustploy-gui 0.1.73

Rustploy — desktop client (glacier-ui) for the Rustploy PaaS daemon
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
--!nonstrict
-- Janela "Novo job" (scripts/new_job_window.luau): passos do assistente, modo
-- edição e a validação do formulário. Os testes de `new_job_create` só chegam
-- até onde a validação recusa — passar dela dispararia o RPC (rede).

local CAMINHO = "views/new_job_window.screen.gv"

local function nova(ctx_inicial)
    local tela = component(CAMINHO, ctx_inicial)
    tela:call("init")
    return tela
end

-- `api_url` semeado: sem ele new_job_create para antes de validar qualquer
-- campo. Os campos do formulário NÃO podem ser semeados: o init zera nome,
-- main_service e compose. Entram depois dele, com `set`, como se o usuário os
-- tivesse digitado.
local function conectada(campos)
    local tela =
        nova({ api_url = "https://zz-teste.invalid", api_token = "t" })
    for k, v in campos or {} do
        tela:set(k, v)
    end
    return tela
end

local PROJETOS =
    json.encode({ { id = "p1", name = "loja" }, { id = "p2", name = "blog" } })
local SERVICOS = json.encode({
    { id = "s1", name = "api", project_id = "p1" },
    { id = "s2", name = "web", project_id = "p1" },
    { id = "s3", name = "wp", project_id = "p2" },
})

--- abre escolhendo o projeto, com o formulário zerado
function test_novo_job_init__abre_escolhendo_o_projeto_com_o_formulario()
    local tela = nova()
    assert_eq(tela:get("new_job_step"), "pick_project")
    assert_eq(tela:get("new_job_name"), "")
    assert_eq(tela:get("new_job_compose"), "")
    assert_eq(tela:get("new_job_main_service"), "")
    assert_eq(tela:get("new_job_edit_id"), "")
    assert_eq(tela:get("new_job_error"), "")
    assert_eq(tela:get("new_job_busy"), "")
end

--- os padrões da recorrência: manual, a cada 6h, 03:00, segunda
function test_novo_job_init__os_padroes_da_recorrencia_manual_a_cada_6h_03_00()
    local tela = nova()
    assert_eq(tela:get("new_job_kind"), "manual")
    assert_eq(tela:get("new_job_hours"), "6")
    assert_eq(tela:get("new_job_time"), "03:00")
    assert_eq(tela:get("new_job_weekday"), "0")
    assert_eq(tela:get("new_job_enabled"), "true")
end

--- a fonte do compose começa em colado, com o caminho padrão
function test_novo_job_init__a_fonte_do_compose_comeca_em_colado_com_o()
    local tela = nova()
    assert_eq(tela:get("new_job_source"), "compose")
    assert_eq(tela:get("new_job_compose_path"), "docker-compose.yml")
    assert_eq(tela:get("new_job_git_providers"), "[]")
    assert_eq(tela:get("new_job_git_repos"), "[]")
end

--- os catálogos semeados pelo pai ficam à disposição
function test_novo_job_init__os_catalogos_semeados_pelo_pai_ficam_a()
    local tela =
        nova({ new_job_projects = PROJETOS, new_job_services = SERVICOS })
    assert_eq(tela:get("new_job_projects"), PROJETOS)
    assert_eq(tela:get("new_job_services_all"), SERVICOS)
    assert_eq(tela:get("new_job_services_filtered"), "[]")
end

--- sem catálogo os projetos nascem []
function test_novo_job_init__sem_catalogo_os_projetos_nascem()
    assert_eq(nova():get("new_job_projects"), "[]")
end

--- os dias da semana vêm do formatador
function test_novo_job_init__os_dias_da_semana_vem_do_formatador()
    assert_eq(#json.decode(nova():get("weekdays") or "[]"), 7)
end

local function editando(job)
    return nova({ new_job_edit_job = json.encode(job) })
end

--- pula direto para o formulário e preenche o job
function test_novo_job_modo_edicao__pula_direto_para_o_formulario_e_preenche()
    local tela = editando({
        id = "j1",
        name = "migrar",
        main_service = "app",
        compose = "services: {}",
        enabled = true,
    })
    assert_eq(tela:get("new_job_edit_id"), "j1")
    assert_eq(tela:get("new_job_step"), "form")
    assert_eq(tela:get("new_job_name"), "migrar")
    assert_eq(tela:get("new_job_main_service"), "app")
    assert_eq(tela:get("new_job_compose"), "services: {}")
    assert_eq(tela:get("new_job_enabled"), "true")
end

--- job pausado vem desmarcado
function test_novo_job_modo_edicao__job_pausado_vem_desmarcado()
    assert_eq(
        editando({ id = "j", enabled = false }):get("new_job_enabled"),
        "false"
    )
end

--- recorrência de intervalo
function test_novo_job_modo_edicao__recorrencia_de_intervalo()
    local tela = editando({ id = "j", recurrence = { IntervalHours = 12 } })
    assert_eq(tela:get("new_job_kind"), "interval")
    assert_eq(tela:get("new_job_hours"), "12")
end

--- recorrência diária vira HH:MM
function test_novo_job_modo_edicao__recorrencia_diaria_vira_hh_mm()
    local tela = editando({
        id = "j",
        recurrence = { Daily = { hour = 7, minute = 5 } },
    })
    assert_eq(tela:get("new_job_kind"), "daily")
    assert_eq(tela:get("new_job_time"), "07:05")
end

--- recorrência semanal guarda o dia e o horário
function test_novo_job_modo_edicao__recorrencia_semanal_guarda_o_dia_e_o()
    local tela = editando({
        id = "j",
        recurrence = { Weekly = { weekday = 3, hour = 9, minute = 30 } },
    })
    assert_eq(tela:get("new_job_kind"), "weekly")
    assert_eq(tela:get("new_job_weekday"), "3")
    assert_eq(tela:get("new_job_time"), "09:30")
end

--- sem recorrência (ou desconhecida) é manual com os padrões
function test_novo_job_modo_edicao__sem_recorrencia_ou_desconhecida_e_manual()
    local jobs = { { id = "j" }, { id = "j", recurrence = { Mensal = {} } } }
    for _, job in jobs do
        local tela = editando(job)
        assert_eq(tela:get("new_job_kind"), "manual")
        assert_eq(tela:get("new_job_hours"), "6")
        assert_eq(tela:get("new_job_time"), "03:00")
    end
end

--- job com fonte git troca a fonte e os campos do picker
function test_novo_job_modo_edicao__job_com_fonte_git_troca_a_fonte_e_os()
    local tela = editando({
        id = "j",
        git_source = {
            provider_id = "gp1",
            branch = "main",
            compose_path = "ci/compose.yml",
        },
    })
    assert_eq(tela:get("new_job_source"), "git")
    assert_eq(tela:get("new_job_git_provider_id"), "gp1")
    assert_eq(tela:get("new_job_git_branch"), "main")
    assert_eq(tela:get("new_job_compose_path"), "ci/compose.yml")
    assert_eq(tela:get("new_job_compose"), "")
end

--- git sem compose_path usa o padrão
function test_novo_job_modo_edicao__git_sem_compose_path_usa_o_padrao()
    local tela = editando({ id = "j", git_source = { branch = "dev" } })
    assert_eq(tela:get("new_job_compose_path"), "docker-compose.yml")
end

--- as envs do job ficam guardadas para o "Salvar" reenviá-las (o painel as
--- gerencia direto no daemon)
function test_novo_job_modo_edicao__envs_do_job_ficam_guardadas_para_salvar()
    local tela = editando({
        id = "j",
        project_id = "p1",
        env_vars = {
            { key = "A", value = { Plain = "1" } },
            { key = "B", value = { Secret = "S" } },
        },
        env_comments = { { before_key = "B", text = "# b" } },
    })
    local seed = json.decode(tela:get("new_job_env_seed"))
    assert_eq(#seed.env_vars, 2)
    assert_eq(seed.env_vars[2].value.Secret, "S")
    assert_eq(seed.env_comments[1].text, "# b")
    assert_eq(tela:get("new_job_project_id"), "p1")
end

--- o painel de envs é um passo da janela e a tela avalia
function test_novo_job_passo_env_avalia()
    local tela = editando({ id = "j", project_id = "p1" })
    tela:set("new_job_step", "env")
    tela:set("job_env_name", "backup")
    tela:set("job_env", "[]")
    tela:set("job_secrets", "[]")
    assert_not_nil(tela:render())
    assert_true(#tela:buttons() > 0)
end

--- JSON de edição ilegível é ignorado: abre como job novo
function test_novo_job_modo_edicao__json_de_edicao_ilegivel_e_ignorado_abre()
    local tela = nova({ new_job_edit_job = "isso não é json" })
    assert_eq(tela:get("new_job_edit_id"), "")
    assert_eq(tela:get("new_job_step"), "pick_project")
end

local function com_catalogo()
    return nova({ new_job_projects = PROJETOS, new_job_services = SERVICOS })
end

--- escolher o projeto filtra os serviços dele e avança
function test_novo_job_passos__escolher_o_projeto_filtra_os_servicos_dele_e()
    local tela = com_catalogo()
    tela:call("new_job_pick_project", "p1")
    assert_eq(tela:get("new_job_project_id"), "p1")
    assert_eq(tela:get("new_job_project_name"), "loja")
    assert_eq(tela:get("new_job_step"), "pick_service")
    local nomes = {}
    for _, s in json.decode(tela:get("new_job_services_filtered") or "[]") do
        table.insert(nomes, s.name)
    end
    assert_eq(nomes, { "api", "web" })
end

--- outro projeto, outros serviços
function test_novo_job_passos__outro_projeto_outros_servicos()
    local tela = com_catalogo()
    tela:call("new_job_pick_project", "p2")
    assert_eq(tela:get("new_job_project_name"), "blog")
    assert_eq(#json.decode(tela:get("new_job_services_filtered") or "[]"), 1)
end

--- projeto desconhecido não acha nome nem serviço, mas o passo avança
function test_novo_job_passos__projeto_desconhecido_nao_acha_nome_nem_servico()
    local tela = com_catalogo()
    tela:call("new_job_pick_project", "zzz")
    assert_eq(tela:get("new_job_project_name"), "")
    assert_eq(tela:get("new_job_services_filtered"), "[]")
end

--- escolher o serviço gatilho guarda o nome e vai ao formulário
function test_novo_job_passos__escolher_o_servico_gatilho_guarda_o_nome_e_vai()
    local tela = com_catalogo()
    tela:call("new_job_pick_project", "p1")
    tela:call("new_job_pick_service", "s2")
    assert_eq(tela:get("new_job_service_id"), "s2")
    assert_eq(tela:get("new_job_service_name"), "web")
    assert_eq(tela:get("new_job_step"), "form")
end

--- job autônomo: sem serviço gatilho
function test_novo_job_passos__job_autonomo_sem_servico_gatilho()
    local tela = com_catalogo()
    tela:call("new_job_pick_project", "p1")
    tela:call("new_job_pick_no_service")
    assert_eq(tela:get("new_job_service_id"), "")
    assert_eq(tela:get("new_job_service_name"), "none (standalone)")
    assert_eq(tela:get("new_job_step"), "form")
end

--- voltar recua um passo por vez
function test_novo_job_passos__voltar_recua_um_passo_por_vez()
    local tela = com_catalogo()
    tela:call("new_job_pick_project", "p1")
    tela:call("new_job_pick_service", "s1")
    assert_eq(tela:get("new_job_step"), "form")
    tela:call("new_job_back")
    assert_eq(tela:get("new_job_step"), "pick_service")
    tela:call("new_job_back")
    assert_eq(tela:get("new_job_step"), "pick_project")
    tela:call("new_job_back")
    assert_eq(tela:get("new_job_step"), "pick_project")
end

--- o tipo de recorrência e o setter genérico
function test_novo_job_passos__o_tipo_de_recorrencia_e_o_setter_generico()
    local tela = nova()
    tela:call("new_job_kind", "weekly")
    assert_eq(tela:get("new_job_kind"), "weekly")
    tela:call("field", "new_job_name")
    assert_eq(tela:get("new_job_name"), "")
end

--- trocar a fonte sem conexão só grava a escolha
function test_novo_job_fonte_git__trocar_a_fonte_sem_conexao_so_grava_a()
    local tela = nova()
    tela:call("new_job_source", "git")
    assert_eq(tela:get("new_job_source"), "git")
    assert_eq(tela:get("new_job_git_message"), "")
    tela:call("new_job_source", "compose")
    assert_eq(tela:get("new_job_source"), "compose")
end

--- escolher a conta zera repositório, repos e branches
function test_novo_job_fonte_git__escolher_a_conta_zera_repositorio_repos_e()
    local tela = nova({
        new_job_git_repo = "a/b",
        new_job_git_repos = "[1]",
        new_job_git_branches = "[1]",
    })
    tela:call("new_job_git_provider_pick", "gp1")
    assert_eq(tela:get("new_job_git_provider_id"), "gp1")
    assert_eq(tela:get("new_job_git_repo"), "")
    assert_eq(tela:get("new_job_git_repos"), "[]")
    assert_eq(tela:get("new_job_git_branches"), "[]")
end

--- escolher o repositório adota a branch padrão dele
function test_novo_job_fonte_git__escolher_o_repositorio_adota_a_branch()
    local repos = json.encode({
        { full_name = "a/b", clone_url = "u1", default_branch = "main" },
        { full_name = "a/c", clone_url = "u2", default_branch = "" },
    })
    local tela = nova({ new_job_git_repos = repos })
    tela:call("new_job_git_repo_pick", "a/b")
    assert_eq(tela:get("new_job_git_repo"), "a/b")
    assert_eq(tela:get("new_job_git_branch"), "main")
    assert_eq(tela:get("new_job_git_branches"), "[]")
end

--- repositório sem branch padrão deixa a branch como estava
function test_novo_job_fonte_git__repositorio_sem_branch_padrao_deixa_a()
    local repos = json.encode({
        { full_name = "a/c", clone_url = "u2", default_branch = "" },
    })
    local tela = nova({ new_job_git_repos = repos })
    tela:call("new_job_git_repo_pick", "a/c")
    assert_eq(tela:get("new_job_git_branch"), "")
end

local function erro_ao_criar(campos, fonte)
    local tela = conectada()
    if fonte then
        tela:call("new_job_source", fonte)
    end
    for k, v in campos or {} do
        tela:set(k, v)
    end
    tela:call("new_job_create")
    return tela:get("new_job_error"), tela
end

--- sem conexão manda reabrir a janela
function test_novo_job_criar_so_ate_a_validacao_recusar__sem_conexao_manda()
    local tela = nova()
    tela:set("new_job_name", "x")
    tela:call("new_job_create")
    assert_eq(
        tela:get("new_job_error"),
        "no connection — reopen the window from the app"
    )
end

--- o nome é obrigatório, mesmo só de espaços
function test_novo_job_criar_so_ate_a_validacao_recusar__o_nome_e_obrigatorio()
    assert_eq((erro_ao_criar({ new_job_name = "   " })), "enter a name")
    assert_eq((erro_ao_criar({})), "enter a name")
end

--- o main_service é obrigatório
function test_novo_job_criar_so_ate_a_validacao_recusar__o_main_service_e()
    local erro =
        erro_ao_criar({ new_job_name = "migrar", new_job_main_service = " " })
    assert_has(erro, "main_service")
end

--- fonte compose pede o docker-compose colado
function test_novo_job_criar_so_ate_a_validacao_recusar__fonte_compose_pede_o()
    local erro = erro_ao_criar({
        new_job_name = "migrar",
        new_job_main_service = "app",
        new_job_compose = "  \n ",
    })
    assert_eq(erro, "paste the job's docker-compose.yml")
end

--- fonte git pede repositório e branch
function test_novo_job_criar_so_ate_a_validacao_recusar__fonte_git_pede()
    for _, parcial in
        {
            {},
            { new_job_git_repo = "a/b" },
            { new_job_git_branch = "main" },
            { new_job_git_repo = "a/b", new_job_git_branch = "   " },
        }
    do
        local campos =
            { new_job_name = "migrar", new_job_main_service = "app" }
        for k, v in parcial do
            campos[k] = v
        end
        assert_eq(
            (erro_ao_criar(campos, "git")),
            "select repository and branch"
        )
    end
end

--- git com repositório que não está na lista pede para selecionar de novo
function test_novo_job_criar_so_ate_a_validacao_recusar__git_com_repositorio()
    local erro = erro_ao_criar({
        new_job_name = "migrar",
        new_job_main_service = "app",
        new_job_git_repo = "a/b",
        new_job_git_branch = "main",
        new_job_git_repos = "[]",
    }, "git")
    assert_has(erro, "could not resolve the repository URL")
end

--- o erro some quando o formulário passa a validar (limpa antes de ir à rede)
function test_novo_job_criar_so_ate_a_validacao_recusar__o_erro_some_quando_o()
    local tela = conectada({
        new_job_name = "migrar",
        new_job_main_service = "app",
        new_job_compose = "services: {}",
    })
    tela:set("new_job_error", "velho")
    tela:call("new_job_create") -- passa da validação: o RPC suspende
    assert_eq(tela:get("new_job_error"), "")
    assert_eq(tela:get("new_job_message"), "creating…")
end

--- o formulário recusado não deixa o botão travado
function test_novo_job_criar_so_ate_a_validacao_recusar__o_formulario()
    local _, tela = erro_ao_criar({ new_job_name = "migrar" })
    assert_eq(tela:get("new_job_busy"), "")
    assert_eq(tela:get("new_job_message"), "")
end

-- ── variáveis já na criação (lista local que vai no JobCreate) ─────────────

--- ir ao formulário de um job novo zera a lista de variáveis e mostra o form
--- de adicionar
function test_novo_job_envs_na_criacao__o_formulario_tem_o_form_de_variaveis()
    local tela = com_catalogo()
    tela:call("new_job_pick_project", "p1")
    tela:call("new_job_pick_service", "s2")
    assert_eq(tela:get("new_job_env_rows"), "[]")
    assert_eq(tela:get("new_job_env_count"), "0")
    assert_not_nil(tela:render())
    assert_true(
        #tela:find_all({ kind = "checkbox", class = "env__use-secret" }) == 1,
        'falta o "usar secret" do form'
    )
end

--- adicionar, editar no lugar e remover mexem só na lista local
function test_novo_job_envs_na_criacao__adicionar_editar_e_remover()
    local tela = com_catalogo()
    tela:call("new_job_pick_project", "p1")
    tela:call("new_job_pick_no_service")
    tela:set("new_job_env_key", "  OLA ")
    tela:set("new_job_env_value", "mundo")
    tela:call("new_job_env_add")
    assert_eq(tela:get("new_job_env_count"), "1")
    assert_eq(
        tela:get("new_job_env_key"),
        "",
        "o form esvazia depois de adicionar"
    )
    tela:set("new_job_env_key", "TOKEN")
    tela:set("new_job_env_is_secret", "true")
    tela:set("new_job_env_value", "API_TOKEN")
    tela:call("new_job_env_add")
    local linhas = json.decode(tela:get("new_job_env_rows"))
    assert_eq(#linhas, 2)
    assert_eq(linhas[1].key, "OLA")
    assert_eq(linhas[2].is_secret, "true")
    assert_eq(linhas[2].secret, "API_TOKEN")
    -- a linha é editável no lugar: o campo lê a chave do ctx da posição
    assert_not_nil(tela:render())
    local chaves =
        tela:find_all({ kind = "textinput", class = "env__inline-key" })
    assert_eq(#chaves, 2)
    assert_eq(tela:get(chaves[1].attrs.value_var), "OLA")
    tela:call("new_job_env_row_value:1", "terra")
    assert_eq(tela:get("new_job_env_row_1_value"), "terra")
    tela:call("new_job_env_delete:1")
    linhas = json.decode(tela:get("new_job_env_rows"))
    assert_eq(#linhas, 1)
    assert_eq(linhas[1].key, "TOKEN")
end

--- secret sem escolher e chave vazia não entram
function test_novo_job_envs_na_criacao__chave_vazia_e_secret_sem_nome_fora()
    local tela = com_catalogo()
    tela:call("new_job_pick_project", "p1")
    tela:call("new_job_pick_no_service")
    tela:set("new_job_env_key", "")
    tela:call("new_job_env_add")
    tela:set("new_job_env_key", "X")
    tela:set("new_job_env_is_secret", "true")
    tela:set("new_job_env_value", "")
    tela:call("new_job_env_add")
    assert_eq(tela:get("new_job_env_count"), "0")
end