twin-cli 0.2.0

Git worktree wrapper with side effects (symlinks and hooks)
Documentation
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
/// 基本的なE2Eテスト - 実際の動作を確認
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;

/// twinバイナリのパスを取得
fn get_twin_binary() -> PathBuf {
    // OSに応じて適切なバイナリ名を選択
    let binary_name = if cfg!(windows) { "twin.exe" } else { "twin" };

    // テスト実行ファイルから相対的にバイナリを探す
    let current_exe = std::env::current_exe().expect("Failed to get current exe path");

    // target/debug/deps/test-xxx から target/debug/twin へ
    let path = current_exe
        .parent() // deps
        .and_then(|p| p.parent()) // debug
        .map(|p| p.join(binary_name))
        .expect("Failed to construct twin binary path");

    if !path.exists() {
        // デバッグ情報を出力
        eprintln!("Current exe: {current_exe:?}");
        eprintln!("Looking for twin binary at: {path:?}");
        eprintln!("Current dir: {:?}", std::env::current_dir().ok());
        panic!("twin binary not found. Run 'cargo build' first.");
    }
    path
}

/// テスト用のGitリポジトリをセットアップ
fn setup_git_repo() -> TempDir {
    let dir = TempDir::new().unwrap();

    // Git初期化(デフォルトブランチ名を明示的に指定)
    Command::new("git")
        .args(["init", "-b", "main"])
        .current_dir(dir.path())
        .output()
        .expect("git init failed");

    // Git設定
    Command::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    Command::new("git")
        .args(["config", "user.name", "Test User"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    // 初期ファイルとコミット
    std::fs::write(dir.path().join("README.md"), "# Test").unwrap();

    Command::new("git")
        .args(["add", "."])
        .current_dir(dir.path())
        .output()
        .unwrap();

    Command::new("git")
        .args(["commit", "-m", "initial"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    dir
}

#[test]
fn test_help_command() {
    let twin = get_twin_binary();

    let output = Command::new(&twin)
        .arg("--help")
        .output()
        .expect("Failed to run twin --help");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("create"));
    assert!(stdout.contains("list"));
    assert!(stdout.contains("remove"));
}

#[test]
fn test_create_environment() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // 環境を作成
    let output = Command::new(&twin)
        .args(["create", "test-env"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to run twin create");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("STDOUT: {stdout}");
    println!("STDERR: {stderr}");

    // 成功メッセージまたはWorktreeパスが出力されることを確認
    assert!(
        stdout.contains("環境") || stdout.contains("Worktree") || output.status.success(),
        "Environment creation should succeed or show appropriate message"
    );

    // Git worktreeが作成されたか確認
    let worktree_output = Command::new("git")
        .args(["worktree", "list"])
        .current_dir(repo.path())
        .output()
        .unwrap();

    let worktree_list = String::from_utf8_lossy(&worktree_output.stdout);
    println!("Worktrees: {worktree_list}");

    // test-envまたはagent/test-envブランチが存在することを確認
    assert!(
        worktree_list.contains("test-env") || worktree_list.contains("agent"),
        "Worktree should be created"
    );
}

#[test]
fn test_list_environments() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // まず環境を作成
    Command::new(&twin)
        .args(["create", "list-test"])
        .current_dir(repo.path())
        .output()
        .unwrap();

    // リストコマンドを実行
    let output = Command::new(&twin)
        .args(["list"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to run twin list");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    println!("List output: {stdout}");

    // 何らかの出力があることを確認(空でもOK - レジストリが未実装の可能性)
}

#[test]
fn test_remove_environment() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // 環境を作成
    Command::new(&twin)
        .args(["create", "remove-test"])
        .current_dir(repo.path())
        .output()
        .unwrap();

    // 環境を削除(--forceで確認をスキップ)
    let output = Command::new(&twin)
        .args(["remove", "remove-test", "--force"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to run twin remove");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Remove STDOUT: {stdout}");
    println!("Remove STDERR: {stderr}");

    // 削除メッセージまたは成功を確認
    assert!(
        stdout.contains("削除") || output.status.success(),
        "Remove should complete"
    );
}

#[test]
fn test_config_with_symlinks() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // 設定ファイルを作成(hooksフィールドを省略)
    let config_content = r#"
[[files]]
path = ".env"
mapping_type = "symlink"
description = "Environment variables"

[[files]]
path = "config.json"
mapping_type = "copy"
description = "Configuration file"
"#;

    std::fs::write(repo.path().join(".twin.toml"), config_content).unwrap();

    // ソースファイルを作成
    std::fs::write(repo.path().join(".env"), "TEST=true").unwrap();
    std::fs::write(repo.path().join("config.json"), r#"{"test": true}"#).unwrap();

    // 設定ファイルを指定して環境を作成
    let output = Command::new(&twin)
        .args(["create", "config-test", "--config", ".twin.toml"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to create with config");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Config create STDOUT: {stdout}");
    println!("Config create STDERR: {stderr}");

    // 作成が成功することを確認
    assert!(
        output.status.success() || stdout.contains("環境"),
        "Should create environment with config"
    );

    // Worktreeパスを取得して、シンボリックリンクが作成されたか確認
    // 注: 現在の実装では完全に動作しない可能性がある
}

#[test]
fn test_partial_config_file() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // 最小限の設定ファイル(filesだけ)
    let config_content = r#"
[[files]]
path = "test.txt"
"#;

    std::fs::write(repo.path().join("minimal.toml"), config_content).unwrap();
    std::fs::write(repo.path().join("test.txt"), "test content").unwrap();

    // 最小限の設定で環境作成
    let output = Command::new(&twin)
        .args(["create", "minimal-test", "--config", "minimal.toml"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to create with minimal config");

    assert!(
        output.status.success(),
        "Should create environment with minimal config"
    );

    // hooksだけの設定ファイル
    let hooks_content = r#"
[hooks]
post_create = [{command = "echo 'Created!'"}]
"#;

    std::fs::write(repo.path().join("hooks.toml"), hooks_content).unwrap();

    // hooksだけの設定で環境作成
    let output = Command::new(&twin)
        .args(["create", "hooks-test", "--config", "hooks.toml"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to create with hooks config");

    let stderr = String::from_utf8_lossy(&output.stderr);

    // post_createフックが実行されることを確認(または成功)
    assert!(
        output.status.success() || stderr.contains("Created!"),
        "Should create environment with hooks config"
    );
}

#[test]
fn test_hook_execution() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // フック付き設定ファイル
    let config_content = r#"
[hooks]
pre_create = [
    {command = "echo Pre-create hook running"}
]
post_create = [
    {command = "echo Post-create hook completed"}
]

[[files]]
path = "dummy.txt"
"#;

    std::fs::write(repo.path().join("hook-test.toml"), config_content).unwrap();
    std::fs::write(repo.path().join("dummy.txt"), "dummy").unwrap();

    // 環境作成(フック実行)
    let output = Command::new(&twin)
        .args(["create", "hook-env", "--config", "hook-test.toml"])
        .env("TWIN_VERBOSE", "1") // Verboseモードでフック実行を確認
        .current_dir(repo.path())
        .output()
        .expect("Failed to create with hooks");

    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);

    println!("Hook test STDOUT: {stdout}");
    println!("Hook test STDERR: {stderr}");

    // フック実行または成功を確認
    assert!(
        output.status.success() || stdout.contains("hook") || stderr.contains("hook"),
        "Hooks should be executed or environment created successfully"
    );
}

#[test]
fn test_json_output_format() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // JSON形式でリスト表示
    let output = Command::new(&twin)
        .args(["list", "--format", "json"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to list with JSON format");

    assert!(output.status.success());

    let stdout = String::from_utf8_lossy(&output.stdout);
    println!("JSON output: {stdout}");

    // 空の配列またはJSON形式であることを確認
    let trimmed = stdout.trim();
    assert!(
        trimmed.is_empty() || trimmed.starts_with('[') || trimmed.starts_with('{'),
        "Should output JSON format or be empty"
    );
}

#[test]
fn test_verbose_logging() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // TWIN_VERBOSE環境変数を設定して実行
    let output = Command::new(&twin)
        .args(["create", "verbose-test"])
        .env("TWIN_VERBOSE", "1")
        .current_dir(repo.path())
        .output()
        .expect("Failed to run with verbose");

    let stderr = String::from_utf8_lossy(&output.stderr);

    // Verbose出力があるか確認(🔧などの絵文字が含まれる)
    if stderr.contains("🔧") || stderr.contains("実行中") {
        println!("Verbose logging is working");
    } else {
        println!("Verbose logging may not be fully implemented");
    }
}

#[test]
fn test_branch_naming() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // デフォルトブランチ名で作成
    Command::new(&twin)
        .args(["create", "branch-test"])
        .current_dir(repo.path())
        .output()
        .unwrap();

    // ブランチ一覧を確認
    let output = Command::new("git")
        .args(["branch", "-a"])
        .current_dir(repo.path())
        .output()
        .unwrap();

    let branches = String::from_utf8_lossy(&output.stdout);
    println!("Branches: {branches}");

    // agent/branch-testまたはbranch-testブランチが存在
    assert!(
        branches.contains("branch-test") || branches.contains("agent"),
        "Branch should be created with appropriate name"
    );
}

#[test]
fn test_custom_branch_name() {
    let repo = setup_git_repo();
    let twin = get_twin_binary();

    // カスタムブランチ名で作成(-bオプションを使用)
    let output = Command::new(&twin)
        .args(["create", "-b", "feature/my-branch", "../custom"])
        .current_dir(repo.path())
        .output()
        .expect("Failed to create with custom branch");

    let stdout = String::from_utf8_lossy(&output.stdout);
    println!("Custom branch output: {stdout}");

    // ブランチを確認
    let branch_output = Command::new("git")
        .args(["branch", "-a"])
        .current_dir(repo.path())
        .output()
        .unwrap();

    let branches = String::from_utf8_lossy(&branch_output.stdout);
    assert!(
        branches.contains("feature/my-branch"),
        "Custom branch should be created"
    );
}