-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinteractive-option.test.mjs
More file actions
109 lines (87 loc) · 4.57 KB
/
interactive-option.test.mjs
File metadata and controls
109 lines (87 loc) · 4.57 KB
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
import { $ } from '../src/$.mjs';
import { test, expect, describe, beforeEach, afterEach } from 'bun:test';
import { beforeTestCleanup, afterTestCleanup } from './test-cleanup.mjs';
describe('Interactive Option Tests', () => {
beforeEach(beforeTestCleanup);
afterEach(afterTestCleanup);
test('interactive option - default behavior', async () => {
// Test that interactive is false by default
const $custom = $({ capture: true, mirror: false });
const cmd = $custom`echo test`;
// Interactive should be false by default
expect(cmd.options.interactive).toBe(false);
});
test('interactive option - explicit true', async () => {
// Test that interactive can be set to true
const $custom = $({ capture: true, mirror: false, interactive: true });
const cmd = $custom`echo test`;
// Interactive should be true when explicitly set
expect(cmd.options.interactive).toBe(true);
});
test('interactive option - explicit false', async () => {
// Test that interactive can be explicitly set to false
const $custom = $({ capture: true, mirror: false, interactive: false });
const cmd = $custom`echo test`;
// Interactive should be false when explicitly set
expect(cmd.options.interactive).toBe(false);
});
test('interactive option - passed through options merge', async () => {
// Test that interactive option is preserved when merging options
const $base = $({ capture: true, mirror: false });
const cmd = $base`echo test`;
// Start with different options to test merging
cmd.start({ interactive: true });
// Interactive should be true after options merge
expect(cmd.options.interactive).toBe(true);
});
test('interactive option - does not affect command execution with pipes/capture', async () => {
// Test that setting interactive: true doesn't interfere with normal command execution
// when pipes are used (which prevents TTY forwarding anyway)
const $interactive = $({ capture: true, mirror: false, interactive: true });
const result = await $interactive`echo "interactive test"`;
expect(result.code).toBe(0);
expect(result.stdout.trim()).toBe('interactive test');
});
test('interactive option - behavior with stdin inherit but no TTY', async () => {
// Test that interactive mode requires both interactive:true AND TTY conditions
// This test verifies the logic but won't actually use TTY in test environment
const $interactive = $({ capture: false, mirror: false, interactive: true, stdin: 'inherit' });
const cmd = $interactive`echo "tty test"`;
// Should still work even if TTY conditions aren't met
expect(cmd.options.interactive).toBe(true);
expect(cmd.options.stdin).toBe('inherit');
});
test('interactive option - works with template literal syntax', async () => {
// Test interactive option with various template literal syntaxes
const name = 'world';
const $interactive = $({ capture: true, mirror: false, interactive: true });
const cmd = $interactive`echo "hello ${name}"`;
const result = await cmd;
expect(result.code).toBe(0);
expect(result.stdout.trim()).toBe("hello world"); // Safe string 'world' doesn't need quotes
expect(cmd.options.interactive).toBe(true); // Check the command object, not the result
});
test('interactive option - preserved in command chaining', async () => {
// Test that interactive option is preserved through command operations
const $interactive = $({ capture: true, mirror: false, interactive: true });
const cmd1 = $interactive`echo "first"`;
const cmd2 = cmd1.pipe($interactive`tr 'a-z' 'A-Z'`);
// Both commands should preserve the interactive setting
expect(cmd1.options.interactive).toBe(true);
// Note: cmd2 (piped command) might have different options, that's expected
});
test('interactive option - type checking', async () => {
// Test that interactive option accepts boolean values properly
const $true = $({ interactive: true, capture: true, mirror: false });
const $false = $({ interactive: false, capture: true, mirror: false });
const $default = $({ capture: true, mirror: false });
expect($true`echo test`.options.interactive).toBe(true);
expect($false`echo test`.options.interactive).toBe(false);
expect($default`echo test`.options.interactive).toBe(false);
// Test with non-boolean values (should still work, JavaScript is flexible)
const $truthy = $({ interactive: 1, capture: true, mirror: false });
const $falsy = $({ interactive: 0, capture: true, mirror: false });
expect(Boolean($truthy`echo test`.options.interactive)).toBe(true);
expect(Boolean($falsy`echo test`.options.interactive)).toBe(false);
});
});