Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,47 @@
package com.microsoft.copilot.eclipse.core.lsp;

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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import com.google.gson.Gson;
import org.eclipse.core.resources.IFile;
import org.eclipse.e4.core.contexts.EclipseContextFactory;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.event.EventHandler;

import com.microsoft.copilot.eclipse.core.CopilotCore;
import com.microsoft.copilot.eclipse.core.FeatureFlags;
import com.microsoft.copilot.eclipse.core.chat.service.IChatServiceManager;
import com.microsoft.copilot.eclipse.core.chat.service.IReferencedFileService;
import com.microsoft.copilot.eclipse.core.events.CopilotEventConstants;
import com.microsoft.copilot.eclipse.core.lsp.protocol.ConversationCapabilities;
import com.microsoft.copilot.eclipse.core.lsp.protocol.ConversationContextParams;
import com.microsoft.copilot.eclipse.core.lsp.protocol.CurrentEditorContext;
import com.microsoft.copilot.eclipse.core.lsp.protocol.DidChangeFeatureFlagsParams;
import com.microsoft.copilot.eclipse.core.lsp.protocol.policy.DidChangePolicyParams;
import com.microsoft.copilot.eclipse.core.utils.FileUtils;

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -131,4 +144,53 @@ void testOnDidChangeFeatureFlagsWithEmptyFeatureFlags() {
verify(mockFeatureFlags).setByokEnabled(true);
}
}

@Test
void testOnDidChangePolicy_publishesAutoModelPolicyEventOnlyWhenValueChanges() throws InterruptedException {
IEventBroker eventBroker = EclipseContextFactory
.getServiceContext(FrameworkUtil.getBundle(getClass()).getBundleContext())
.get(IEventBroker.class);
assertNotNull(eventBroker);

CountDownLatch eventReceived = new CountDownLatch(1);
CountDownLatch duplicateEventReceived = new CountDownLatch(1);
AtomicInteger eventCount = new AtomicInteger();
AtomicReference<Object> eventData = new AtomicReference<>();
EventHandler eventHandler = event -> {
eventData.set(event.getProperty(IEventBroker.DATA));
if (eventCount.incrementAndGet() == 1) {
eventReceived.countDown();
} else {
duplicateEventReceived.countDown();
}
};
eventBroker.subscribe(CopilotEventConstants.TOPIC_DID_CHANGE_AUTO_MODEL_POLICY, eventHandler);

DidChangePolicyParams params = new Gson().fromJson("""
{
"mcp.contributionPoint.enabled": false,
"customAgent.enabled": true,
"agentMode.autoApproval.enabled": true,
"autoModel.enabled": false
}
""", DidChangePolicyParams.class);
FeatureFlags featureFlags = new FeatureFlags();

try (MockedStatic<CopilotCore> copilotCoreMock = Mockito.mockStatic(CopilotCore.class)) {
copilotCoreMock.when(CopilotCore::getPlugin).thenReturn(plugin);
when(plugin.getFeatureFlags()).thenReturn(featureFlags);

client.onDidChangePolicy(params);

assertTrue(eventReceived.await(5, TimeUnit.SECONDS));
assertEquals(Boolean.FALSE, eventData.get());

client.onDidChangePolicy(params);

assertFalse(duplicateEventReceived.await(500, TimeUnit.MILLISECONDS));
assertEquals(1, eventCount.get());
} finally {
eventBroker.unsubscribe(eventHandler);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class FeatureFlags {

private boolean autoApprovalPolicyEnabled = true;

private boolean autoModelPolicyEnabled = true;

public boolean isAgentModeEnabled() {
return agentModeEnabled;
}
Expand Down Expand Up @@ -85,6 +87,14 @@ public void setAutoApprovalPolicyEnabled(boolean autoApprovalPolicyEnabled) {
this.autoApprovalPolicyEnabled = autoApprovalPolicyEnabled;
}

public boolean isAutoModelPolicyEnabled() {
return autoModelPolicyEnabled;
}

public void setAutoModelPolicyEnabled(boolean autoModelPolicyEnabled) {
this.autoModelPolicyEnabled = autoModelPolicyEnabled;
}

public boolean isClientPreviewFeatureEnabled() {
return clientPreviewFeatureEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public class CopilotEventConstants {
*/
public static final String TOPIC_DID_CHANGE_CUSTOM_AGENT_POLICY = TOPIC_POLICY + "CUSTOM_AGENT_ENABLED";

/**
* Event when the Auto model policy flag is updated.
*/
public static final String TOPIC_DID_CHANGE_AUTO_MODEL_POLICY = TOPIC_POLICY + "AUTO_MODEL_ENABLED";

/**
* Event when the chat mode is changed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,12 @@ public void onDidChangePolicy(DidChangePolicyParams params) {
eventBroker.post(CopilotEventConstants.TOPIC_DID_CHANGE_CUSTOM_AGENT_POLICY, params.isCustomAgentEnabled());
}
flags.setAutoApprovalPolicyEnabled(params.isAutoApprovalPolicyEnabled());
if (flags.isAutoModelPolicyEnabled() != params.isAutoModelEnabled()) {
flags.setAutoModelPolicyEnabled(params.isAutoModelEnabled());
if (eventBroker != null) {
eventBroker.post(CopilotEventConstants.TOPIC_DID_CHANGE_AUTO_MODEL_POLICY, params.isAutoModelEnabled());
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class DidChangePolicyParams {
@SerializedName("agentMode.autoApproval.enabled")
private boolean autoApprovalPolicyEnabled = true;

@SerializedName("autoModel.enabled")
private boolean autoModelEnabled = true;

public boolean isMcpContributionPointEnabled() {
return mcpContributionPointEnabled;
}
Expand All @@ -46,10 +49,18 @@ public void setAutoApprovalPolicyEnabled(boolean autoApprovalPolicyEnabled) {
this.autoApprovalPolicyEnabled = autoApprovalPolicyEnabled;
}

public boolean isAutoModelEnabled() {
return autoModelEnabled;
}

public void setAutoModelEnabled(boolean autoModelEnabled) {
this.autoModelEnabled = autoModelEnabled;
}

@Override
public int hashCode() {
return Objects.hash(mcpContributionPointEnabled, customAgentEnabled,
autoApprovalPolicyEnabled);
autoApprovalPolicyEnabled, autoModelEnabled);
}

@Override
Expand All @@ -66,7 +77,8 @@ public boolean equals(Object obj) {
DidChangePolicyParams other = (DidChangePolicyParams) obj;
return mcpContributionPointEnabled == other.mcpContributionPointEnabled
&& customAgentEnabled == other.customAgentEnabled
&& autoApprovalPolicyEnabled == other.autoApprovalPolicyEnabled;
&& autoApprovalPolicyEnabled == other.autoApprovalPolicyEnabled
&& autoModelEnabled == other.autoModelEnabled;
}

@Override
Expand All @@ -75,6 +87,7 @@ public String toString() {
builder.append("mcpContributionPointEnabled", mcpContributionPointEnabled);
builder.append("customAgentEnabled", customAgentEnabled);
builder.append("autoApprovalPolicyEnabled", autoApprovalPolicyEnabled);
builder.append("autoModelEnabled", autoModelEnabled);
return builder.toString();
}
}
Loading
Loading