Files
msd-core/tests/surface-state.test.cjs
Cristian Uibar c5657fcbfd fix(surface): default missing optional fields in readSurface, normalize writeSurface input (#3666)
* surface: default missing optional fields in readSurface, normalize writeSurface input

readSurface used to reject any .gsd-surface.json missing one of its four fields
and return null with no diagnostic, so the active surface silently degraded to
the 'full' profile. Optional array fields (disabledClusters, explicitAdds,
explicitRemoves) now default to [] when missing or wrong-typed; hard failures
(malformed JSON, non-object root, missing/non-string baseProfile) still return
null but emit a console.warn naming the file + reason.

writeSurface now normalizes its input to the full SurfaceState shape and
throws on missing baseProfile, so partial writes can no longer land on disk
and trip readSurface later. Tests extended to cover the new lenient and
warn-on-hard-fail behavior plus the writer guard.

Fixes #3662

* surface: reject whitespace-only baseProfile + migrate new tests to helpers.cjs

Applies CodeRabbit findings on PR #3666:

1. readSurface and writeSurface now reject baseProfile values that are blank
   after trim() (e.g. "   "), not only the empty string. Whitespace-only
   strings would split-by-comma to [''] downstream and silently produce an
   unresolvable profile mode. Both guards updated symmetrically; warn/error
   messages reworded to "missing, non-string, or blank".

2. tests/surface-state.test.cjs now uses createTempDir + cleanup from
   tests/helpers.cjs instead of local mkdtempSync + fs.rmSync, aligning with
   the repo coding guideline for root-level tests. The local tmpDir() helper
   delegates to createTempDir for backward-compat with the existing test
   bodies. Per-test cleanup calls swapped to cleanup(dir).

3. Added regression tests:
   - readSurface rejects whitespace-only baseProfile and warns
   - writeSurface rejects whitespace-only baseProfile, non-string baseProfile,
     and null surfaceState

Refs #3662

* surface: warn on unknown baseProfile mode names in read and write

Applies a Codex review finding on PR #3666:

readSurface and writeSurface used to accept any non-blank string as
baseProfile. A typo like {"baseProfile":"standrad"} would pass validation,
then resolveProfile() in install-profiles.cjs would silently fall back to
'full' with no diagnostic — the same silent-degradation symptom that #3662
was filed to fix, just through a different code path.

Both functions now split baseProfile by comma, validate each mode against
the registered PROFILES set ('core', 'standard', 'full'), and emit a single
[gsd] console.warn line that names the unknown modes and lists the valid
ones. The state is still parsed/written — resolveProfile() decides the
actual resolution fallback. Composed profiles where some modes are valid
and some are not warn only about the unknown subset.

Side note: the pre-existing 'round-trips composed base profile' test used
'core,audit' as a stand-in composed string. 'audit' is not a registered
profile (the three known profiles are 'core', 'standard', 'full'), so the
test was relying on the old lack of validation. Switched to 'core,standard'
to preserve the round-trip intent without producing diagnostic noise.

Refs #3662

* changeset: include blank/typo baseProfile in documented read failure cases

CodeRabbit minor finding on PR #3666 — the changeset wording only mentioned
"missing/non-string baseProfile" but the implementation also rejects blank
(including whitespace-only) baseProfile values, and warns on typo'd /
unknown profile mode names. Updated to match actual behavior.

* changeset: pr field should be PR number, not issue number

Codex review finding on PR #3666. The changeset's `pr: 3662` was the linked
issue (#3662), but the convention across other .changeset/*.md files is that
`pr:` carries the PR number. Verified by spot-checking other changesets
(2937 → pr: 3515, 3298 → pr: 3306, 3541 → pr: 3547 — all PR numbers).
Updated to `pr: 3666`. The body text still references the issue.

* surface: reject comma-only baseProfile + warn on wrong-typed optional fields

Three Gemini review findings on PR #3666 — all spirit-of-#3662 edge cases:

1. Comma-only baseProfile bypasses validation. readSurface used to accept
   baseProfile: ", ," because trim() returned "," (non-empty). Downstream
   resolveProfile() would split-and-filter to [] and silently fall back to
   'full'. Added effectiveProfileModes() helper that splits, trims, filters
   empty — both readSurface and writeSurface now reject when the result is
   empty. Same silent-degradation symptom as the original bug.

2. Wrong-typed optional fields silently coerced. readSurface used to coerce
   {disabledClusters: 42} to {disabledClusters: []} with no diagnostic.
   Now warns via mistypedOptionalFields() before normalizeSurfaceState()
   does the coercion, in both reader and writer.

3. Missing test coverage for the EACCES branch in readSurface. Added a
   chmod-000 unreadable-file test, skipped on Windows and root accounts
   (mode bits are ignored on those platforms).

48/48 surface tests pass. Final codex pass returned LGTM on the prior
state; these three additions strengthen the same lenient/loud contract.

Refs #3662
2026-05-18 00:14:07 -04:00

448 lines
14 KiB
JavaScript

'use strict';
/**
* Tests for readSurface / writeSurface — state IO round-trips.
*/
const { test, describe } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('fs');
const path = require('path');
const os = require('os');
const { readSurface, writeSurface } = require('../get-shit-done/bin/lib/surface.cjs');
const { createTempDir, cleanup } = require('./helpers.cjs');
function tmpDir() {
return createTempDir('gsd-surface-state-');
}
function captureWarn(fn) {
const original = console.warn;
const warnings = [];
console.warn = (...args) => warnings.push(args.join(' '));
try {
return { result: fn(), warnings };
} finally {
console.warn = original;
}
}
describe('readSurface / writeSurface', () => {
test('round-trips a complete surface state', () => {
const dir = tmpDir();
try {
const state = {
baseProfile: 'standard',
disabledClusters: ['utility'],
explicitAdds: ['sketch'],
explicitRemoves: [],
};
writeSurface(dir, state);
const read = readSurface(dir);
assert.deepStrictEqual(read, state);
} finally {
cleanup(dir);
}
});
test('round-trips empty arrays', () => {
const dir = tmpDir();
try {
const state = {
baseProfile: 'core',
disabledClusters: [],
explicitAdds: [],
explicitRemoves: [],
};
writeSurface(dir, state);
assert.deepStrictEqual(readSurface(dir), state);
} finally {
cleanup(dir);
}
});
test('round-trips composed base profile', () => {
const dir = tmpDir();
try {
const state = {
baseProfile: 'core,standard',
disabledClusters: [],
explicitAdds: [],
explicitRemoves: ['health'],
};
writeSurface(dir, state);
assert.deepStrictEqual(readSurface(dir), state);
} finally {
cleanup(dir);
}
});
test('missing file returns null', () => {
const dir = tmpDir();
try {
const result = readSurface(dir);
assert.strictEqual(result, null);
} finally {
cleanup(dir);
}
});
test('non-existent directory returns null', () => {
const ghost = path.join(os.tmpdir(), 'gsd-surface-no-exist-' + Date.now());
const result = readSurface(ghost);
assert.strictEqual(result, null);
});
// chmod-000 unreadable file — Linux-only because Windows and root accounts
// ignore mode bits. Covers the EACCES branch in readSurface (#3662 Gemini).
test('unreadable file (EACCES) returns null and warns', { skip: process.platform === 'win32' || process.getuid?.() === 0 }, () => {
const dir = tmpDir();
try {
const filePath = path.join(dir, '.gsd-surface.json');
fs.writeFileSync(filePath, '{"baseProfile":"standard"}', 'utf8');
fs.chmodSync(filePath, 0o000);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.strictEqual(result, null);
assert.strictEqual(warnings.length, 1);
assert.match(warnings[0], /unreadable/);
fs.chmodSync(filePath, 0o644); // restore so cleanup can rm
} finally {
cleanup(dir);
}
});
test('corrupt JSON returns null and warns', () => {
const dir = tmpDir();
try {
fs.writeFileSync(path.join(dir, '.gsd-surface.json'), '{not valid json', 'utf8');
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.strictEqual(result, null);
assert.strictEqual(warnings.length, 1);
assert.match(warnings[0], /malformed JSON/);
} finally {
cleanup(dir);
}
});
test('JSON missing baseProfile field returns null and warns (#3662)', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ disabledClusters: [], explicitAdds: [], explicitRemoves: [] }),
'utf8'
);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.strictEqual(result, null);
assert.strictEqual(warnings.length, 1);
assert.match(warnings[0], /baseProfile/);
} finally {
cleanup(dir);
}
});
test('JSON with non-string baseProfile returns null and warns', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ baseProfile: 42, disabledClusters: [], explicitAdds: [], explicitRemoves: [] }),
'utf8'
);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.strictEqual(result, null);
assert.match(warnings[0], /baseProfile/);
} finally {
cleanup(dir);
}
});
test('typo in baseProfile warns about unknown mode but still returns state (#3662 Codex)', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ baseProfile: 'standrad', disabledClusters: [], explicitAdds: [], explicitRemoves: [] }),
'utf8'
);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.deepStrictEqual(result, {
baseProfile: 'standrad',
disabledClusters: [],
explicitAdds: [],
explicitRemoves: [],
});
assert.strictEqual(warnings.length, 1);
assert.match(warnings[0], /unknown profile mode/);
assert.match(warnings[0], /standrad/);
} finally {
cleanup(dir);
}
});
test('composed baseProfile warns only about unknown member (#3662 Codex)', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ baseProfile: 'core,bogus', disabledClusters: [], explicitAdds: [], explicitRemoves: [] }),
'utf8'
);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.strictEqual(result.baseProfile, 'core,bogus');
assert.strictEqual(warnings.length, 1);
// Warning must call out 'bogus' as unknown, but not list 'core' as unknown
// (it does appear once in the "(valid: core, standard, full)" hint — that's fine).
const unknownPart = warnings[0].split('(valid:')[0];
assert.match(unknownPart, /bogus/);
assert.doesNotMatch(unknownPart, /\bcore\b/);
} finally {
cleanup(dir);
}
});
test('known composed baseProfile does not warn (#3662 Codex)', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ baseProfile: 'core,standard', disabledClusters: [], explicitAdds: [], explicitRemoves: [] }),
'utf8'
);
const { warnings } = captureWarn(() => readSurface(dir));
assert.deepStrictEqual(warnings, [], 'all-known modes should not warn');
} finally {
cleanup(dir);
}
});
test('writeSurface warns on unknown profile mode but still writes (#3662 Codex)', () => {
const dir = tmpDir();
try {
const { warnings } = captureWarn(() => writeSurface(dir, { baseProfile: 'standrad' }));
const onDisk = JSON.parse(fs.readFileSync(path.join(dir, '.gsd-surface.json'), 'utf8'));
assert.strictEqual(onDisk.baseProfile, 'standrad');
assert.strictEqual(warnings.length, 1);
assert.match(warnings[0], /unknown profile mode/);
assert.match(warnings[0], /standrad/);
} finally {
cleanup(dir);
}
});
test('JSON with whitespace-only baseProfile returns null and warns (#3662 CodeRabbit)', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ baseProfile: ' ', disabledClusters: [], explicitAdds: [], explicitRemoves: [] }),
'utf8'
);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.strictEqual(result, null);
assert.match(warnings[0], /baseProfile/);
} finally {
cleanup(dir);
}
});
test('JSON with non-object root returns null and warns', () => {
const dir = tmpDir();
try {
fs.writeFileSync(path.join(dir, '.gsd-surface.json'), JSON.stringify(['not', 'an', 'object']), 'utf8');
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.strictEqual(result, null);
assert.match(warnings[0], /object root/);
} finally {
cleanup(dir);
}
});
test('missing optional array field defaults to [] (#3662)', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ baseProfile: 'standard', disabledClusters: [], explicitAdds: [] }),
'utf8'
);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.deepStrictEqual(result, {
baseProfile: 'standard',
disabledClusters: [],
explicitAdds: [],
explicitRemoves: [],
});
assert.deepStrictEqual(warnings, [], 'defaulting an optional field should not warn');
} finally {
cleanup(dir);
}
});
test('all optional arrays missing default to [] (#3662)', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ baseProfile: 'standard' }),
'utf8'
);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.deepStrictEqual(result, {
baseProfile: 'standard',
disabledClusters: [],
explicitAdds: [],
explicitRemoves: [],
});
assert.deepStrictEqual(warnings, []);
} finally {
cleanup(dir);
}
});
test('non-array optional field is coerced to [] and warns (#3662 Gemini)', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ baseProfile: 'standard', disabledClusters: 'utility', explicitAdds: 42, explicitRemoves: [] }),
'utf8'
);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.deepStrictEqual(result, {
baseProfile: 'standard',
disabledClusters: [],
explicitAdds: [],
explicitRemoves: [],
});
assert.strictEqual(warnings.length, 1);
assert.match(warnings[0], /wrong type/);
assert.match(warnings[0], /disabledClusters/);
assert.match(warnings[0], /explicitAdds/);
} finally {
cleanup(dir);
}
});
test('comma-only baseProfile is rejected (#3662 Gemini)', () => {
const dir = tmpDir();
try {
fs.writeFileSync(
path.join(dir, '.gsd-surface.json'),
JSON.stringify({ baseProfile: ', ,', disabledClusters: [], explicitAdds: [], explicitRemoves: [] }),
'utf8'
);
const { result, warnings } = captureWarn(() => readSurface(dir));
assert.strictEqual(result, null);
assert.match(warnings[0], /comma-only/);
} finally {
cleanup(dir);
}
});
test('writeSurface rejects comma-only baseProfile (#3662 Gemini)', () => {
const dir = tmpDir();
try {
assert.throws(() => writeSurface(dir, { baseProfile: ', ,' }), /baseProfile/);
assert.throws(() => writeSurface(dir, { baseProfile: ',' }), /baseProfile/);
} finally {
cleanup(dir);
}
});
test('writeSurface warns on wrong-typed optional field but still writes (#3662 Gemini)', () => {
const dir = tmpDir();
try {
const { warnings } = captureWarn(() => writeSurface(dir, {
baseProfile: 'standard',
disabledClusters: 'utility',
}));
const onDisk = JSON.parse(fs.readFileSync(path.join(dir, '.gsd-surface.json'), 'utf8'));
assert.deepStrictEqual(onDisk, {
baseProfile: 'standard',
disabledClusters: [],
explicitAdds: [],
explicitRemoves: [],
});
assert.strictEqual(warnings.length, 1);
assert.match(warnings[0], /wrong type/);
assert.match(warnings[0], /disabledClusters/);
} finally {
cleanup(dir);
}
});
test('writeSurface normalizes partial input — all four fields land on disk (#3662)', () => {
const dir = tmpDir();
try {
writeSurface(dir, { baseProfile: 'standard' });
const onDisk = JSON.parse(fs.readFileSync(path.join(dir, '.gsd-surface.json'), 'utf8'));
assert.deepStrictEqual(onDisk, {
baseProfile: 'standard',
disabledClusters: [],
explicitAdds: [],
explicitRemoves: [],
});
} finally {
cleanup(dir);
}
});
test('writeSurface rejects missing, empty, or blank baseProfile (#3662 writer guard)', () => {
const dir = tmpDir();
try {
assert.throws(
() => writeSurface(dir, { disabledClusters: [], explicitAdds: [], explicitRemoves: [] }),
/baseProfile/
);
assert.throws(() => writeSurface(dir, { baseProfile: '' }), /baseProfile/);
assert.throws(() => writeSurface(dir, { baseProfile: ' ' }), /baseProfile/);
assert.throws(() => writeSurface(dir, { baseProfile: 42 }), /baseProfile/);
assert.throws(() => writeSurface(dir, null), /baseProfile/);
} finally {
cleanup(dir);
}
});
test('atomic write: result file is never a partial tmp file', () => {
const dir = tmpDir();
try {
const state = { baseProfile: 'full', disabledClusters: [], explicitAdds: [], explicitRemoves: [] };
writeSurface(dir, state);
// No .tmp.* files should remain
const files = fs.readdirSync(dir);
const tmpFiles = files.filter(f => f.includes('.tmp.'));
assert.deepStrictEqual(tmpFiles, [], 'no tmp files should remain after write');
// The canonical file exists
assert.ok(files.includes('.gsd-surface.json'));
} finally {
cleanup(dir);
}
});
test('second write overwrites first', () => {
const dir = tmpDir();
try {
writeSurface(dir, { baseProfile: 'core', disabledClusters: [], explicitAdds: [], explicitRemoves: [] });
writeSurface(dir, { baseProfile: 'standard', disabledClusters: ['utility'], explicitAdds: [], explicitRemoves: [] });
const read = readSurface(dir);
assert.strictEqual(read.baseProfile, 'standard');
assert.deepStrictEqual(read.disabledClusters, ['utility']);
} finally {
cleanup(dir);
}
});
test('writeSurface creates directory if it does not exist', () => {
const base = tmpDir();
const nested = path.join(base, 'skills', 'subdir');
try {
writeSurface(nested, { baseProfile: 'full', disabledClusters: [], explicitAdds: [], explicitRemoves: [] });
assert.ok(fs.existsSync(nested));
assert.ok(readSurface(nested) !== null);
} finally {
cleanup(base);
}
});
});