forked from github/copilot-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageAttachmentTest.java
More file actions
158 lines (127 loc) · 6.31 KB
/
MessageAttachmentTest.java
File metadata and controls
158 lines (127 loc) · 6.31 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
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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot.sdk;
import static org.junit.jupiter.api.Assertions.*;
import java.util.List;
import org.junit.jupiter.api.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.copilot.sdk.json.Attachment;
import com.github.copilot.sdk.json.BlobAttachment;
import com.github.copilot.sdk.json.MessageAttachment;
import com.github.copilot.sdk.json.MessageOptions;
import com.github.copilot.sdk.json.SendMessageRequest;
/**
* Tests for the {@link MessageAttachment} sealed interface and type-safe
* attachment handling.
*/
class MessageAttachmentTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
// =========================================================================
// Sealed interface hierarchy
// =========================================================================
@Test
void attachmentImplementsMessageAttachment() {
Attachment attachment = new Attachment("file", "/path/to/file.java", "Source");
assertInstanceOf(MessageAttachment.class, attachment);
assertEquals("file", attachment.getType());
}
@Test
void blobAttachmentImplementsMessageAttachment() {
BlobAttachment blob = new BlobAttachment().setData("aGVsbG8=").setMimeType("image/png")
.setDisplayName("test.png");
assertInstanceOf(MessageAttachment.class, blob);
assertEquals("blob", blob.getType());
}
// =========================================================================
// MessageOptions type safety
// =========================================================================
@Test
void setAttachmentsAcceptsListOfAttachment() {
MessageOptions options = new MessageOptions();
List<Attachment> list = List.of(new Attachment("file", "/a.java", "A"));
options.setAttachments(list);
assertEquals(1, options.getAttachments().size());
assertInstanceOf(Attachment.class, options.getAttachments().get(0));
}
@Test
void setAttachmentsAcceptsListOfBlobAttachment() {
MessageOptions options = new MessageOptions();
List<BlobAttachment> list = List.of(new BlobAttachment().setData("ZGF0YQ==").setMimeType("image/jpeg"));
options.setAttachments(list);
assertEquals(1, options.getAttachments().size());
assertInstanceOf(BlobAttachment.class, options.getAttachments().get(0));
}
@Test
void setAttachmentsAcceptsMixedList() {
MessageOptions options = new MessageOptions();
List<MessageAttachment> mixed = List.of(new Attachment("file", "/a.java", "A"),
new BlobAttachment().setData("ZGF0YQ==").setMimeType("image/png"));
options.setAttachments(mixed);
assertEquals(2, options.getAttachments().size());
assertInstanceOf(Attachment.class, options.getAttachments().get(0));
assertInstanceOf(BlobAttachment.class, options.getAttachments().get(1));
}
@Test
void setAttachmentsHandlesNull() {
MessageOptions options = new MessageOptions();
options.setAttachments(null);
assertNull(options.getAttachments());
}
@Test
void getAttachmentsReturnsUnmodifiableList() {
MessageOptions options = new MessageOptions();
options.setAttachments(List.of(new Attachment("file", "/a.java", "A")));
assertThrows(UnsupportedOperationException.class,
() -> options.getAttachments().add(new Attachment("file", "/b.java", "B")));
}
// =========================================================================
// SendMessageRequest type safety
// =========================================================================
@Test
void sendMessageRequestAcceptsMessageAttachmentList() {
SendMessageRequest request = new SendMessageRequest();
List<MessageAttachment> list = List.of(new Attachment("file", "/a.java", "A"),
new BlobAttachment().setData("ZGF0YQ==").setMimeType("image/png"));
request.setAttachments(list);
assertEquals(2, request.getAttachments().size());
}
// =========================================================================
// Jackson serialization
// =========================================================================
@Test
void serializeAttachmentIncludesType() throws Exception {
Attachment attachment = new Attachment("file", "/path/to/file.java", "Source");
String json = MAPPER.writeValueAsString(attachment);
assertTrue(json.contains("\"type\":\"file\""));
assertTrue(json.contains("\"path\":\"/path/to/file.java\""));
}
@Test
void serializeBlobAttachmentIncludesType() throws Exception {
BlobAttachment blob = new BlobAttachment().setData("aGVsbG8=").setMimeType("image/png")
.setDisplayName("test.png");
String json = MAPPER.writeValueAsString(blob);
assertTrue(json.contains("\"type\":\"blob\""));
assertTrue(json.contains("\"data\":\"aGVsbG8=\""));
assertTrue(json.contains("\"mimeType\":\"image/png\""));
}
@Test
void serializeMessageOptionsWithMixedAttachments() throws Exception {
MessageOptions options = new MessageOptions().setPrompt("Describe")
.setAttachments(List.of(new Attachment("file", "/a.java", "A"),
new BlobAttachment().setData("ZGF0YQ==").setMimeType("image/png").setDisplayName("img.png")));
String json = MAPPER.writeValueAsString(options);
assertTrue(json.contains("\"type\":\"file\""));
assertTrue(json.contains("\"type\":\"blob\""));
}
@Test
void cloneMessageOptionsPreservesAttachments() {
MessageOptions original = new MessageOptions().setPrompt("test")
.setAttachments(List.of(new Attachment("file", "/a.java", "A")));
MessageOptions cloned = original.clone();
assertEquals(1, cloned.getAttachments().size());
assertInstanceOf(Attachment.class, cloned.getAttachments().get(0));
// Verify clone is independent
assertNotSame(original.getAttachments(), cloned.getAttachments());
}
}