Files
Netcatty/domain/portForwardingPersistence.test.ts
T
8ee55963d8 feat(portforward): make main process authoritative for runtime state (#2288) (#2819)
* feat(portforward): make main process authoritative for runtime state

Phase 1 of #2288: add snapshot/runtime event protocol with epoch+revision,
stop persisting active/connecting/error into localStorage, migrate legacy
stored phases on read, and project unknown when snapshots are unavailable.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(portforward): access runtime bridge via netcattyBridge

Satisfy the no-restricted-properties lint rule for window.netcatty.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

* fix(portforward): honor snapshot authority and prune on resync

Default runtime projection to unknown until a successful snapshot,
reconcile (not sync-only) on epoch/revision gaps, serialize runtime
event handling, clear worker runtime subscribers on window destroy,
and align tray unknown UX with RuleCard.

Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: 陈大猫 <binaricat@users.noreply.github.com>
2026-08-08 02:50:41 +00:00

60 lines
1.9 KiB
TypeScript

import test from 'node:test';
import assert from 'node:assert/strict';
import type { PortForwardingRule } from './models/portForwarding.ts';
import {
migratePortForwardingRulesFromStorage,
toPersistedPortForwardingRule,
toPersistedPortForwardingRules,
} from './portForwardingPersistence.ts';
const baseRule: PortForwardingRule = {
id: 'rule-1',
label: 'Tunnel',
type: 'local',
localPort: 8080,
bindAddress: '127.0.0.1',
remoteHost: '127.0.0.1',
remotePort: 80,
hostId: 'host-1',
autoStart: true,
createdAt: 1,
status: 'active',
error: 'stale',
lastUsedAt: 99,
};
test('toPersistedPortForwardingRule clears runtime phase fields', () => {
const persisted = toPersistedPortForwardingRule(baseRule);
assert.equal(persisted.status, 'inactive');
assert.equal(persisted.error, undefined);
assert.equal(persisted.label, 'Tunnel');
assert.equal(persisted.autoStart, true);
assert.equal(persisted.lastUsedAt, 99);
});
test('toPersistedPortForwardingRules maps every rule', () => {
const persisted = toPersistedPortForwardingRules([
baseRule,
{ ...baseRule, id: 'rule-2', status: 'error', error: 'boom' },
]);
assert.equal(persisted.length, 2);
assert.ok(persisted.every((rule) => rule.status === 'inactive' && rule.error === undefined));
});
test('migratePortForwardingRulesFromStorage drops legacy active/connecting', () => {
const migrated = migratePortForwardingRulesFromStorage([
baseRule,
{ ...baseRule, id: 'rule-2', status: 'connecting' },
{ ...baseRule, id: 'rule-3', status: 'error', error: 'auth failed' },
{ ...baseRule, id: 'rule-4', status: 'inactive', error: undefined },
]);
assert.equal(migrated[0]?.status, 'inactive');
assert.equal(migrated[0]?.error, undefined);
assert.equal(migrated[1]?.status, 'inactive');
assert.equal(migrated[2]?.status, 'error');
assert.equal(migrated[2]?.error, 'auth failed');
assert.equal(migrated[3]?.status, 'inactive');
});