DSH-Arch-Setup/plugins/dsh-open-in-smerge/test/index.test.js
Шурупов Илья Викторович cf9443753e Add one-command Arch DSH setup
Package the portable DSH profiles, local plugins, desktop integration, Firefox wrapper, and ownership-aware installer lifecycle. Bundle checksum-pinned OpenVSCode, editor extensions, translation assets, and cloudflared so unreliable upstream artifact downloads cannot break a clean installation.
2026-09-12 19:13:36 +03:00

95 lines
3.5 KiB
JavaScript

import assert from 'node:assert/strict'
import { execFile } from 'node:child_process'
import { mkdtemp, mkdir, realpath, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { promisify } from 'node:util'
import test from 'node:test'
import { ActiveGitTargetStore, isLoopbackRequest, resolveDirectoryRepository, resolveSessionRepository } from '../lib/index.js'
const execFileAsync = promisify(execFile)
async function repositoryAt(path) {
await mkdir(path, { recursive: true })
await execFileAsync('git', ['init', '--quiet'], { cwd: path })
return realpath(path)
}
test('resolves the repository containing a nested working directory', async () => {
const root = await mkdtemp(join(tmpdir(), 'dsh-open-in-smerge-'))
const nested = join(root, 'modules', 'runtime')
try {
await mkdir(nested, { recursive: true })
await execFileAsync('git', ['init', '--quiet'], { cwd: root })
const resolved = await resolveDirectoryRepository(nested)
assert.equal(resolved.workingDirectory, await realpath(nested))
assert.equal(resolved.repository, await realpath(root))
} finally {
await rm(root, { recursive: true, force: true })
}
})
test('returns null when the agent has not set a target', async () => {
const root = await mkdtemp(join(tmpdir(), 'dsh-open-in-smerge-'))
try {
const store = new ActiveGitTargetStore(join(root, 'targets.json'))
const resolved = await resolveSessionRepository(store, 'session-without-target')
assert.equal(resolved, null)
} finally {
await rm(root, { recursive: true, force: true })
}
})
test('agent-set working directory resolves and persists across store reloads', async () => {
const root = await mkdtemp(join(tmpdir(), 'dsh-open-in-smerge-'))
const selectedRoot = join(root, 'runtime-worktrees', 'radio-manager-cli')
const targetFile = join(root, 'state', 'targets.json')
try {
await repositoryAt(selectedRoot)
const firstStore = new ActiveGitTargetStore(targetFile)
const selected = await resolveDirectoryRepository(selectedRoot)
await firstStore.set('session-a', selected)
const reloadedStore = new ActiveGitTargetStore(targetFile)
const resolved = await resolveSessionRepository(reloadedStore, 'session-a')
assert.equal(resolved.workingDirectory, await realpath(selectedRoot))
assert.equal(resolved.repository, await realpath(selectedRoot))
} finally {
await rm(root, { recursive: true, force: true })
}
})
test('returns null for a stale target whose directory no longer resolves', async () => {
const root = await mkdtemp(join(tmpdir(), 'dsh-open-in-smerge-'))
const targetFile = join(root, 'targets.json')
try {
const store = new ActiveGitTargetStore(targetFile)
await store.set('session-a', { workingDirectory: join(root, 'deleted-worktree'), repository: join(root, 'deleted-worktree') })
const resolved = await resolveSessionRepository(store, 'session-a')
assert.equal(resolved, null)
} finally {
await rm(root, { recursive: true, force: true })
}
})
test('accepts only same-origin loopback requests', () => {
const request = {
socket: { remoteAddress: '::ffff:127.0.0.1' },
headers: { host: '127.0.0.1:3080', origin: 'http://127.0.0.1:3080' },
}
assert.equal(isLoopbackRequest(request), true)
assert.equal(isLoopbackRequest({ ...request, headers: { ...request.headers, host: 'example.com' } }), false)
assert.equal(isLoopbackRequest({ ...request, headers: { ...request.headers, 'sec-fetch-site': 'cross-site' } }), false)
})