--!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" },
})
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
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
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
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
function test_novo_job_init__sem_catalogo_os_projetos_nascem()
assert_eq(nova():get("new_job_projects"), "[]")
end
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
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
function test_novo_job_modo_edicao__job_pausado_vem_desmarcado()
assert_eq(
editando({ id = "j", enabled = false }):get("new_job_enabled"),
"false"
)
end
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
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
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
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
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
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
function test_novo_job_modo_edicao__as_envs_do_job_ficam_guardadas_para_o_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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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