File size: 7,188 Bytes
04f192c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

package copilotj;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import copilotj.ImagejListener.HistoryRequest;
import copilotj.ScreenCapturer.CaptureImageRequest;
import copilotj.ScriptRunner.PluginRequest;
import copilotj.ScriptRunner.Result;
import copilotj.ScriptRunner.ScriptRequest;
import copilotj.SnapshotManager.ActionRequest;
import copilotj.SnapshotManager.CompareRequest;
import copilotj.Summerizer.EnvironmentSummary;
import copilotj.util.Trie;

/**
 * Protocol tests for {@link EventHandler}'s JSON wire format. Exercises the exact
 * ObjectMapper configuration production uses via the package-private
 * {@link EventHandler#configureMapper(ObjectMapper)} seam, WITHOUT ever constructing an
 * EventHandler — its constructor cascades into ImageJ statics and cannot run headlessly.
 * The dispatch {@code handle(Payload)} is covered by Layer C.
 */
public class EventHandlerProtocolTest {

  private final ObjectMapper mapper = EventHandler.configureMapper(new ObjectMapper());

  /** Serialize to a tree for stable, substring-free key assertions. */
  private JsonNode ser(final Object value) throws Exception {
    return mapper.readTree(mapper.writeValueAsString(value));
  }

  // ===== Payload =====

  @Test
  public void payloadSerializesSnakeCaseKeys() throws Exception {
    final EventHandler.Payload p = new EventHandler.Payload();
    p.id = "id-1";
    p.event_id = "eid-1";
    p.event = "take_snapshot";
    final JsonNode tree = ser(p);
    assertEquals("id-1", tree.get("id").asText());
    assertEquals("eid-1", tree.get("event_id").asText());
    assertEquals("take_snapshot", tree.get("event").asText());
    assertFalse(tree.has("data") && !tree.get("data").isNull(), "absent data omitted");
  }

  @Test
  public void payloadRoundTripsThroughSnakeCase() throws Exception {
    final String json = "{\"event\":\"run_script\",\"event_id\":\"e1\",\"id\":\"i1\","
        + "\"data\":{\"x\":1},\"err\":null}";
    final EventHandler.Payload p = mapper.readValue(json, EventHandler.Payload.class);
    assertEquals("run_script", p.event);
    assertEquals("e1", p.event_id);
    assertEquals("i1", p.id);
    assertNotNull(p.data);
    assertEquals(1, p.data.get("x").asInt());
    assertNull(p.err);
  }

  // ===== IdChanged =====

  @Test
  public void idChangedRoundTrip() throws Exception {
    final EventHandler.IdChanged ic = mapper.readValue("{\"id\":\"abc\"}", EventHandler.IdChanged.class);
    assertEquals("abc", ic.id);
  }

  // ===== SnapshotManager DTOs =====

  @Test
  public void compareRequestSnakeCase() throws Exception {
    final CompareRequest req = mapper.readValue("{\"id_early\":2,\"id_later\":5}", CompareRequest.class);
    assertEquals(Integer.valueOf(2), req.idEarly);
    assertEquals(Integer.valueOf(5), req.idLater);
    assertTrue(ser(req).has("id_early"));
    assertTrue(ser(req).has("id_later"));
  }

  @Test
  public void actionRequestSnakeCase() throws Exception {
    final ActionRequest req = mapper.readValue("{\"snapshot_id\":3,\"action_id\":7,\"parameters\":[\"a\",1]}",
        ActionRequest.class);
    assertEquals(3, req.snapshotId);
    assertEquals(7, req.actionId);
    assertEquals(2, req.parameters.size());
    assertEquals("a", req.parameters.get(0));
    assertEquals(Integer.valueOf(1), req.parameters.get(1));
    assertTrue(ser(req).has("snapshot_id"));
    assertTrue(ser(req).has("action_id"));
  }

  // ===== ScreenCapturer / ScriptRunner DTOs =====

  @Test
  public void captureImageRequestRoundTrip() throws Exception {
    final CaptureImageRequest req = mapper.readValue("{\"title\":\"foo.png\"}", CaptureImageRequest.class);
    assertEquals("foo.png", req.title);
  }

  @Test
  public void scriptRequestRoundTrip() throws Exception {
    final ScriptRequest req =
        mapper.readValue("{\"language\":\"groovy\",\"script\":\"print 1\",\"timeout\":30}", ScriptRequest.class);
    assertEquals("groovy", req.language);
    assertEquals("print 1", req.script);
    assertEquals(30, req.timeout);
  }

  @Test
  public void pluginRequestSnakeCaseIj1Style() throws Exception {
    final PluginRequest req = mapper.readValue("{\"plugin\":\"P\",\"ij1_style\":false}", PluginRequest.class);
    assertEquals("P", req.plugin);
    assertFalse(req.ij1Style);
    assertTrue(ser(req).has("ij1_style"));
  }

  @Test
  public void resultSerializesErrKey() throws Exception {
    final JsonNode tree = ser(new Result("boom"));
    assertEquals("boom", tree.get("err").asText());
  }

  // ===== Summerizer.EnvironmentSummary (serialize-only; ctor-based) =====

  @Test
  public void environmentSummarySerializesSnakeCase() throws Exception {
    final Map<String, Trie.SentenceTrie> plugins = new LinkedHashMap<>();
    final JsonNode tree = ser(new EnvironmentSummary("/ij", "/java", 21, plugins));
    assertEquals("/ij", tree.get("imagej_home").asText());
    assertEquals("/java", tree.get("java_home").asText());
    assertEquals(21, tree.get("java_version").asInt());
    assertTrue(tree.has("plugins"));
  }

  // ===== ImagejListener.HistoryRequest (FlexibleLocalDateTimeDeserializer) =====

  @Test
  public void historyRequestParsesIsoDate() throws Exception {
    final HistoryRequest req = mapper.readValue("{\"since\":\"2026-06-30T12:34:56\"}", HistoryRequest.class);
    assertNotNull(req.since);
    assertEquals(LocalDateTime.of(2026, 6, 30, 12, 34, 56), req.since);
  }

  @Test
  public void historyRequestParsesIsoWithFractional() throws Exception {
    // The flexible deserializer accepts fractional seconds (and other variants).
    final HistoryRequest req = mapper.readValue("{\"since\":\"2026-06-30T12:34:56.123\"}", HistoryRequest.class);
    assertNotNull(req.since);
  }

  // ===== connected-event construction (exercises the real buildConnectedPayload branch) =====

  @Test
  public void connectedPayloadWithIdIsNegotiateId() {
    final EventHandler.Payload p = EventHandler.buildConnectedPayload("plugin-42", mapper);
    assertEquals("negotiate_id", p.event);
    assertNotNull(p.data);
    assertEquals("plugin-42", p.data.get("id").asText());
  }

  @Test
  public void connectedPayloadWithoutIdIsQueryId() {
    final EventHandler.Payload p = EventHandler.buildConnectedPayload(null, mapper);
    assertEquals("query_id", p.event);
    assertNull(p.data);
  }

  @Test
  public void connectedPayloadWithEmptyIdIsQueryId() {
    // The branch treats an empty id the same as null (no negotiation).
    final EventHandler.Payload p = EventHandler.buildConnectedPayload("", mapper);
    assertEquals("query_id", p.event);
    assertNull(p.data);
  }
}