Files
NetMesh/domain/portForwardingAgentOps.test.ts
zhaolei 3c72efcb7f
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
[Init] Initial commit - NetMesh terminal manager
2026-09-13 18:24:01 +08:00

74 lines
3.7 KiB
TypeScript

import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import type { Host, PortForwardingRule } from './models';
import { createPortForwardingRule, duplicatePortForwardingRule, updatePortForwardingRule } from './portForwardingAgentOps';
const host: Host = { id: 'host-1', label: 'Host', hostname: 'host.test', username: 'root', tags: [], os: 'linux' };
const source = {
label: 'Web', type: 'local', localPort: 8080, bindAddress: '127.0.0.1',
remoteHost: '127.0.0.1', remotePort: 80, hostId: 'host-1',
};
describe('portForwardingAgentOps', () => {
it('creates deterministic rules from caller-provided id and time', () => {
const result = createPortForwardingRule([], [host], source, { id: 'rule-1', now: 123 });
assert.equal(result.ok, true);
if (!result.ok) return;
assert.equal(result.value.rule.id, 'rule-1');
assert.equal(result.value.rule.createdAt, 123);
});
it('validates types, ports, hosts, and dynamic forwarding fields', () => {
assert.equal(createPortForwardingRule([], [host], { ...source, localPort: 0 }, { id: 'x', now: 1 }).ok, false);
assert.equal(createPortForwardingRule([], [host], { ...source, hostId: 'missing' }, { id: 'x', now: 1 }).ok, false);
assert.equal(createPortForwardingRule([], [host], { ...source, type: 'invalid' }, { id: 'x', now: 1 }).ok, false);
for (const protocol of ['telnet', 'local', 'serial'] as const) {
const unsupportedHost: Host = { ...host, id: `${protocol}-host`, protocol };
const unsupportedResult = createPortForwardingRule([], [unsupportedHost], {
...source, hostId: unsupportedHost.id,
}, { id: `${protocol}-rule`, now: 1 });
assert.equal(unsupportedResult.ok, false);
if (!unsupportedResult.ok) assert.match(unsupportedResult.error, /does not support port forwarding/i);
}
const dynamic = createPortForwardingRule([], [host], {
type: 'dynamic', localPort: 1080, hostId: 'host-1',
}, { id: 'dynamic-1', now: 1 });
assert.equal(dynamic.ok, true);
});
it('updates without changing identity and duplicates with clean runtime state', () => {
const rule: PortForwardingRule = {
id: 'rule-1', label: 'Web', type: 'local', localPort: 8080, bindAddress: '127.0.0.1',
remoteHost: '127.0.0.1', remotePort: 80, hostId: 'host-1', status: 'active', createdAt: 10,
error: 'previous warning', lastUsedAt: 9,
};
const updated = updatePortForwardingRule([rule], [host], 'rule-1', { localPort: 8081 });
assert.equal(updated.ok, true);
if (updated.ok) {
assert.equal(updated.value.rule.id, 'rule-1');
assert.equal(updated.value.rule.status, 'inactive');
assert.equal(updated.value.rule.error, undefined);
assert.equal(updated.value.rule.lastUsedAt, 9);
}
const relabeled = updatePortForwardingRule([rule], [host], 'rule-1', { label: 'Renamed' });
assert.equal(relabeled.ok, true);
if (relabeled.ok) {
assert.equal(relabeled.value.rule.status, 'active');
assert.equal(relabeled.value.rule.error, 'previous warning');
assert.equal(relabeled.value.rule.lastUsedAt, 9);
}
const duplicated = duplicatePortForwardingRule([rule], [host], 'rule-1', { id: 'rule-2', now: 20 });
assert.equal(duplicated.ok, true);
if (duplicated.ok) {
assert.equal(duplicated.value.rule.status, 'inactive');
assert.equal(duplicated.value.rule.createdAt, 20);
}
const serialHost: Host = { ...host, protocol: 'serial' };
const rejectedDuplicate = duplicatePortForwardingRule(
[rule], [serialHost], 'rule-1', { id: 'rule-3', now: 30 },
);
assert.equal(rejectedDuplicate.ok, false);
if (!rejectedDuplicate.ok) assert.match(rejectedDuplicate.error, /does not support port forwarding/i);
});
});