Implement Harbor gateway device ecosystem support
Build and Deploy Gateway / build-and-push (push) Successful in 26s
Build and Deploy Gateway / deploy (push) Successful in 14s

This commit is contained in:
2026-08-31 02:06:22 +03:00
parent 7e15cc199f
commit 116686a138
21 changed files with 1924 additions and 51 deletions
+61 -2
View File
@@ -3,8 +3,10 @@ import { readFileSync } from 'node:fs';
import test from 'node:test';
import { createDeviceInventoryRoute } from '../../dist/server/http/routes/deviceInventoryRoute.js';
import { errorDefinition } from '../../dist/shared/errors.js';
const deviceId = 'dev_0123456789abcdef';
const tagId = 'tag_0123456789abcdef';
function response() {
return {
@@ -34,6 +36,18 @@ function createHarness({ inventory = {}, body = {} } = {}) {
calls.push(['update', ...args]);
return inventory.update ?? { revision: 3 };
},
createTag: (...args) => {
calls.push(['createTag', ...args]);
return inventory.createTag ?? { revision: 6 };
},
renameTag: (...args) => {
calls.push(['renameTag', ...args]);
return inventory.renameTag ?? { revision: 7 };
},
deleteTag: (...args) => {
calls.push(['deleteTag', ...args]);
return inventory.deleteTag ?? { revision: 8 };
},
resetTraffic: async (...args) => {
calls.push(['resetTraffic', ...args]);
return inventory.resetTraffic ?? { revision: 4 };
@@ -79,7 +93,7 @@ test('device route forwards list and refresh query paths as raw JSON responses',
});
test('device route forwards metadata patch and policy arguments without coercion', async () => {
const patch = { expectedRevision: 7, alias: 'Desk', pinned: false, deprioritized: true, extra: 0 };
const patch = { expectedRevision: 7, alias: 'Desk', pinned: false, deprioritized: true, tagIds: [tagId], extra: 0 };
const metadata = createHarness({ body: patch });
const metadataResponse = response();
assert.equal(await metadata.route.handle({
@@ -89,7 +103,7 @@ test('device route forwards metadata patch and policy arguments without coercion
assert.deepEqual(metadata.calls, [[
'update',
deviceId,
{ alias: 'Desk', pinned: false, deprioritized: true, extra: 0 },
{ alias: 'Desk', pinned: false, deprioritized: true, tagIds: [tagId], extra: 0 },
7,
]]);
assert.deepEqual(metadataResponse.payload, { revision: 3 });
@@ -113,6 +127,39 @@ test('device route forwards metadata patch and policy arguments without coercion
assert.deepEqual(resetResponse.payload, { revision: 4 });
});
test('device tag routes forward catalog mutations and return complete snapshots', async () => {
const create = createHarness({ body: { name: 'Умный дом', expectedRevision: 10 } });
const createResponse = response();
assert.equal(await create.route.handle({ method: 'POST', url: '/api/device-tags' }, createResponse), true);
assert.deepEqual(create.calls, [['createTag', 'Умный дом', 10]]);
assert.deepEqual(createResponse.payload, { revision: 6 });
const rename = createHarness({ body: { name: 'Дом', expectedRevision: 11 } });
const renameResponse = response();
assert.equal(await rename.route.handle({ method: 'PUT', url: `/api/device-tags/${tagId}` }, renameResponse), true);
assert.deepEqual(rename.calls, [['renameTag', tagId, 'Дом', 11]]);
assert.deepEqual(renameResponse.payload, { revision: 7 });
const remove = createHarness({ body: { expectedRevision: 12 } });
const deleteResponse = response();
assert.equal(await remove.route.handle({ method: 'DELETE', url: `/api/device-tags/${tagId}` }, deleteResponse), true);
assert.deepEqual(remove.calls, [['deleteTag', tagId, 12]]);
assert.deepEqual(deleteResponse.payload, { revision: 8 });
});
test('device tag errors expose stable HTTP contracts', () => {
assert.deepEqual(errorDefinition('DEVICE_TAG_NOT_FOUND'), {
status: 404,
message: 'Тег больше недоступен.',
retryable: false,
});
assert.deepEqual(errorDefinition('DEVICE_TAG_NAME_CONFLICT'), {
status: 409,
message: 'Тег с таким именем уже существует.',
retryable: false,
});
});
test('device route preserves endpoint gating and strict lowercase IDs', async () => {
for (const [method, url] of [
['POST', '/api/devices'],
@@ -120,6 +167,8 @@ test('device route preserves endpoint gating and strict lowercase IDs', async ()
['GET', '/api/devices/traffic'],
['GET', `/api/devices/${deviceId}`],
['POST', `/api/devices/${deviceId}/policy`],
['GET', '/api/device-tags'],
['PATCH', `/api/device-tags/${tagId}`],
]) {
const harness = createHarness();
await assert.rejects(
@@ -147,6 +196,9 @@ test('device route preserves endpoint gating and strict lowercase IDs', async ()
['DELETE', '/api/devices/traffic'],
['PUT', `/api/devices/${deviceId}`],
['PUT', `/api/devices/${deviceId}/policy`],
['POST', '/api/device-tags'],
['PUT', `/api/device-tags/${tagId}`],
['DELETE', `/api/device-tags/${tagId}`],
]) {
const client = createHarness({ inventory: null });
await assert.rejects(
@@ -164,6 +216,9 @@ test('device route propagates synchronous and asynchronous service errors unchan
snapshot: () => { throw syncError; },
refresh: async () => ({}),
update: () => ({}),
createTag: () => ({}),
renameTag: () => ({}),
deleteTag: () => ({}),
resetTraffic: async () => ({}),
setPolicy: async () => ({}),
},
@@ -180,6 +235,9 @@ test('device route propagates synchronous and asynchronous service errors unchan
snapshot: () => ({}),
refresh: async () => { throw asyncError; },
update: () => ({}),
createTag: () => ({}),
renameTag: () => ({}),
deleteTag: () => ({}),
resetTraffic: async () => ({}),
setPolicy: async () => ({}),
},
@@ -205,4 +263,5 @@ test('device route is the only HTTP owner while lifecycle stays in composition',
assert.match(index, /deviceInventory\.refresh\(\)/);
assert.match(route, /DEVICE_PATH/);
assert.match(route, /DEVICE_POLICY_PATH/);
assert.match(route, /DEVICE_TAG_PATH/);
});