-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathconfig.test.ts
More file actions
180 lines (156 loc) · 5.18 KB
/
Copy pathconfig.test.ts
File metadata and controls
180 lines (156 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { mkdtemp, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import type { SyncConfig } from './config.js';
import {
canCommitMcpSecrets,
chmodIfExists,
deepMerge,
normalizeSecretsBackend,
normalizeSyncConfig,
parseJsonc,
stripOverrides,
} from './config.js';
describe('deepMerge', () => {
it('merges nested objects and replaces arrays', () => {
const base = { a: 1, nested: { x: 1, y: 2 }, list: [1] };
const override = { b: 2, nested: { y: 3 }, list: [2] };
const merged = deepMerge(base, override);
expect(merged).toEqual({
a: 1,
b: 2,
nested: { x: 1, y: 3 },
list: [2],
});
});
});
describe('stripOverrides', () => {
it('removes override keys and restores base values', () => {
const base = {
theme: 'opencode',
provider: { openai: { apiKey: 'base', models: { tiny: true } } },
};
const overrides = {
provider: { openai: { apiKey: 'local' } },
};
const local = deepMerge(base, overrides) as Record<string, unknown>;
const stripped = stripOverrides(local, overrides, base);
expect(stripped).toEqual(base);
});
it('drops override-only keys not present in base', () => {
const base = { theme: 'opencode' };
const overrides = { theme: 'local', editor: 'vim' };
const local = { theme: 'local', editor: 'vim', other: true };
const stripped = stripOverrides(local, overrides, base);
expect(stripped).toEqual({ theme: 'opencode', other: true });
});
});
describe('normalizeSyncConfig', () => {
it('disables MCP secrets when secrets are disabled', () => {
const normalized = normalizeSyncConfig({
includeSecrets: false,
includeMcpSecrets: true,
});
expect(normalized.includeMcpSecrets).toBe(false);
});
it('allows MCP secrets when secrets are enabled', () => {
const normalized = normalizeSyncConfig({
includeSecrets: true,
includeMcpSecrets: true,
});
expect(normalized.includeMcpSecrets).toBe(true);
});
it('enables model favorites by default', () => {
const normalized = normalizeSyncConfig({});
expect(normalized.includeModelFavorites).toBe(true);
});
it('enables skills and home .agents by default', () => {
const normalized = normalizeSyncConfig({});
expect(normalized.includeOpencodeSkills).toBe(true);
expect(normalized.includeAgentsDir).toBe(true);
});
it('allows disabling skills and home .agents', () => {
const normalized = normalizeSyncConfig({
includeOpencodeSkills: false,
includeAgentsDir: false,
});
expect(normalized.includeOpencodeSkills).toBe(false);
expect(normalized.includeAgentsDir).toBe(false);
});
it('defaults extra path lists when omitted', () => {
const normalized = normalizeSyncConfig({ includeSecrets: true });
expect(normalized.extraSecretPaths).toEqual([]);
expect(normalized.extraConfigPaths).toEqual([]);
});
});
describe('normalizeSecretsBackend', () => {
it('returns undefined when backend is missing', () => {
expect(normalizeSecretsBackend(undefined)).toBeUndefined();
});
it('preserves unknown backend types for validation', () => {
const unknownBackend = { type: 'unknown' } as unknown as SyncConfig['secretsBackend'];
expect(normalizeSecretsBackend(unknownBackend)).toEqual({ type: 'unknown' });
});
it('normalizes 1password documents', () => {
const raw = {
type: '1password',
vault: 'Personal',
documents: {
authJson: 'auth.json',
mcpAuthJson: 'mcp-auth.json',
extra: 'ignored',
},
} as unknown as SyncConfig['secretsBackend'];
expect(normalizeSecretsBackend(raw)).toEqual({
type: '1password',
vault: 'Personal',
documents: {
authJson: 'auth.json',
mcpAuthJson: 'mcp-auth.json',
},
});
});
});
describe('canCommitMcpSecrets', () => {
it('requires includeSecrets and includeMcpSecrets', () => {
expect(canCommitMcpSecrets({ includeSecrets: false, includeMcpSecrets: true })).toBe(false);
expect(canCommitMcpSecrets({ includeSecrets: true, includeMcpSecrets: false })).toBe(false);
expect(canCommitMcpSecrets({ includeSecrets: true, includeMcpSecrets: true })).toBe(true);
});
});
describe('parseJsonc', () => {
it('parses JSONC with comments and trailing commas', () => {
const input = `{
// comment
"repo": {
"owner": "me",
"name": "opencode-config",
},
"includeSecrets": false,
"extraSecretPaths": [
"foo",
],
"extraConfigPaths": [
"bar",
],
}`;
expect(parseJsonc(input)).toEqual({
repo: { owner: 'me', name: 'opencode-config' },
includeSecrets: false,
extraSecretPaths: ['foo'],
extraConfigPaths: ['bar'],
});
});
});
describe('chmodIfExists', () => {
it('ignores missing paths', async () => {
const tempDir = await mkdtemp(path.join(os.tmpdir(), 'opencode-sync-'));
try {
const missingPath = path.join(tempDir, 'missing.txt');
await expect(chmodIfExists(missingPath, 0o600)).resolves.toBeUndefined();
} finally {
await rm(tempDir, { recursive: true, force: true });
}
});
});