// Attach validation and ownership checks need no PDS; no records should be accessed for these requests. import assert from 'node:assert/strict' import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import { test } from 'node:test' import { createService } from '../src/server.ts' import { hashToken } from '../src/hub.ts' import { loadState, saveState } from '../src/state.ts' test('attaching computers requires explicit agents owned by the same caller', async (t) => { const home = fs.mkdtempSync(path.join(os.tmpdir(), 'languid-attach-')) process.env.LANGUID_HOME = home const state = loadState() const alice = { did: 'did:plc:alice', handle: 'alice.test', password: 'unused' } const bob = { did: 'did:plc:bob', handle: 'bob.test', password: 'unused' } state.owner = alice state.agents = { scout: { name: 'scout', agent: alice, owner: alice.did, space: 'at://alice/space/scout' }, blip: { name: 'blip', agent: bob, owner: bob.did, space: 'at://bob/space/blip' }, } const registeredAt = new Date().toISOString() state.runners = { clipper: { name: 'clipper', owner: alice.did, tokenHash: hashToken('runner-token'), registeredAt }, other: { name: 'other', owner: bob.did, tokenHash: hashToken('other-token'), registeredAt }, } saveState(state) const svc = createService({ host: '127.0.0.1', port: 0, token: 'owner-token' }) const server = await svc.listen() const url = `http://127.0.0.1:${(server.address() as { port: number }).port}` t.after(async () => { await svc.close() fs.rmSync(home, { recursive: true, force: true }) }) const attach = (body: unknown, token = 'owner-token') => fetch(`${url}/xrpc/ai.languid.computer.attach`, { method: 'POST', headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' }, body: JSON.stringify(body), }) for (const body of [{}, { name: 'clipper' }, { name: 'clipper', agents: [] }, { name: 'clipper', agents: [''] }, { name: 'clipper', agents: [1] }]) { const result = await attach(body) assert.equal(result.status, 400, await result.text()) } assert.equal((await attach({ name: 'clipper', agents: ['scout'] }, 'invalid-token')).status, 401) for (const name of ['other', 'missing']) { const result = await attach({ name, agents: ['scout'] }) assert.equal(result.status, 404) assert.equal((await result.json() as any).error, 'ComputerNotFound') } for (const name of ['blip', 'missing']) { const result = await attach({ name: 'clipper', agents: ['scout', name] }) assert.equal(result.status, 404) assert.equal((await result.json() as any).error, 'AgentNotFound') } assert.deepEqual(loadState().runners, state.runners) })