// @vitest-environment jsdom
import { cleanup, render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { InstanceContext } from '../app-shell.js';
import { MessagesModule, type MessagesDataSource } from './messages-module.js';
const instance: InstanceContext = {
id: 'alpha',
name: 'Alpha',
origin: 'http://192.168.1.2',
status: 'unknown',
authentication: 'unknown',
freshness: 'unknown',
};
afterEach(cleanup);
describe('MessagesModule', () => {
it('shows the real message list including content and never exposes PDU', async () => {
const dataSource: MessagesDataSource = {
load: vi.fn().mockResolvedValue({
messages: [
{
id: '32',
direction: 'incoming',
phoneNumber: '10086',
content: '余额提醒',
timestamp: '2026-07-18 09:09:20',
status: 'received',
pdu: 'secret-pdu',
},
],
}),
send: vi.fn(),
};
render();
expect(await screen.findByText('余额提醒')).toBeTruthy();
expect(screen.getByText(/收到 · 10086/)).toBeTruthy();
expect(document.body.textContent).not.toContain('secret-pdu');
});
it('requires an explicit review step before it submits exactly once', async () => {
const user = userEvent.setup();
const send = vi.fn().mockResolvedValue(undefined);
const dataSource: MessagesDataSource = {
load: vi.fn().mockResolvedValue({ messages: [] }),
send,
};
render();
await user.type(screen.getByRole('textbox', { name: '手机号' }), '10086');
await user.type(screen.getByRole('textbox', { name: '短信内容' }), 'CXLL');
await user.click(screen.getByRole('button', { name: '发送短信' }));
expect(send).not.toHaveBeenCalled();
const confirmation = screen.getByRole('region', { name: '确认发送短信' });
expect(confirmation.textContent).toContain('10086');
expect(confirmation.textContent).toContain('CXLL');
await user.click(within(confirmation).getByRole('button', { name: '确认并发送' }));
expect(send).toHaveBeenCalledTimes(1);
expect(send).toHaveBeenCalledWith('alpha', { phoneNumber: '10086', content: 'CXLL' });
expect((await screen.findByRole('status')).textContent).toContain('短信已提交发送');
});
});