← Public packages
@kentcdodds/devin
Start, monitor, and manage Devin sessions, knowledge, playbooks, and schedules via the Devin v3 API
Files
import assert from 'node:assert/strict'
import { registerHooks } from 'node:module'
import { beforeEach, describe, it } from 'node:test'
/**
* Default `create_as_user_id` resolution. `kody:runtime` is faked with an
* in-memory package storage and `fetch` is a recorder, so nothing reaches
* Devin.
*/
type Call = { url: string; method: string; body: unknown }
const fakes = {
store: new Map<string, unknown>(),
calls: [] as Call[],
}
;(globalThis as Record<string, unknown>).__devinFakes = fakes
registerHooks({
resolve(specifier, context, nextResolve) {
if (specifier === 'kody:runtime') {
return { url: 'fake:kody-runtime', shortCircuit: true }
}
return nextResolve(specifier, context)
},
load(url, context, nextLoad) {
if (url === 'fake:kody-runtime') {
return {
format: 'module',
shortCircuit: true,
source: `
const f = globalThis.__devinFakes
export function packageStorage() {
return {
async get(key) { return f.store.get(key) ?? null },
async set(key, value) { f.store.set(key, value) },
async delete(key) { f.store.delete(key) },
}
}
`,
}
}
return nextLoad(url, context)
},
})
globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => {
fakes.calls.push({
url: String(input),
method: init?.method ?? 'GET',
body: typeof init?.body === 'string' ? JSON.parse(init.body) : init?.body,
})
return new Response(JSON.stringify({ session_id: 'devin-test' }), {
status: 200,
headers: { 'content-type': 'application/json' },
})
}) as typeof fetch
const { createSession, buildCreateSessionBody } = await import('./sessions.ts')
const { createSchedule, updateSchedule } = await import('./schedules.ts')
const { devinRequest, buildDevinRequestBody } = await import('./request.ts')
const { default: devinSettings } = await import('./settings.ts')
const STORED = 'user-stored'
const lastBody = () => fakes.calls.at(-1)?.body as Record<string, unknown>
beforeEach(async () => {
fakes.store.clear()
fakes.calls.length = 0
await devinSettings({ orgId: 'org-test' })
})
describe('settings', () => {
it('stores, reads, and clears createAsUserId', async () => {
assert.deepEqual(await devinSettings(), { orgId: 'org-test', createAsUserId: null })
const result = await devinSettings({ createAsUserId: STORED })
assert.deepEqual(result, {
updated: ['createAsUserId'],
settings: { orgId: 'org-test', createAsUserId: STORED },
})
assert.equal(fakes.store.get('devinCreateAsUserId'), STORED)
await devinSettings({ createAsUserId: null })
assert.equal(fakes.store.has('devinCreateAsUserId'), false)
})
it('rejects ids that are not Devin user ids', async () => {
await assert.rejects(devinSettings({ createAsUserId: 'bot_apk_123' }), /user-/)
})
})
describe('createSession', () => {
it('sends no user when nothing is stored (unchanged behavior)', async () => {
await createSession({ prompt: 'hi' })
assert.equal('create_as_user_id' in lastBody(), false)
assert.equal(fakes.calls[0]?.url, 'https://api.devin.ai/v3/organizations/org-test/sessions')
})
it('uses the stored default, explicit override, and null opt-out', async () => {
await devinSettings({ createAsUserId: STORED })
await createSession({ prompt: 'hi' })
assert.equal(lastBody().create_as_user_id, STORED)
await createSession({ prompt: 'hi', createAsUserId: 'user-explicit' })
assert.equal(lastBody().create_as_user_id, 'user-explicit')
await createSession({ prompt: 'hi', createAsUserId: null })
assert.equal('create_as_user_id' in lastBody(), false)
})
it('buildCreateSessionBody matches without calling Devin', async () => {
await devinSettings({ createAsUserId: STORED })
const body = await buildCreateSessionBody({ prompt: 'hi', tags: ['t'] })
assert.deepEqual(body, { prompt: 'hi', tags: ['t'], create_as_user_id: STORED })
assert.equal(fakes.calls.length, 0)
})
})
describe('schedules', () => {
it('createSchedule applies the default, override, and opt-out', async () => {
await devinSettings({ createAsUserId: STORED })
await createSchedule({ name: 'n', prompt: 'p' })
assert.equal(lastBody().create_as_user_id, STORED)
await createSchedule({ name: 'n', prompt: 'p', createAsUserId: 'user-x' })
assert.equal(lastBody().create_as_user_id, 'user-x')
await createSchedule({ name: 'n', prompt: 'p', createAsUserId: null })
assert.equal('create_as_user_id' in lastBody(), false)
})
it('updateSchedule never applies the default; null reverts', async () => {
await devinSettings({ createAsUserId: STORED })
await updateSchedule({ scheduleId: 's1', enabled: false })
assert.deepEqual(lastBody(), { enabled: false })
await updateSchedule({ scheduleId: 's1', runAsUserId: null })
assert.deepEqual(lastBody(), { run_as_user_id: null })
})
})
describe('devinRequest (raw)', () => {
const path = '/v3/organizations/{org_id}/sessions'
it('adds the default to create-session POSTs only', async () => {
await devinSettings({ createAsUserId: STORED })
await devinRequest({ path, method: 'POST', body: { prompt: 'p' } })
assert.equal(lastBody().create_as_user_id, STORED)
await devinRequest({ path: '/v3/organizations/{org_id}/schedules', method: 'POST', body: { name: 'n' } })
assert.equal(lastBody().create_as_user_id, STORED)
await devinRequest({ path: `${path}/abc/messages`, method: 'POST', body: { message: 'm' } })
assert.equal('create_as_user_id' in lastBody(), false)
await devinRequest({ path: '/v3/organizations/{org_id}/schedules/s1', method: 'PATCH', body: { enabled: true } })
assert.equal('create_as_user_id' in lastBody(), false)
})
it('respects an explicit body value and null opt-out', async () => {
await devinSettings({ createAsUserId: STORED })
await devinRequest({ path, method: 'POST', body: { prompt: 'p', create_as_user_id: 'user-x' } })
assert.equal(lastBody().create_as_user_id, 'user-x')
await devinRequest({ path, method: 'POST', body: { prompt: 'p', create_as_user_id: null } })
assert.equal(lastBody().create_as_user_id, null)
})
it('leaves bodies alone when nothing is stored', async () => {
await devinRequest({ path, method: 'POST', body: { prompt: 'p' } })
assert.deepEqual(lastBody(), { prompt: 'p' })
})
it('buildDevinRequestBody previews without calling Devin', async () => {
await devinSettings({ createAsUserId: STORED })
assert.deepEqual(await buildDevinRequestBody({ path, method: 'POST', body: { prompt: 'p' } }), {
prompt: 'p',
create_as_user_id: STORED,
})
assert.equal(fakes.calls.length, 0)
})
})