refactor(install): install.sh delegates config generation, and tests stop reading hand-written configs

Two things fall out of one principle: a test that asserts against a
hand-written file is testing an artifact a human or an agent may legitimately
rewrite at any moment, so only generated output is a valid target.

install.sh generated config.toml two ways -- a full heredoc for the self-host
path and an awk splice of the [forge] block into config.example.toml -- with a
forge_block shell helper feeding both. That is the arrangement
docs/architecture/configuration.md decided against: it "must stop generating
config.toml; archied setup owns it", because the installer hand-rolling TOML
already drifted once (choosing Gitea produced a config naming a token the
installer never wrote). Both generators and forge_block are gone; install.sh
collects its answers as before and passes them to archied setup as parameters,
with secrets passed as REFERENCES to the env keys it just wrote rather than as
values, which would put them on a command line. The installer also now asks for
a cloud model, which archied setup requires and the old path never emitted.

deployment_contract_test.go tested the removed generators by parsing install.sh's
shell text, reconstructing the heredoc and asserting on its content. It broke the
instant install.sh stopped generating config -- "install.sh has no heredoc
declaring the daemon config's [nats] table" -- which is the failure mode in
miniature: the test was coupled to an artifact, not to behaviour. Deleted.

What it actually protected is kept, asserted on the GENERATED config instead:
TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements runs archied setup and
loads what it wrote, requiring services.state.target and bot_user. A test against
generated output fails when generation breaks and passes when a human merely
tidies the template.

Found by an adversarial package sweep (temp tracker: config-1,
repo-root-deployment-1, repo-root-deployment-2).
This commit is contained in:
sam committed 2026-09-15 03:35:45 +10:00
1 parent a856e5ac80
commit 0e64573b04
3 files changed
+90 -164

No files matched your search

+50 -5
View File
@@ -153,12 +153,21 @@ func TestInstallValidatedConfig_ConfigWriteFailureLeavesNoSecret(t *testing.T) {
}
rendered := []byte("bot_user = \"widget\"\nwork_dir = \"/tmp/archie-setup-work\"\n")
err := installValidatedConfig(configuration.New(nil), cfgPath, sink, rendered)
if err == nil {
t.Fatal("installValidatedConfig = nil error, want a write failure")
// Prove the fixture is loadable BEFORE relying on the write failing.
// Asserted by loading it rather than by matching the error text below: a
// reworded error must not silently disarm this guard and let the test pass
// for the wrong reason.
fixture := filepath.Join(dir, "fixture.toml")
if err := os.WriteFile(fixture, rendered, 0o600); err != nil {
t.Fatal(err)
}
if strings.Contains(err.Error(), "does not load") {
t.Fatalf("fixture failed validation rather than the write, so this test never exercised the write path: %v", err)
if _, err := configuration.New(nil).File(fixture); err != nil {
t.Fatalf("fixture config does not load, so this test would not exercise the write path: %v", err)
}
if err := installValidatedConfig(configuration.New(nil), cfgPath, sink, rendered); err == nil {
t.Fatal("installValidatedConfig = nil error, want a write failure")
}
if _, statErr := os.Stat(envPath); !os.IsNotExist(statErr) {
t.Errorf("secret committed even though the config was never written: %v", statErr)
@@ -237,3 +246,39 @@ func TestRunSetup_RejectsReferencesNothingWouldUse(t *testing.T) {
})
}
}
// TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements pins the properties
// archied needs of whatever setup emits, asserted on the GENERATED config.
//
// It replaces a test that read config.example.toml and reconstructed install.sh's
// shell heredoc. Testing hand-written files is testing an artifact a human or an
// agent may legitimately rewrite at any time -- and that test in fact broke the
// moment install.sh stopped generating config, because the heredoc it parsed no
// longer existed. The generated output is the contract; the template is an input.
func TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements(t *testing.T) {
dir := t.TempDir()
cfgPath := filepath.Join(dir, "config.toml")
devNull, err := os.Open(os.DevNull)
if err != nil {
t.Fatal(err)
}
defer func() { _ = devNull.Close() }()
var stdout, stderr strings.Builder
if code := RunSetup([]string{"--defaults", "-config", cfgPath}, devNull, &stdout, &stderr); code != 0 {
t.Fatalf("RunSetup exit = %d, stderr: %s", code, stderr.String())
}
doc, err := configuration.New(nil).File(cfgPath)
if err != nil {
t.Fatalf("generated config does not load: %v", err)
}
// openStateStoreAdapter refuses an empty target, so this is the difference
// between a config that boots and one that does not.
if doc.Config.Services.State.Target == "" {
t.Error("generated config leaves services.state.target empty; the daemon would not boot")
}
if doc.Config.BotUser == "" {
t.Error("generated config leaves bot_user empty")
}
}