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
//! Engine reply-channel tests (#27): app queries → bytes the consumer writes
//! back to the PTY, drained pull-style.
//!
//! Driven through the public API — feed the query an app emits, then drain the
//! reply. Reply bytes are the VT/DEC spec (DA1/DSR/DECRQM).
use justerm_core::Engine;
#[test]
fn da1_reports_device_attributes() {
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[c"); // primary DA (DA1) query
// justerm advertises VT220 (62) + ANSI colour (22) — the levels it genuinely
// implements; it does not claim Sixel/printer/etc. it does not do.
assert_eq!(t.drain_replies(), b"\x1b[?62;22c");
}
#[test]
fn dsr_reports_cursor_position() {
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[5;3H"); // CUP to row 5, col 3 (1-based)
t.feed(b"\x1b[6n"); // DSR cursor-position query
// Reply is 1-based row;col, matching the CUP coordinates.
assert_eq!(t.drain_replies(), b"\x1b[5;3R");
}
#[test]
fn dsr_reports_operating_status_ok() {
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[5n"); // DSR operating-status query
assert_eq!(t.drain_replies(), b"\x1b[0n"); // 0n = terminal OK
}
#[test]
fn decrqm_reports_mode_state() {
let mut t = Engine::new(80, 24);
// ?2004 (bracketed paste) starts reset → val 2.
t.feed(b"\x1b[?2004$p");
assert_eq!(t.drain_replies(), b"\x1b[?2004;2$y");
// After enabling it, the report flips to set → val 1.
t.feed(b"\x1b[?2004h\x1b[?2004$p");
assert_eq!(t.drain_replies(), b"\x1b[?2004;1$y");
// A mode the engine doesn't track → not recognized → val 0.
t.feed(b"\x1b[?9999$p");
assert_eq!(t.drain_replies(), b"\x1b[?9999;0$y");
}
#[test]
fn decrqm_reports_the_urxvt_encoding() {
// #51: the non-SGR encodings must be DECRQM-reportable too. With urxvt
// (?1015) active, querying it reports set — before the fix it answered
// "not recognized" (val 0).
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[?1015h\x1b[?1015$p"); // enable urxvt encoding, then query it
assert_eq!(t.drain_replies(), b"\x1b[?1015;1$y"); // set
}
#[test]
fn decrqm_reports_the_utf8_and_sgr_pixels_encodings() {
// The other two non-SGR encodings, each set when active (#51).
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[?1005h\x1b[?1005$p"); // UTF-8 encoding
assert_eq!(t.drain_replies(), b"\x1b[?1005;1$y");
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[?1016h\x1b[?1016$p"); // SGR-pixels encoding
assert_eq!(t.drain_replies(), b"\x1b[?1016;1$y");
}
#[test]
fn decrqm_encoding_is_single_state_and_defaults_reset() {
// The coordinate encoding is one-active-at-a-time, so querying an encoding
// that is NOT the active one reports reset — mirroring the protocol axis
// (?1000 vs ?1002). And ?1006 is unaffected by this change (#51).
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[?1005h"); // UTF-8 active
t.feed(b"\x1b[?1015$p\x1b[?1016$p\x1b[?1006$p"); // the other encodings
assert_eq!(
t.drain_replies(),
b"\x1b[?1015;2$y\x1b[?1016;2$y\x1b[?1006;2$y" // all reset
);
// ?1006 still reports set when SGR is the active encoding (unchanged).
t.feed(b"\x1b[?1006h\x1b[?1006$p");
assert_eq!(t.drain_replies(), b"\x1b[?1006;1$y");
// Default (X10) encoding: every encoding mode reports reset.
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[?1006$p\x1b[?1005$p\x1b[?1015$p\x1b[?1016$p");
assert_eq!(
t.drain_replies(),
b"\x1b[?1006;2$y\x1b[?1005;2$y\x1b[?1015;2$y\x1b[?1016;2$y"
);
}
#[test]
fn drain_empties_the_reply_buffer() {
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[c");
assert_eq!(t.drain_replies(), b"\x1b[?62;22c");
// A second drain with no new query is empty — replies are consumed, not
// re-sent (the consumer must not write them twice).
assert_eq!(t.drain_replies(), Vec::<u8>::new());
}
#[test]
fn unhandled_query_produces_no_reply() {
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[7n"); // DSR with an unsupported param
t.feed(b"\x1b[>q"); // XTVERSION — a `>`-prefixed final we deliberately do not answer
t.feed(b"plain text\r\n"); // ordinary output
assert_eq!(t.drain_replies(), Vec::<u8>::new());
}
// --- DA2, secondary device attributes (CSI > c) (#824) ---------------------
//
// `>` reaches `csi_dispatch` as an *intermediate*, and the dispatcher returns
// early on any intermediate it does not name — so DA2 was unreachable rather
// than unhandled, and vim could not identify the terminal. The route is opened
// for the `c` final only; every other `>`-prefixed final stays silent.
/// The version the reply must carry, derived here by arithmetic independent of
/// the engine's, so the two have to agree rather than share one expression.
fn expected_version() -> u32 {
let v = env!("CARGO_PKG_VERSION");
// Cut at whichever comes first — semver's pre-release `-` or its build
// metadata `+`. Splitting on `-` alone would disagree with the engine on
// `1.2.3+build.5`, where `"3+build".parse()` fails to 0 and this helper
// would report a *false red* the engine does not deserve.
let v = v.split(['-', '+']).next().unwrap();
let mut parts = v.split('.').map(|p| p.parse::<u32>().unwrap_or(0));
let major = parts.next().unwrap_or(0);
let minor = parts.next().unwrap_or(0);
let patch = parts.next().unwrap_or(0);
major * 10_000 + minor * 100 + patch
}
#[test]
fn da2_reports_secondary_device_attributes() {
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[>c"); // secondary DA (DA2) query — what vim sends
// Pp = 1 (VT220), matching what DA1 already advertises (62 = level 2);
// Pv = justerm's own crate version; Pc = 0, the ROM cartridge field the
// spec fixes at zero.
let want = format!("\x1b[>1;{};0c", expected_version());
assert_eq!(t.drain_replies(), want.as_bytes());
}
#[test]
fn da2_answers_an_explicit_zero_first_parameter() {
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[>0c"); // ctlseqs: "Ps = 0 or omitted -> request"
let want = format!("\x1b[>1;{};0c", expected_version());
assert_eq!(t.drain_replies(), want.as_bytes());
}
#[test]
fn da2_ignores_a_non_zero_first_parameter() {
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[>1c"); // a qualifier we do not recognise
t.feed(b"\x1b[>2c");
assert_eq!(t.drain_replies(), Vec::<u8>::new());
}
#[test]
fn da2_version_field_tracks_the_crate_version() {
// The engine must derive the number from its own crate version rather than
// carry a literal, so a release cannot ship a report that disagrees with it.
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[>c");
let reply = String::from_utf8(t.drain_replies()).unwrap();
let version = reply
.trim_start_matches("\x1b[>1;")
.trim_end_matches(";0c")
.parse::<u32>()
.expect("the version field is a single integer");
assert_eq!(version, expected_version());
// Padded base-100, so a higher semver always reports a higher number.
assert!(version >= 1_00, "a released version reports at least 1_00");
}
#[test]
fn da1_still_replies_exactly_as_before() {
// The dispatcher change opens one intermediate route; the unprefixed `c`
// must be untouched by it.
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[c");
assert_eq!(t.drain_replies(), b"\x1b[?62;22c");
}
#[test]
fn an_unrelated_prefixed_sequence_stays_silent_and_leaves_the_screen_alone() {
let mut t = Engine::new(80, 24);
t.feed(b"ab");
// XTMODKEYS first: it is the highest-reach `>` sequence justerm does not
// route — 7 occurrences across this repo's captures against DA2's 3 — so it
// is the one a widened prefix match would break in practice. XTVERSION and
// tertiary DA follow; all three are #47 tail material.
t.feed(b"\x1b[>4;2m"); // XTMODKEYS, what vim emits at startup
t.feed(b"\x1b[>4;m"); // and at exit
t.feed(b"\x1b[>q"); // XTVERSION
t.feed(b"\x1b[=c"); // tertiary DA
t.feed(b"\x1b[>5n");
assert_eq!(t.drain_replies(), Vec::<u8>::new());
let grid = t.grid();
let row: String = (0..grid.cols()).map(|c| grid.row(0)[c].c()).collect();
assert_eq!(row.trim_end(), "ab");
}
#[test]
fn da2_needs_the_prefix_alone_not_merely_a_leading_prefix() {
// The handler matches the whole intermediates slice, so a `>` followed by a
// real 0x20..0x2f intermediate is not DA2. Without this case the predicate
// is unfalsifiable: relaxing it to `intermediates.first() == Some(&b'>')`
// leaves every other test in the tree green, because nothing else in the
// suite feeds a prefixed sequence that also carries an intermediate.
//
// 3-1 among the references, and alacritty is the one that would answer, so
// this pins a divergence rather than a consensus.
let mut t = Engine::new(80, 24);
t.feed(b"\x1b[>$c");
t.feed(b"\x1b[> c");
assert_eq!(t.drain_replies(), Vec::<u8>::new());
}