Skip to content

test: test for heartbeat #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugin-api-test-client/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugin-api-test-client/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugin-api-test-client/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugin-api-test-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repositories {
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:3.4.2")
implementation("com.fasterxml.jackson.core:jackson-databind:2.18.2")
implementation("com.vaadin:vaadin-core:24.6.5")
implementation("com.vaadin:vaadin-core:24.7.4")
testImplementation("org.springframework.boot:spring-boot-starter-test:3.4.2")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ public RestClient.ResponseSpec delete(Path path) throws IOException {
return send("delete", new Message.DeleteMessage(path.toString()));
}

public RestClient.ResponseSpec heartbeat() throws IOException {
return send("heartbeat", new Message.HeartbeatMessage());
}

private RestClient.ResponseSpec send(String command, Object data) throws JsonProcessingException {
Message.CopilotRestRequest message = new Message.CopilotRestRequest(command, projectBasePath, data);
String body = new ObjectMapper().writeValueAsString(message);
org.springframework.web.client.RestClient.ResponseSpec response = org.springframework.web.client.RestClient.create().post()
return org.springframework.web.client.RestClient.create().post()
.uri(endpoint).contentType(MediaType.APPLICATION_JSON)
.body(body).retrieve();
return response;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vaadin.plugin;

import java.util.List;
import java.util.Set;

public class Message {

Expand Down Expand Up @@ -31,4 +32,10 @@ record CompileMessage(List<String> files) {
record DeleteMessage(String file) {
}

record HeartbeatMessage() {
}

record HeartbeatResponse(Boolean hasCompilationError, Set<String> filesContainCompilationError) {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.util.Base64;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.*;
import java.util.function.Predicate;

@SpringBootTest(classes = {SpringBootApplication.class})
Expand All @@ -33,6 +30,12 @@ protected Path getTestResourcePath(String childPath) {
.resolve(childPath);
}

protected Path getSourcePath(String childPath) {
return Path.of(projectBasePath)
.resolve("src/main/java")
.resolve(childPath);
}

@BeforeAll
public static void beforeAll() throws IOException {
projectBasePath = Path.of(System.getProperty("user.dir")).toString();
Expand Down Expand Up @@ -121,6 +124,38 @@ public void testUndoRedo() throws IOException, InterruptedException {
Assertions.assertFalse(performed);
}

// Run using Debug to trigger compile on save
@Test
public void testHeartbeat() throws IOException, InterruptedException {
var response = client.heartbeat();
var body = response.body(Message.HeartbeatResponse.class);
Assertions.assertFalse(body.hasCompilationError());
Assertions.assertEquals(Collections.EMPTY_SET, body.filesContainCompilationError());

var filePath = getSourcePath("com/vaadin/plugin/Test.java");
filePath.toFile().deleteOnExit();

response = client.write(filePath, "package com.vaadin.plugin;\n" +
"\n" +
"public class Test {\n" +
"asdasdasd" +
"}");
assertHttpOk(response);
Thread.sleep(2000);

for (int i = 0 ; i < 10 ; ++i) {
response = client.heartbeat();
body = response.body(Message.HeartbeatResponse.class);
if (body.hasCompilationError()) {
Assertions.assertEquals(Collections.EMPTY_SET, body.filesContainCompilationError());
return;
}
Thread.sleep(2000);
}

Assertions.fail("No compilation error received");
}

// add more tests when needed

private void assertNewFileCreated(Path file) {
Expand Down