mirror of
https://github.com/Matysh/houseplan-card
synced 2026-10-07 15:09:30 +00:00
committed by
claude[bot]
parent
8273af59ca
commit
372d838ec2
@@ -2,6 +2,7 @@ import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
applyAreaRelocationResolution,
|
||||
MARKER_AREA_SNAPSHOT_LIMIT,
|
||||
markerAreaSnapshotOf,
|
||||
removeMarkerAreaSnapshots,
|
||||
registryFollowingBinding,
|
||||
@@ -166,6 +167,37 @@ test('unresolved duplicate Area and non-authoritative registry are no-ops', () =
|
||||
assert.deepEqual(resolve({ authoritative: false }), { decisions: [], relocateIds: new Set() });
|
||||
});
|
||||
|
||||
test('authoritative registry removes orphan snapshots but preserves live marker ids', () => {
|
||||
const snapshot = {
|
||||
orphan: { binding: 'device:orphan', area: 'area-a' },
|
||||
'marker-id': { binding: 'device:marker-id', area: 'area-a' },
|
||||
'canonical-device-id': { binding: 'device:device-row', area: 'area-a' },
|
||||
};
|
||||
const result = resolve({
|
||||
devices: [device({
|
||||
id: 'device-row',
|
||||
bindingRef: 'device-row',
|
||||
marker: { id: 'marker-id', binding: 'device:marker-id', removed: true },
|
||||
})],
|
||||
snapshot,
|
||||
});
|
||||
const orphan = result.decisions.find((decision) => decision.id === 'orphan');
|
||||
assert.equal(orphan?.reason, 'registry-unverified');
|
||||
assert.equal(orphan?.removeSnapshot, true);
|
||||
assert.equal(result.decisions.some((decision) => decision.id === 'marker-id'), false);
|
||||
assert.deepEqual(applyAreaRelocationResolution(snapshot, result), {
|
||||
'marker-id': snapshot['marker-id'],
|
||||
'canonical-device-id': snapshot['canonical-device-id'],
|
||||
});
|
||||
});
|
||||
|
||||
test('non-authoritative registry preserves orphan snapshots', () => {
|
||||
const snapshot = { orphan: { binding: 'device:orphan', area: 'area-a' } };
|
||||
const result = resolve({ devices: [], snapshot, authoritative: false });
|
||||
assert.deepEqual(result, { decisions: [], relocateIds: new Set() });
|
||||
assert.deepEqual(applyAreaRelocationResolution(snapshot, result), snapshot);
|
||||
});
|
||||
|
||||
test('failed delete does not advance relocation provenance', () => {
|
||||
const previous = { 'device-1': { binding: 'device:device-1', area: 'area-a' } };
|
||||
const result = resolve({ snapshot: previous });
|
||||
@@ -180,6 +212,21 @@ test('defensive snapshot reader drops malformed entries without poisoning valid
|
||||
}), { good: { binding: 'entity:sensor.good', area: 'kitchen' } });
|
||||
});
|
||||
|
||||
test('defensive snapshot reader keeps the newest entries when over its limit', () => {
|
||||
const source = Object.fromEntries(Array.from(
|
||||
{ length: MARKER_AREA_SNAPSHOT_LIMIT + 2 },
|
||||
(_, index) => [`entry-${index}`, { binding: `device:${index}`, area: 'area-a' }],
|
||||
));
|
||||
const snapshot = markerAreaSnapshotOf(source);
|
||||
assert.equal(Object.keys(snapshot).length, MARKER_AREA_SNAPSHOT_LIMIT);
|
||||
assert.equal(Object.hasOwn(snapshot, 'entry-0'), false);
|
||||
assert.equal(Object.hasOwn(snapshot, 'entry-1'), false);
|
||||
assert.deepEqual(snapshot['entry-2'], { binding: 'device:2', area: 'area-a' });
|
||||
assert.deepEqual(snapshot[`entry-${MARKER_AREA_SNAPSHOT_LIMIT + 1}`], {
|
||||
binding: `device:${MARKER_AREA_SNAPSHOT_LIMIT + 1}`, area: 'area-a',
|
||||
});
|
||||
});
|
||||
|
||||
test('marker deletion and rebind cleanup remove only their own lifecycle entries', () => {
|
||||
const source = {
|
||||
keep: { binding: 'device:keep', area: 'living' },
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync, readdirSync } from 'node:fs';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import test from 'node:test';
|
||||
import ts from 'typescript';
|
||||
|
||||
const repoRoot = dirname(dirname(fileURLToPath(import.meta.url)));
|
||||
const dictionary = JSON.parse(readFileSync(join(repoRoot, 'src/i18n/en.json'), 'utf8'));
|
||||
const dictionaryKeys = Object.keys(dictionary);
|
||||
|
||||
const sourceFiles = [];
|
||||
const visitDirectory = (directory) => {
|
||||
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
||||
const path = join(directory, entry.name);
|
||||
if (entry.isDirectory()) visitDirectory(path);
|
||||
else if (entry.isFile() && entry.name.endsWith('.ts')) sourceFiles.push(path);
|
||||
}
|
||||
};
|
||||
visitDirectory(join(repoRoot, 'src'));
|
||||
|
||||
const escapeRegExp = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
|
||||
/** Convert a string-producing expression into a dictionary-key matcher. */
|
||||
const expressionPattern = (node) => {
|
||||
if (ts.isStringLiteralLike(node)) return { source: escapeRegExp(node.text), dynamic: false };
|
||||
if (ts.isParenthesizedExpression(node)) return expressionPattern(node.expression);
|
||||
if (ts.isTemplateExpression(node)) {
|
||||
let source = escapeRegExp(node.head.text);
|
||||
for (const span of node.templateSpans) {
|
||||
source += '.+' + escapeRegExp(span.literal.text);
|
||||
}
|
||||
return { source, dynamic: true };
|
||||
}
|
||||
if (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.PlusToken) {
|
||||
const left = expressionPattern(node.left);
|
||||
const right = expressionPattern(node.right);
|
||||
if (!left && !right) return null;
|
||||
return {
|
||||
source: (left?.source || '.+') + (right?.source || '.+'),
|
||||
dynamic: (left?.dynamic ?? true) || (right?.dynamic ?? true),
|
||||
};
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const literalConsumers = new Set();
|
||||
const dynamicConsumers = [];
|
||||
const derivedHelpAria = new Set();
|
||||
|
||||
for (const path of sourceFiles) {
|
||||
const source = ts.createSourceFile(
|
||||
path,
|
||||
readFileSync(path, 'utf8'),
|
||||
ts.ScriptTarget.Latest,
|
||||
true,
|
||||
ts.ScriptKind.TS,
|
||||
);
|
||||
const visit = (node) => {
|
||||
if (ts.isStringLiteralLike(node)) literalConsumers.add(node.text);
|
||||
|
||||
if (ts.isTemplateExpression(node)
|
||||
|| (ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.PlusToken)) {
|
||||
const pattern = expressionPattern(node);
|
||||
if (pattern?.dynamic && /[A-Za-z]/.test(pattern.source)) {
|
||||
dynamicConsumers.push(new RegExp(`^${pattern.source}$`));
|
||||
}
|
||||
}
|
||||
|
||||
if (ts.isCallExpression(node) && node.arguments.length) {
|
||||
const callee = ts.isPropertyAccessExpression(node.expression)
|
||||
? node.expression.name.text
|
||||
: ts.isIdentifier(node.expression) ? node.expression.text : '';
|
||||
const key = node.arguments[0];
|
||||
if (callee === '_help' && ts.isStringLiteralLike(key) && key.text.endsWith('.help')) {
|
||||
derivedHelpAria.add(`${key.text}.aria`);
|
||||
}
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
};
|
||||
visit(source);
|
||||
}
|
||||
|
||||
test('every i18n key has a literal, dynamic-family or derived help consumer', () => {
|
||||
const unused = dictionaryKeys.filter((key) => !literalConsumers.has(key)
|
||||
&& !derivedHelpAria.has(key)
|
||||
&& !dynamicConsumers.some((pattern) => pattern.test(key)));
|
||||
assert.deepEqual(unused, [], unused.length
|
||||
? `Unused i18n keys: ${unused.join(', ')}. Use each key from src/ or delete it from every locale.`
|
||||
: undefined);
|
||||
});
|
||||
|
||||
test('help accessibility copy is derived from every literal help consumer', () => {
|
||||
assert.equal(derivedHelpAria.size, 19, 'the current settings surface has 19 help descriptions');
|
||||
for (const key of derivedHelpAria) {
|
||||
assert.equal(typeof dictionary[key], 'string', `${key} must accompany its .help consumer`);
|
||||
}
|
||||
});
|
||||
@@ -29,7 +29,6 @@ test('only old tool copy is removed while persisted partition copy remains', ()
|
||||
for (const key of [
|
||||
'markup.partition',
|
||||
'physical.partition_properties',
|
||||
'history.partition_add',
|
||||
]) assert.equal(typeof locale[key], 'string', key);
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user