Skip to content
Merged
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
@@ -0,0 +1,164 @@
.platformHeader {
display: flex;
align-items: center;
gap: 12px;
}

.platformMark {
display: inline-flex;
width: 42px;
height: 42px;
flex: 0 0 42px;
align-items: center;
justify-content: center;
border: 1px solid var(--border);
border-radius: 13px;
background: var(--muted);
font-size: 13px;
font-weight: 750;
}

.form {
display: flex;
min-height: 0;
flex-direction: column;
gap: 0;
}

.body {
display: flex;
max-height: min(66vh, 650px);
flex-direction: column;
gap: 22px;
overflow-y: auto;
padding-block: 4px 8px;
padding-inline: 1px;
}

.section {
display: flex;
flex-direction: column;
gap: 12px;
}

.sectionHeading {
display: flex;
align-items: center;
gap: 10px;
color: var(--muted-foreground);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}

.sectionHeading::after {
height: 1px;
flex: 1;
background: var(--border);
content: '';
}

.field {
display: flex;
flex-direction: column;
gap: 7px;
}

.fieldHeader {
display: flex;
min-height: 20px;
align-items: center;
justify-content: space-between;
gap: 10px;
}

.required {
color: var(--destructive);
}

.hint {
color: var(--muted-foreground);
font-size: 11px;
}

.secretState {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 10px 11px;
border: 1px solid var(--border);
border-radius: var(--radius-lg);
background: color-mix(in srgb, var(--muted) 52%, transparent);
}

.secretStatus {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted-foreground);
font-size: 12px;
}

.secretActions {
display: flex;
flex-wrap: wrap;
gap: 4px;
}

.policyGrid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 9px;
}

.policyCard {
display: flex;
min-height: 76px;
cursor: pointer;
align-items: flex-start;
gap: 10px;
padding: 12px;
border: 1px solid var(--border);
border-radius: var(--radius-lg);
background: var(--card);
transition:
border-color 120ms ease,
background-color 120ms ease;
}

.policyCard[data-selected='true'] {
border-color: var(--foreground);
background: color-mix(in srgb, var(--muted) 62%, transparent);
}

.policyCopy {
display: flex;
flex-direction: column;
gap: 4px;
}

.policyTitle {
font-size: 13px;
font-weight: 650;
}

.policyDescription {
color: var(--muted-foreground);
font-size: 11px;
line-height: 1.45;
}

@media (max-width: 520px) {
.policyGrid {
grid-template-columns: 1fr;
}
}

@media (prefers-reduced-motion: reduce) {
.policyCard {
transition: none;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/**
* @license
* Copyright 2026 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/

// @vitest-environment jsdom

import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type {
DaemonChannelInstanceSnapshot,
DaemonChannelTypeDescriptor,
} from '@qwen-code/sdk/daemon';

Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });

const DINGTALK: DaemonChannelTypeDescriptor = {
type: 'dingtalk',
displayName: 'DingTalk',
manageable: true,
fields: [
{
key: 'clientId',
label: 'Client ID',
kind: 'string',
required: true,
},
{
key: 'clientSecret',
label: 'Client Secret',
kind: 'secret',
required: true,
},
],
};

const OPTIONAL_SECRET: DaemonChannelTypeDescriptor = {
...DINGTALK,
fields: DINGTALK.fields.map((field) =>
field.key === 'clientSecret' ? { ...field, required: false } : field,
),
};

const INSTANCE: DaemonChannelInstanceSnapshot = {
name: 'release-bot',
config: {
type: 'dingtalk',
clientId: 'stored-id',
senderPolicy: 'open',
},
secrets: {
clientSecret: { present: true, source: 'environment' },
},
startsWithServe: false,
runtime: { state: 'stopped' },
};

const { ChannelEditorDialog } = await import('./ChannelEditorDialog');
const { I18nProvider } = await import('../../i18n');

let container: HTMLDivElement;
let root: Root;

async function renderDialog(
props: Partial<React.ComponentProps<typeof ChannelEditorDialog>> = {},
) {
await act(async () => {
root.render(
<I18nProvider language="en">
<ChannelEditorDialog
open
descriptor={DINGTALK}
expectedRevision="revision-1"
existingNames={[]}
onOpenChange={vi.fn()}
onSave={vi.fn().mockResolvedValue(undefined)}
onReload={vi.fn().mockResolvedValue(undefined)}
{...props}
/>
</I18nProvider>,
);
});
}

function inputByLabel(label: string): HTMLInputElement | null {
const labels = Array.from(document.querySelectorAll('label'));
const match = labels.find((item) => item.textContent?.includes(label));
const id = match?.htmlFor;
return id ? document.querySelector<HTMLInputElement>(`#${id}`) : null;
}

function setInputValue(input: HTMLInputElement, value: string) {
Object.getOwnPropertyDescriptor(
HTMLInputElement.prototype,
'value',
)?.set?.call(input, value);
input.dispatchEvent(new Event('input', { bubbles: true }));
}

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
root = createRoot(container);
});

afterEach(() => {
act(() => root.unmount());
container.remove();
document.body.innerHTML = '';
});

describe('ChannelEditorDialog', () => {
it('preserves a stored secret until Replace is explicitly selected', async () => {
await renderDialog({ instance: INSTANCE });

expect(document.body.textContent).toContain('Edit DingTalk');
expect(document.body.textContent).toContain('Stored in environment');
expect(document.body.textContent).not.toContain('Clear');
expect(inputByLabel('Client Secret')).toBeNull();

const replace = Array.from(document.querySelectorAll('button')).find(
(button) => button.textContent?.trim() === 'Replace',
);
await act(async () => {
replace?.click();
});

expect(inputByLabel('Client Secret')).not.toBeNull();
});

it('offers Clear for an optional stored secret', async () => {
await renderDialog({ descriptor: OPTIONAL_SECRET, instance: INSTANCE });

const clear = Array.from(document.querySelectorAll('button')).find(
(button) => button.textContent?.trim() === 'Clear',
);
expect(clear).toBeDefined();
});

it('submits a new instance with typed fields and the current revision', async () => {
const onSave = vi.fn().mockResolvedValue(undefined);
await renderDialog({ onSave });

const name = inputByLabel('Instance name');
const clientId = inputByLabel('Client ID');
const clientSecret = inputByLabel('Client Secret');
expect(name).not.toBeNull();
expect(clientId).not.toBeNull();
expect(clientSecret).not.toBeNull();

await act(async () => {
setInputValue(name!, 'release-bot');
setInputValue(clientId!, 'ding-client-id');
setInputValue(clientSecret!, 'ding-client-secret');
});

const save = Array.from(document.querySelectorAll('button')).find(
(button) => button.textContent?.trim() === 'Save',
);
await act(async () => {
save?.click();
});

expect(onSave).toHaveBeenCalledWith('release-bot', {
expectedRevision: 'revision-1',
config: {
type: 'dingtalk',
clientId: 'ding-client-id',
senderPolicy: 'pairing',
},
secrets: {
clientSecret: {
operation: 'replace',
value: 'ding-client-secret',
},
},
});
});

it('keeps the dialog open and offers a reload after a stale write', async () => {
const onSave = vi
.fn()
.mockRejectedValue(
new Error('Channel settings changed; reload before trying again.'),
);
await renderDialog({ instance: INSTANCE, onSave });

const save = Array.from(document.querySelectorAll('button')).find(
(button) => button.textContent?.trim() === 'Save',
);
await act(async () => {
save?.click();
});

expect(document.body.textContent).toContain(
'Channel settings changed; reload before trying again.',
);
expect(document.body.textContent).toContain('Reload latest');
expect(document.querySelector('[role="dialog"]')).not.toBeNull();
});

it('keeps the dialog open when reloading the latest configuration fails', async () => {
const onSave = vi.fn().mockRejectedValue(new Error('Revision conflict.'));
const onReload = vi
.fn()
.mockRejectedValue(new Error('Reload is temporarily unavailable.'));
await renderDialog({ instance: INSTANCE, onSave, onReload });

const save = Array.from(document.querySelectorAll('button')).find(
(button) => button.textContent?.trim() === 'Save',
);
await act(async () => {
save?.click();
});
const reload = Array.from(document.querySelectorAll('button')).find(
(button) => button.textContent?.trim() === 'Reload latest',
);
await act(async () => {
reload?.click();
});

expect(document.body.textContent).toContain(
'Reload is temporarily unavailable.',
);
expect(document.querySelector('[role="dialog"]')).not.toBeNull();
});
});
Loading
Loading