Files
openclaw/src/routing/session-key.test.ts
max 223eee0a20 refactor: unify peer kind to ChatType, rename dm to direct (#11881)
* fix: use .js extension for ESM imports of RoutePeerKind

The imports incorrectly used .ts extension which doesn't resolve
with moduleResolution: NodeNext. Changed to .js and added 'type'
import modifier.

* fix tsconfig

* refactor: unify peer kind to ChatType, rename dm to direct

- Replace RoutePeerKind with ChatType throughout codebase
- Change 'dm' literal values to 'direct' in routing/session keys
- Keep backward compat: normalizeChatType accepts 'dm' -> 'direct'
- Add ChatType export to plugin-sdk, deprecate RoutePeerKind
- Update session key parsing to accept both 'dm' and 'direct' markers
- Update all channel monitors and extensions to use ChatType

BREAKING CHANGE: Session keys now use 'direct' instead of 'dm'.
Existing 'dm' keys still work via backward compat layer.

* fix tests

* test: update session key expectations for dmdirect migration

- Fix test expectations to expect :direct: in generated output
- Add explicit backward compat test for normalizeChatType('dm')
- Keep input test data with :dm: keys to verify backward compat

* fix: accept legacy 'dm' in session key parsing for backward compat

getDmHistoryLimitFromSessionKey now accepts both :dm: and :direct:
to ensure old session keys continue to work correctly.

* test: add explicit backward compat tests for dmdirect migration

- session-key.test.ts: verify both :dm: and :direct: keys are valid
- getDmHistoryLimitFromSessionKey: verify both formats work

* feat: backward compat for resetByType.dm config key

* test: skip unix-path Nix tests on Windows
2026-02-09 09:20:52 +09:00

42 lines
1.9 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { classifySessionKeyShape } from "./session-key.js";
describe("classifySessionKeyShape", () => {
it("classifies empty keys as missing", () => {
expect(classifySessionKeyShape(undefined)).toBe("missing");
expect(classifySessionKeyShape(" ")).toBe("missing");
});
it("classifies valid agent keys", () => {
expect(classifySessionKeyShape("agent:main:main")).toBe("agent");
expect(classifySessionKeyShape("agent:research:subagent:worker")).toBe("agent");
});
it("classifies malformed agent keys", () => {
expect(classifySessionKeyShape("agent::broken")).toBe("malformed_agent");
expect(classifySessionKeyShape("agent:main")).toBe("malformed_agent");
});
it("treats non-agent legacy or alias keys as non-malformed", () => {
expect(classifySessionKeyShape("main")).toBe("legacy_or_alias");
expect(classifySessionKeyShape("custom-main")).toBe("legacy_or_alias");
expect(classifySessionKeyShape("subagent:worker")).toBe("legacy_or_alias");
});
});
describe("session key backward compatibility", () => {
it("classifies legacy :dm: session keys as valid agent keys", () => {
// Legacy session keys use :dm: instead of :direct:
// Both should be recognized as valid agent keys
expect(classifySessionKeyShape("agent:main:telegram:dm:123456")).toBe("agent");
expect(classifySessionKeyShape("agent:main:whatsapp:dm:+15551234567")).toBe("agent");
expect(classifySessionKeyShape("agent:main:discord:dm:user123")).toBe("agent");
});
it("classifies new :direct: session keys as valid agent keys", () => {
expect(classifySessionKeyShape("agent:main:telegram:direct:123456")).toBe("agent");
expect(classifySessionKeyShape("agent:main:whatsapp:direct:+15551234567")).toBe("agent");
expect(classifySessionKeyShape("agent:main:discord:direct:user123")).toBe("agent");
});
});