node-app-build 5.20.2

Mini app developer CLI: scaffold, validate, package node-app-* Debian packages
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
//! Integration tests for `node-app new` and related commands.
//!
//! Templates are loaded from the monorepo's `infra/repo-templates/` directory
//! via the `NODE_APP_TEMPLATES_REPO` environment variable so tests are hermetic
//! (no network, no `gh` auth required).

use assert_cmd::Command;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

fn bin() -> Command {
    // Binary name is "node-app" as defined in [[bin]] name in Cargo.toml.
    Command::cargo_bin("node-app").expect("build binary")
}

/// Absolute path to the monorepo's repo-templates directory, used as the
/// local template source in all scaffold tests.
fn templates_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("../../infra/repo-templates")
        .canonicalize()
        .expect("infra/repo-templates exists")
}

#[test]
fn new_bun_then_validate_passes() {
    let tmp = TempDir::new().unwrap();
    let dest = tmp.path().join("my-test-app");

    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args([
            "new",
            "my-test-app",
            "--type",
            "bun",
            "--out",
            dest.to_str().unwrap(),
            "--no-deps-update",
        ])
        .assert()
        .success();

    // Basic file presence.
    assert!(dest.join("manifest.json").exists(), "manifest.json missing");
    assert!(dest.join("package.json").exists(), "package.json missing");
    assert!(dest.join("src/index.ts").exists(), "src/index.ts missing");
    assert!(dest.join("ui/index.html").exists(), "ui/index.html missing");
    assert!(
        dest.join("debian/control.template").exists(),
        "debian/control.template missing"
    );
    assert!(dest.join("debian/postinst").exists(), "debian/postinst missing");
    assert!(dest.join("debian/prerm").exists(), "debian/prerm missing");

    // Manifest sanity — name was substituted.
    let manifest = fs::read_to_string(dest.join("manifest.json")).unwrap();
    assert!(
        manifest.contains("\"name\": \"my-test-app\""),
        "name placeholder not substituted: {}",
        manifest
    );
    assert!(
        manifest.contains("\"manifest_version\": 2"),
        "v2 marker missing"
    );
    assert!(manifest.contains("\"abi\": \"v1\""), "abi missing");

    // Description default was substituted.
    assert!(
        manifest.contains("A Node mini app"),
        "description placeholder not substituted: {}",
        manifest
    );

    // Validator should pass on the just-scaffolded project.
    bin()
        .args(["validate", "--path", dest.to_str().unwrap()])
        .assert()
        .success();
}

#[test]
fn new_bun_fullstack_has_ui_dir() {
    let tmp = TempDir::new().unwrap();
    let dest = tmp.path().join("my-fullstack-app");

    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args([
            "new",
            "my-fullstack-app",
            "--type",
            "bun-fullstack",
            "--out",
            dest.to_str().unwrap(),
            "--no-deps-update",
        ])
        .assert()
        .success();

    assert!(dest.join("manifest.json").exists());
    assert!(dest.join("ui/src/App.tsx").exists(), "ui/src/App.tsx missing");
    assert!(dest.join("ui/package.json").exists(), "ui/package.json missing");

    let manifest = fs::read_to_string(dest.join("manifest.json")).unwrap();
    assert!(
        manifest.contains("\"has_ui\": true"),
        "has_ui should be true in fullstack template: {}",
        manifest
    );
}

#[test]
fn new_cdylib_template_renders_but_validator_rejects() {
    let tmp = TempDir::new().unwrap();
    let dest = tmp.path().join("my-native-app");

    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args([
            "new",
            "my-native-app",
            "--type",
            "cdylib",
            "--out",
            dest.to_str().unwrap(),
            "--no-deps-update",
        ])
        .assert()
        .success();

    assert!(dest.join("Cargo.toml").exists());
    assert!(dest.join("src/lib.rs").exists());
    assert!(dest.join("manifest.json").exists());

    // The validator targets the apt-install policy by default and must
    // reject native apps (T118 — path-based tier rule, R8).
    bin()
        .args(["validate", "--path", dest.to_str().unwrap()])
        .assert()
        .failure()
        .stderr(predicates::str::contains("native"));
}

#[test]
fn new_standalone_rust_has_systemd_files() {
    let tmp = TempDir::new().unwrap();
    let dest = tmp.path().join("my-standalone-svc");

    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args([
            "new",
            "my-standalone-svc",
            "--type",
            "standalone-rust",
            "--out",
            dest.to_str().unwrap(),
            "--no-deps-update",
        ])
        .assert()
        .success();

    assert!(dest.join("Cargo.toml").exists(), "Cargo.toml missing");
    assert!(dest.join("src/main.rs").exists(), "src/main.rs missing");
    assert!(dest.join("src/ipc.rs").exists(), "src/ipc.rs missing");
    assert!(dest.join("manifest.json").exists(), "manifest.json missing");

    // The systemd unit file exists (filename has placeholder substituted).
    assert!(
        dest.join("systemd/node-app-my-standalone-svc.service").exists(),
        "systemd unit file missing"
    );

    let manifest = fs::read_to_string(dest.join("manifest.json")).unwrap();
    assert!(
        manifest.contains("\"app_type\": \"standalone\""),
        "app_type should be standalone: {}",
        manifest
    );

    // Standalone apps pass the validator (they're allowed at the apt path).
    bin()
        .args(["validate", "--path", dest.to_str().unwrap()])
        .assert()
        .success();
}

#[test]
fn new_standalone_bun_has_ipc_client() {
    let tmp = TempDir::new().unwrap();
    let dest = tmp.path().join("my-standalone-bun");

    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args([
            "new",
            "my-standalone-bun",
            "--type",
            "standalone-bun",
            "--out",
            dest.to_str().unwrap(),
            "--no-deps-update",
        ])
        .assert()
        .success();

    assert!(dest.join("src/index.ts").exists(), "src/index.ts missing");
    assert!(dest.join("src/ipc.ts").exists(), "src/ipc.ts missing");
    assert!(dest.join("manifest.json").exists(), "manifest.json missing");
    assert!(
        dest.join("systemd/node-app-my-standalone-bun.service").exists(),
        "systemd unit file missing"
    );
}

#[test]
fn name_placeholder_substituted_in_all_templates() {
    for kind in &[
        "bun",
        "bun-fullstack",
        "cdylib",
        "cdylib-fullstack",
        "standalone-rust",
        "standalone-bun",
    ] {
        let tmp = TempDir::new().unwrap();
        let dest = tmp.path().join("test-subst");

        bin()
            .env("NODE_APP_TEMPLATES_REPO", templates_dir())
            .args([
                "new",
                "test-subst",
                "--type",
                kind,
                "--out",
                dest.to_str().unwrap(),
                "--no-deps-update",
            ])
            .assert()
            .success();

        let manifest = fs::read_to_string(dest.join("manifest.json")).unwrap();
        assert!(
            manifest.contains("\"name\": \"test-subst\""),
            "name not substituted in {} template: {}",
            kind,
            manifest
        );
        assert!(
            !manifest.contains("{{name}}"),
            "unreplaced {{name}} placeholder in {} template: {}",
            kind,
            manifest
        );
    }
}

#[test]
fn rejects_invalid_names() {
    let tmp = TempDir::new().unwrap();
    let bad = tmp.path().join("Bad-Name");
    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args(["new", "Bad-Name", "--type", "bun", "--out", bad.to_str().unwrap()])
        .assert()
        .failure();

    let prefixed = tmp.path().join("node-app-foo");
    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args([
            "new",
            "node-app-foo",
            "--type",
            "bun",
            "--out",
            prefixed.to_str().unwrap(),
        ])
        .assert()
        .failure();
}

#[test]
fn new_with_git_initializes_repo() {
    if std::process::Command::new("git")
        .arg("--version")
        .output()
        .is_err()
    {
        eprintln!("skipping: git not on PATH");
        return;
    }

    let tmp = TempDir::new().unwrap();
    let dest = tmp.path().join("git-test-app");

    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args([
            "new",
            "git-test-app",
            "--type",
            "bun",
            "--out",
            dest.to_str().unwrap(),
            "--git",
            "--no-deps-update",
        ])
        .assert()
        .success();

    assert!(dest.join(".git").is_dir(), ".git/ should exist after --git");

    let log = std::process::Command::new("git")
        .current_dir(&dest)
        .args(["log", "--oneline"])
        .output()
        .expect("git log");
    assert!(
        log.status.success(),
        "git log failed: {}",
        String::from_utf8_lossy(&log.stderr)
    );
    let out = String::from_utf8_lossy(&log.stdout);
    assert_eq!(
        out.lines().count(),
        1,
        "expected exactly one initial commit, got: {}",
        out
    );
    assert!(
        out.contains("git-test-app"),
        "commit msg should mention app name: {}",
        out
    );

    let branch = std::process::Command::new("git")
        .current_dir(&dest)
        .args(["branch", "--show-current"])
        .output()
        .expect("git branch");
    let branch_name = String::from_utf8_lossy(&branch.stdout).trim().to_string();
    assert_eq!(branch_name, "main", "expected branch=main, got: {}", branch_name);
}

// ── dev command: parser surface ────────────────────────────────────────────

#[test]
fn dev_help_renders() {
    bin().args(["dev", "--help"]).assert().success();
}

#[test]
fn dev_daemon_and_monorepo_are_mutually_exclusive() {
    bin()
        .args(["dev", "--daemon", "docker", "--monorepo", "/tmp"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("cannot be used with"));
}

#[test]
fn dev_unknown_daemon_value_errors_clearly() {
    let tmp = TempDir::new().unwrap();
    let dest = tmp.path().join("dev-bogus-daemon");
    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args([
            "new",
            "dev-bogus-daemon",
            "--type",
            "bun",
            "--out",
            dest.to_str().unwrap(),
            "--no-deps-update",
        ])
        .assert()
        .success();

    bin()
        .args(["dev", "-p", dest.to_str().unwrap(), "--daemon", "wat", "--once"])
        .assert()
        .failure()
        .stderr(predicates::str::contains("unknown --daemon value"));
}

#[test]
fn rejects_malformed_github_slug() {
    let tmp = TempDir::new().unwrap();
    let dest = tmp.path().join("malformed-test");

    // Missing slash.
    bin()
        .env("NODE_APP_TEMPLATES_REPO", templates_dir())
        .args([
            "new",
            "malformed-test",
            "--type",
            "bun",
            "--out",
            dest.to_str().unwrap(),
            "--github",
            "no-slash",
            "--no-deps-update",
        ])
        .assert()
        .failure()
        .stderr(predicates::str::contains("org/repo"));
}