@workclaw/openclaw-workclaw 1.0.17 → 1.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -1
- package/index.ts +210 -210
- package/openclaw.plugin.json +1 -0
- package/package.json +11 -4
- package/setup-entry.ts +6 -0
- package/skills/openclaw-workclaw-cron/SKILL.md +45 -28
- package/src/accounts.ts +62 -37
- package/src/api/accounts-api.ts +88 -89
- package/src/api/prompts-api.ts +70 -77
- package/src/api/session-api.ts +99 -108
- package/src/api/skills-api.ts +35 -37
- package/src/api/workspace.ts +27 -29
- package/src/channel.ts +200 -202
- package/src/config-schema.ts +9 -9
- package/src/connection/workclaw-client.ts +554 -567
- package/src/gateway/agent-handlers.ts +392 -426
- package/src/gateway/config-writer.ts +228 -243
- package/src/gateway/message-context.ts +534 -362
- package/src/gateway/message-dispatcher.ts +529 -489
- package/src/gateway/reconnect.ts +217 -113
- package/src/gateway/skills-handler.ts +408 -472
- package/src/gateway/skills-list-handler.ts +9 -9
- package/src/gateway/tools-list-handler.ts +70 -72
- package/src/gateway/workclaw-gateway.ts +328 -486
- package/src/media/upload.ts +83 -94
- package/src/outbound/index.ts +57 -55
- package/src/outbound/workclaw-sender.ts +134 -133
- package/src/runtime.ts +291 -194
- package/src/send.ts +1 -1
- package/src/tools/openclaw-workclaw-cron/api/index.ts +6 -6
- package/src/tools/openclaw-workclaw-cron/src/add/params.ts +20 -19
- package/src/tools/openclaw-workclaw-cron/src/add/sync.ts +2 -2
- package/src/tools/openclaw-workclaw-cron/src/disable/params.ts +1 -1
- package/src/tools/openclaw-workclaw-cron/src/disable/sync.ts +3 -3
- package/src/tools/openclaw-workclaw-cron/src/enable/params.ts +1 -1
- package/src/tools/openclaw-workclaw-cron/src/enable/sync.ts +3 -3
- package/src/tools/openclaw-workclaw-cron/src/notify/sync.ts +2 -2
- package/src/tools/openclaw-workclaw-cron/src/remove/params.ts +1 -1
- package/src/tools/openclaw-workclaw-cron/src/remove/sync.ts +3 -3
- package/src/tools/openclaw-workclaw-cron/src/update/params.ts +195 -197
- package/src/tools/openclaw-workclaw-cron/src/update/sync.ts +4 -4
- package/src/tools/openclaw-workclaw-system/src/get/index.ts +2 -2
- package/src/tools/openclaw-workclaw-system/src/token/index.ts +4 -4
- package/src/types.ts +38 -40
- package/src/utils/content.ts +16 -21
- package/tests/accounts.test.ts +285 -0
- package/tests/message-context.test.ts +313 -0
- package/tests/reconnect.test.ts +257 -0
- package/tests/workclaw-client.test.ts +112 -0
- package/tsconfig.json +8 -5
- package/vitest.config.ts +8 -0
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
|
|
6
|
-
import {
|
|
6
|
+
import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
|
|
7
7
|
import { callCronJobUpdate } from '../../api/index.js'
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -25,7 +25,7 @@ const UpdateSyncSchema = {
|
|
|
25
25
|
properties: {
|
|
26
26
|
jobId: {
|
|
27
27
|
type: 'string',
|
|
28
|
-
description:
|
|
28
|
+
description: `OpenClaw 任务 ID(必须是数字或字符串,不是 cron 表达式)`,
|
|
29
29
|
},
|
|
30
30
|
name: {
|
|
31
31
|
type: 'string',
|
|
@@ -33,7 +33,7 @@ const UpdateSyncSchema = {
|
|
|
33
33
|
},
|
|
34
34
|
kind: {
|
|
35
35
|
type: 'string',
|
|
36
|
-
description: '
|
|
36
|
+
description: '任务类型(仅支持 cron)',
|
|
37
37
|
enum: ['cron'],
|
|
38
38
|
},
|
|
39
39
|
expr: {
|
|
@@ -97,7 +97,7 @@ function getOpenclawWorkclawCronUpdateSyncTool(api: OpenClawPluginApi) {
|
|
|
97
97
|
return json({ error: '无法获取账户 ID(accountId),请确保已配置账户' })
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
const accountConfig =
|
|
100
|
+
const accountConfig = resolveWorkclawAccountWithCache({
|
|
101
101
|
cfg: ctx.config,
|
|
102
102
|
accountId,
|
|
103
103
|
})
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
|
|
6
|
-
import {
|
|
6
|
+
import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* 构造工具返回结果
|
|
@@ -35,7 +35,7 @@ function getOpenclawWorkclawSystemConfigTool(api: OpenClawPluginApi) {
|
|
|
35
35
|
|
|
36
36
|
api.logger.info(`[智小途-系统信息] accountId=${accountId}`)
|
|
37
37
|
|
|
38
|
-
const accountConfig =
|
|
38
|
+
const accountConfig = resolveWorkclawAccountWithCache({
|
|
39
39
|
cfg: ctx.config,
|
|
40
40
|
accountId,
|
|
41
41
|
})
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
5
|
import type { OpenClawPluginApi } from 'openclaw/plugin-sdk'
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { resolveWorkclawAccountWithCache } from '../../../../accounts.js'
|
|
7
|
+
import { getWorkclawAccessToken } from '../../../../connection/workclaw-client.js'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* 构造工具返回结果
|
|
@@ -36,7 +36,7 @@ function getOpenclawWorkclawSystemTokenTool(api: OpenClawPluginApi) {
|
|
|
36
36
|
|
|
37
37
|
api.logger.info(`[智小途-系统信息] accountId=${accountId}`)
|
|
38
38
|
|
|
39
|
-
const accountConfig =
|
|
39
|
+
const accountConfig = resolveWorkclawAccountWithCache({
|
|
40
40
|
cfg: ctx.config,
|
|
41
41
|
accountId,
|
|
42
42
|
})
|
|
@@ -53,7 +53,7 @@ function getOpenclawWorkclawSystemTokenTool(api: OpenClawPluginApi) {
|
|
|
53
53
|
|
|
54
54
|
api.logger.info(`[智小途-系统信息] 正在获取 AccessToken,accountId=${accountId}`)
|
|
55
55
|
|
|
56
|
-
const accessToken = await
|
|
56
|
+
const accessToken = await getWorkclawAccessToken(tokenCacheKey, {
|
|
57
57
|
baseUrl: accConfig.baseUrl,
|
|
58
58
|
appKey: accConfig.appKey,
|
|
59
59
|
appSecret: accConfig.appSecret,
|
package/src/types.ts
CHANGED
|
@@ -1,60 +1,58 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
workclawConfigSchema,
|
|
3
|
+
workclawAccountConfigSchema,
|
|
4
4
|
z,
|
|
5
|
-
} from
|
|
5
|
+
} from "./config-schema.js";
|
|
6
6
|
|
|
7
|
-
export type
|
|
8
|
-
export type
|
|
7
|
+
export type WorkclawConfig = z.infer<typeof workclawConfigSchema>;
|
|
8
|
+
export type WorkclawAccountConfig = z.infer<typeof workclawAccountConfigSchema>;
|
|
9
9
|
|
|
10
|
-
export type
|
|
11
|
-
export type WsConnectionStrategy =
|
|
10
|
+
export type WorkclawConnectionMode = "websocket" | "webhook";
|
|
11
|
+
export type WsConnectionStrategy = "per-account" | "per-appKey";
|
|
12
12
|
|
|
13
|
-
export
|
|
14
|
-
accountId: string
|
|
15
|
-
enabled: boolean
|
|
16
|
-
configured: boolean
|
|
17
|
-
name?: string
|
|
13
|
+
export type ResolvedWorkclawAccount = {
|
|
14
|
+
accountId: string;
|
|
15
|
+
enabled: boolean;
|
|
16
|
+
configured: boolean;
|
|
17
|
+
name?: string;
|
|
18
18
|
/** Merged config (top-level defaults + account-specific overrides) */
|
|
19
|
-
config:
|
|
20
|
-
}
|
|
21
|
-
export type ResolvedOpenClawWorkclawAccount =
|
|
19
|
+
config: WorkclawFullAccountConfig;
|
|
20
|
+
};
|
|
21
|
+
export type ResolvedOpenClawWorkclawAccount = ResolvedWorkclawAccount;
|
|
22
22
|
|
|
23
|
-
export type
|
|
23
|
+
export type WorkclawIdType = "open_id" | "user_id" | "union_id" | "chat_id";
|
|
24
24
|
|
|
25
|
-
export
|
|
26
|
-
messageId: string
|
|
27
|
-
chatId: string
|
|
28
|
-
}
|
|
25
|
+
export type WorkclawSendResult = {
|
|
26
|
+
messageId: string;
|
|
27
|
+
chatId: string;
|
|
28
|
+
};
|
|
29
29
|
|
|
30
30
|
/** WorkClaw plugin base config (shared across all accounts) */
|
|
31
31
|
export interface WorkClawBaseConfig {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
baseUrl?: string;
|
|
33
|
+
websocketUrl?: string;
|
|
34
|
+
appKey?: string;
|
|
35
|
+
appSecret?: string;
|
|
36
|
+
localIp?: string;
|
|
37
|
+
allowInsecureTls?: boolean;
|
|
38
|
+
requestTimeout?: number;
|
|
39
|
+
userId?: string | number;
|
|
40
|
+
replyEndpoint?: string;
|
|
41
|
+
pushEndpoint?: string;
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/** Per-account WorkClaw fields */
|
|
45
45
|
export interface WorkClawAccountConfig {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
agentId?: string | number;
|
|
47
|
+
userId?: string | number;
|
|
48
|
+
openConversationId?: string;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
export type WorkClawConfig = WorkClawBaseConfig & WorkClawAccountConfig
|
|
51
|
+
export type WorkClawConfig = WorkClawBaseConfig & WorkClawAccountConfig;
|
|
52
52
|
|
|
53
|
-
/**
|
|
54
|
-
*
|
|
55
|
-
|
|
56
|
-
*/
|
|
57
|
-
export type OpenclawWorkclawFullAccountConfig = OpenclawWorkclawConfig & Partial<OpenclawWorkclawAccountConfig>
|
|
53
|
+
/** account.config is typed as OpenclawWorkclawConfig (top-level) but at runtime
|
|
54
|
+
* also contains all OpenclawWorkclawAccountConfig fields (merged). */
|
|
55
|
+
export type WorkclawFullAccountConfig = WorkclawConfig & Partial<WorkclawAccountConfig>;
|
|
58
56
|
|
|
59
57
|
/** Resolved account's config - merge of top-level defaults + account overrides */
|
|
60
|
-
export type
|
|
58
|
+
export type ResolvedWorkclawAccountConfig = WorkclawFullAccountConfig;
|
package/src/utils/content.ts
CHANGED
|
@@ -9,17 +9,14 @@
|
|
|
9
9
|
* Handles content format: [{ type: "text", text: "..." }]
|
|
10
10
|
*/
|
|
11
11
|
export function collectTextContentBlocks(content: unknown): string[] {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
parts.push(rec.text)
|
|
21
|
-
}
|
|
22
|
-
return parts
|
|
12
|
+
if (!Array.isArray(content)) return [];
|
|
13
|
+
const parts: string[] = [];
|
|
14
|
+
for (const block of content) {
|
|
15
|
+
if (!block || typeof block !== "object") continue;
|
|
16
|
+
const rec = block as { type?: string; text?: string };
|
|
17
|
+
if (rec.type === "text" && typeof rec.text === "string") parts.push(rec.text);
|
|
18
|
+
}
|
|
19
|
+
return parts;
|
|
23
20
|
}
|
|
24
21
|
|
|
25
22
|
/**
|
|
@@ -27,14 +24,12 @@ export function collectTextContentBlocks(content: unknown): string[] {
|
|
|
27
24
|
* Handles result.content as array of text blocks or direct text fields.
|
|
28
25
|
*/
|
|
29
26
|
export function extractToolResultText(result: unknown): string | undefined {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return
|
|
39
|
-
return texts.join('\n')
|
|
27
|
+
if (!result || typeof result !== "object") return;
|
|
28
|
+
const record = result as { content?: unknown };
|
|
29
|
+
const texts = collectTextContentBlocks(record.content).map((item) => {
|
|
30
|
+
const trimmed = item.trim();
|
|
31
|
+
return trimmed ? trimmed : void 0;
|
|
32
|
+
}).filter((value) => Boolean(value));
|
|
33
|
+
if (texts.length === 0) return;
|
|
34
|
+
return texts.join("\n");
|
|
40
35
|
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests for account config resolution and lookup cache.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { describe, expect, it, beforeEach } from 'vitest';
|
|
6
|
+
import type { OpenClawConfig } from 'openclaw/plugin-sdk';
|
|
7
|
+
import {
|
|
8
|
+
normalizeAccountId,
|
|
9
|
+
buildAccountMap,
|
|
10
|
+
resolveAccountByUserIdAndAgentId,
|
|
11
|
+
allocateWorkerAccountId,
|
|
12
|
+
resolveWorkclawAccount,
|
|
13
|
+
listWorkclawAccountIds,
|
|
14
|
+
isWorkclawAccountConfigured,
|
|
15
|
+
listEnabledWorkclawAccounts,
|
|
16
|
+
} from '../src/accounts.js';
|
|
17
|
+
|
|
18
|
+
function makeCfg(workclaw: Record<string, unknown>): OpenClawConfig {
|
|
19
|
+
return { channels: { 'openclaw-workclaw': workclaw } } as unknown as OpenClawConfig;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// normalizeAccountId
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
describe('normalizeAccountId', () => {
|
|
27
|
+
it('trims whitespace', () => {
|
|
28
|
+
expect(normalizeAccountId(' default ')).toBe('default');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('leaves normal IDs unchanged', () => {
|
|
32
|
+
expect(normalizeAccountId('workspace-1')).toBe('workspace-1');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('coerces non-string to string', () => {
|
|
36
|
+
expect(normalizeAccountId(123 as any)).toBe('123');
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// isWorkclawAccountConfigured
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
describe('isWorkclawAccountConfigured', () => {
|
|
45
|
+
it('returns true when appKey and appSecret are present', () => {
|
|
46
|
+
expect(isWorkclawAccountConfigured({ appKey: 'key123', appSecret: 'secret123' })).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('returns false when appKey is missing', () => {
|
|
50
|
+
expect(isWorkclawAccountConfigured({ appSecret: 'secret' })).toBe(false);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('returns false when appSecret is missing', () => {
|
|
54
|
+
expect(isWorkclawAccountConfigured({ appKey: 'key' })).toBe(false);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('returns false when both are empty strings', () => {
|
|
58
|
+
expect(isWorkclawAccountConfigured({ appKey: '', appSecret: '' })).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('returns false when values are only whitespace', () => {
|
|
62
|
+
expect(isWorkclawAccountConfigured({ appKey: ' ', appSecret: ' ' })).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// listWorkclawAccountIds
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
|
|
70
|
+
describe('listWorkclawAccountIds', () => {
|
|
71
|
+
it('returns sorted account IDs when accounts are configured', () => {
|
|
72
|
+
const cfg = makeCfg({
|
|
73
|
+
appKey: 'key',
|
|
74
|
+
appSecret: 'secret',
|
|
75
|
+
accounts: {
|
|
76
|
+
'workspace-2': { appKey: 'k2', appSecret: 's2' },
|
|
77
|
+
'default': { appKey: 'kd', appSecret: 'sd' },
|
|
78
|
+
'workspace-1': { appKey: 'k1', appSecret: 's1' },
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
const ids = listWorkclawAccountIds(cfg);
|
|
82
|
+
expect(ids).toEqual(['default', 'workspace-1', 'workspace-2']);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('returns [default] when no accounts configured but plugin has appKey+appSecret', () => {
|
|
86
|
+
const cfg = makeCfg({ appKey: 'key', appSecret: 'secret' });
|
|
87
|
+
const ids = listWorkclawAccountIds(cfg);
|
|
88
|
+
expect(ids).toContain('default');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('returns [default] when channel config is empty', () => {
|
|
92
|
+
const cfg = makeCfg({});
|
|
93
|
+
const ids = listWorkclawAccountIds(cfg);
|
|
94
|
+
expect(ids).toContain('default');
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
// ---------------------------------------------------------------------------
|
|
99
|
+
// resolveWorkclawAccount — config merging
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
|
|
102
|
+
describe('resolveWorkclawAccount', () => {
|
|
103
|
+
it('account-level fields override top-level fields', () => {
|
|
104
|
+
const cfg = makeCfg({
|
|
105
|
+
appKey: 'top-key',
|
|
106
|
+
appSecret: 'top-secret',
|
|
107
|
+
baseUrl: 'http://top.example.com',
|
|
108
|
+
accounts: {
|
|
109
|
+
'bot-a': { appKey: 'a-key', appSecret: 'a-secret', baseUrl: 'http://a.example.com' },
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
const account = resolveWorkclawAccount({ cfg, accountId: 'bot-a' });
|
|
113
|
+
expect(account.config.appKey).toBe('a-key');
|
|
114
|
+
expect(account.config.baseUrl).toBe('http://a.example.com');
|
|
115
|
+
expect(account.configured).toBe(true);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('account inherits top-level fields not overridden', () => {
|
|
119
|
+
const cfg = makeCfg({
|
|
120
|
+
appKey: 'top-key',
|
|
121
|
+
appSecret: 'top-secret',
|
|
122
|
+
baseUrl: 'http://top.example.com',
|
|
123
|
+
accounts: {
|
|
124
|
+
'bot-a': { appKey: 'a-key', appSecret: 'a-secret' },
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
const account = resolveWorkclawAccount({ cfg, accountId: 'bot-a' });
|
|
128
|
+
expect(account.config.baseUrl).toBe('http://top.example.com');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('account is not configured when appKey/appSecret missing after merge', () => {
|
|
132
|
+
const cfg = makeCfg({
|
|
133
|
+
accounts: {
|
|
134
|
+
'bot-empty': {},
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
const account = resolveWorkclawAccount({ cfg, accountId: 'bot-empty' });
|
|
138
|
+
expect(account.configured).toBe(false);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('account is disabled when top-level enabled=false', () => {
|
|
142
|
+
const cfg = makeCfg({
|
|
143
|
+
enabled: false,
|
|
144
|
+
appKey: 'key',
|
|
145
|
+
appSecret: 'secret',
|
|
146
|
+
accounts: {
|
|
147
|
+
'bot-a': { appKey: 'ak', appSecret: 'as' },
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
const account = resolveWorkclawAccount({ cfg, accountId: 'bot-a' });
|
|
151
|
+
expect(account.enabled).toBe(false);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('account is disabled when account-level enabled=false', () => {
|
|
155
|
+
const cfg = makeCfg({
|
|
156
|
+
appKey: 'key',
|
|
157
|
+
appSecret: 'secret',
|
|
158
|
+
accounts: {
|
|
159
|
+
'bot-off': { appKey: 'ak', appSecret: 'as', enabled: false },
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
const account = resolveWorkclawAccount({ cfg, accountId: 'bot-off' });
|
|
163
|
+
expect(account.enabled).toBe(false);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('accountId is normalized (trimmed)', () => {
|
|
167
|
+
const cfg = makeCfg({
|
|
168
|
+
accounts: {
|
|
169
|
+
'bot-a': { appKey: 'ak', appSecret: 'as' },
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
const account = resolveWorkclawAccount({ cfg, accountId: ' bot-a ' });
|
|
173
|
+
expect(account.accountId).toBe('bot-a');
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
// listEnabledWorkclawAccounts
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
describe('listEnabledWorkclawAccounts', () => {
|
|
182
|
+
it('returns only enabled and configured accounts', () => {
|
|
183
|
+
// No top-level appKey/appSecret so 'unconfigured' stays unconfigured (no inheritance)
|
|
184
|
+
const cfg = makeCfg({
|
|
185
|
+
accounts: {
|
|
186
|
+
'active': { appKey: 'ak', appSecret: 'as' },
|
|
187
|
+
'disabled': { appKey: 'dk', appSecret: 'ds', enabled: false },
|
|
188
|
+
'unconfigured': {},
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
const accounts = listEnabledWorkclawAccounts(cfg);
|
|
192
|
+
const ids = accounts.map(a => a.accountId);
|
|
193
|
+
expect(ids).toContain('active');
|
|
194
|
+
expect(ids).not.toContain('disabled');
|
|
195
|
+
expect(ids).not.toContain('unconfigured');
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
// ---------------------------------------------------------------------------
|
|
200
|
+
// buildAccountMap + resolveAccountByUserIdAndAgentId
|
|
201
|
+
// ---------------------------------------------------------------------------
|
|
202
|
+
|
|
203
|
+
describe('buildAccountMap + resolveAccountByUserIdAndAgentId', () => {
|
|
204
|
+
beforeEach(() => {
|
|
205
|
+
// Reset state by calling buildAccountMap with empty config
|
|
206
|
+
buildAccountMap(makeCfg({}));
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it('resolves accountId from userId+agentId after buildAccountMap', () => {
|
|
210
|
+
const cfg = makeCfg({
|
|
211
|
+
accounts: {
|
|
212
|
+
'workspace-1': { appKey: 'k', appSecret: 's', userId: 'user-123', agentId: '456' },
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
buildAccountMap(cfg);
|
|
216
|
+
const result = resolveAccountByUserIdAndAgentId(cfg, 'user-123', '456');
|
|
217
|
+
expect(result).toBe('workspace-1');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('returns null when no matching account', () => {
|
|
221
|
+
const cfg = makeCfg({
|
|
222
|
+
accounts: {
|
|
223
|
+
'workspace-1': { appKey: 'k', appSecret: 's', userId: 'user-123', agentId: '456' },
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
buildAccountMap(cfg);
|
|
227
|
+
const result = resolveAccountByUserIdAndAgentId(cfg, 'user-999', '456');
|
|
228
|
+
expect(result).toBeNull();
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('caches lookup on second call', () => {
|
|
232
|
+
const cfg = makeCfg({
|
|
233
|
+
accounts: {
|
|
234
|
+
'workspace-1': { appKey: 'k', appSecret: 's', userId: 'user-abc', agentId: '789' },
|
|
235
|
+
},
|
|
236
|
+
});
|
|
237
|
+
buildAccountMap(cfg);
|
|
238
|
+
// First call
|
|
239
|
+
expect(resolveAccountByUserIdAndAgentId(cfg, 'user-abc', '789')).toBe('workspace-1');
|
|
240
|
+
// Second call (from cache)
|
|
241
|
+
expect(resolveAccountByUserIdAndAgentId(cfg, 'user-abc', '789')).toBe('workspace-1');
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// ---------------------------------------------------------------------------
|
|
246
|
+
// allocateWorkerAccountId
|
|
247
|
+
// ---------------------------------------------------------------------------
|
|
248
|
+
|
|
249
|
+
describe('allocateWorkerAccountId', () => {
|
|
250
|
+
beforeEach(() => {
|
|
251
|
+
buildAccountMap(makeCfg({}));
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('allocates workspace-1 on fresh state', () => {
|
|
255
|
+
const cfg = makeCfg({});
|
|
256
|
+
const id = allocateWorkerAccountId(cfg, 'user-1', 'agent-1');
|
|
257
|
+
expect(id).toBe('workspace-1');
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('increments on each allocation', () => {
|
|
261
|
+
const cfg = makeCfg({});
|
|
262
|
+
const id1 = allocateWorkerAccountId(cfg, 'user-1', 'agent-1');
|
|
263
|
+
const id2 = allocateWorkerAccountId(cfg, 'user-2', 'agent-2');
|
|
264
|
+
expect(id1).toBe('workspace-1');
|
|
265
|
+
expect(id2).toBe('workspace-2');
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('allocated account is immediately resolvable', () => {
|
|
269
|
+
const cfg = makeCfg({});
|
|
270
|
+
allocateWorkerAccountId(cfg, 'user-x', 'agent-x');
|
|
271
|
+
const result = resolveAccountByUserIdAndAgentId(cfg, 'user-x', 'agent-x');
|
|
272
|
+
expect(result).toBe('workspace-1');
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it('nextWorkspaceNum continues from existing accounts', () => {
|
|
276
|
+
const cfg = makeCfg({
|
|
277
|
+
accounts: {
|
|
278
|
+
'workspace-5': { appKey: 'k', appSecret: 's', userId: 'u', agentId: '1' },
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
buildAccountMap(cfg);
|
|
282
|
+
const id = allocateWorkerAccountId(cfg, 'user-new', 'agent-new');
|
|
283
|
+
expect(id).toBe('workspace-6');
|
|
284
|
+
});
|
|
285
|
+
});
|