Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

10.3 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { GitHubIssuesClient, GitHubClientError } from './client'
import { pullStatusFrom } from '../../shared/github-pull-status'
import {
  GraphQLShapeError,
  PULL_STATUS_QUERY,
  parsePullChecksResponse,
  parsePullStatusResponse
} from './graphql-pulls'

const REPO = 'eneskirca/nodeterm'
const HEAD = 'a'.repeat(40)

/** One open-PR node in the shape GitHub returned for this repository on 2026-09-28. */
function node(number: number, over: Record<string, unknown> = {}) {
  return {
    number,
    headRefName: `feat/${number}`,
    headRefOid: HEAD,
    isCrossRepository: false,
    isDraft: false,
    mergeable: 'MERGEABLE',
    mergeStateStatus: 'CLEAN',
    closingIssuesReferences: { nodes: [] },
    commits: { nodes: [{ commit: { oid: HEAD, statusCheckRollup: { state: 'SUCCESS' } } }] },
    ...over
  }
}

function body(nodes: unknown[], over: { errors?: unknown[]; totalCount?: number; recent?: unknown[] } = {}) {
  return {
    data: {
      rateLimit: { cost: 1, remaining: 4820, limit: 5000, resetAt: '2026-09-28T21:01:03Z' },
      repository: {
        open: { totalCount: over.totalCount ?? nodes.length, nodes },
        recent: { nodes: over.recent ?? [] }
      }
    },
    ...(over.errors ? { errors: over.errors } : {})
  }
}

describe('parsePullStatusResponse', () => {
  it('decodes open pull requests, the rate limit and recent merges', () => {
    const read = parsePullStatusResponse(body([node(1)], {
      recent: [{ number: 9, headRefName: 'feat/9', isCrossRepository: true, state: 'MERGED' }]
    }), REPO)
    expect(read.open).toEqual([{
      number: 1, headRefName: 'feat/1', headRefOid: HEAD, crossRepository: false, isDraft: false,
      mergeable: 'MERGEABLE',
      mergeStateStatus: 'CLEAN', rollup: 'SUCCESS', rollupOid: HEAD, closes: []
    }])
    expect(read.recent).toEqual([{ number: 9, headRefName: 'feat/9', crossRepository: true, lifecycle: 'merged' }])
    expect(read.access).toEqual({ ci: true, merge: true })
    expect(read.rateLimit).toEqual({ cost: 1, remaining: 4820, limit: 5000, resetAt: Date.parse('2026-09-28T21:01:03Z') })
  })

  it('reads a null rollup as no checks when nothing was refused', () => {
    const read = parsePullStatusResponse(body([node(1, {
      commits: { nodes: [{ commit: { oid: HEAD, statusCheckRollup: null } }] }
    })]), REPO)
    expect(read.access.ci).toBe(true)
    expect(read.open[0].rollup).toBeNull()
  })

  it('a rollup the token may not read HIDES checks — it is not "no checks"', () => {
    const read = parsePullStatusResponse(body([node(1, {
      commits: { nodes: [{ commit: { oid: HEAD, statusCheckRollup: null } }] }
    })], {
      errors: [{
        type: 'FORBIDDEN',
        path: ['repository', 'open', 'nodes', 0, 'commits', 'nodes', 0, 'commit', 'statusCheckRollup'],
        message: 'Resource not accessible by personal access token'
      }]
    }), REPO)
    expect(read.access).toEqual({ ci: false, merge: true })
  })

  it('a token without scope for the query hides everything instead of failing', () => {
    const read = parsePullStatusResponse({
      data: null,
      errors: [{ type: 'INSUFFICIENT_SCOPES', message: 'Your token has not been granted the required scopes' }]
    }, REPO)
    expect(read).toMatchObject({ open: [], access: { ci: false, merge: false } })
  })

  it('keeps only issues in the SAME repository a pull request closes', () => {
    const read = parsePullStatusResponse(body([node(1, {
      closingIssuesReferences: { nodes: [
        { number: 4, repository: { nameWithOwner: 'EnesKirca/Nodeterm' } },
        { number: 5, repository: { nameWithOwner: 'someone/else' } }
      ] }
    })]), REPO)
    expect(read.open[0].closes).toEqual([4])
  })

  it('a closing reference the token may not see drops that reference, not the whole read', () => {
    const read = parsePullStatusResponse(body([node(1, {
      closingIssuesReferences: { nodes: [null, { number: 4, repository: { nameWithOwner: 'eneskirca/nodeterm' } }] }
    })], {
      errors: [{ type: 'FORBIDDEN', path: ['repository', 'open', 'nodes', 0, 'closingIssuesReferences', 'nodes', 0] }]
    }), REPO)
    expect(read.open[0].closes).toEqual([4])
    expect(read.access).toEqual({ ci: true, merge: true })
    // Without a permission error explaining it, a null reference is still a malformed answer.
    expect(() => parsePullStatusResponse(body([node(1, {
      closingIssuesReferences: { nodes: [null] }
    })]), REPO)).toThrow(GraphQLShapeError)
  })

  it('reports a truncated list', () => {
    expect(parsePullStatusResponse(body([node(1)], { totalCount: 60 }), REPO).truncated).toBe(true)
  })

  it('an error it cannot classify is a failed read, never an answer', () => {
    expect(() => parsePullStatusResponse(body([node(1)], {
      errors: [{ type: 'SERVICE_UNAVAILABLE', message: 'try again' }]
    }), REPO)).toThrow(GraphQLShapeError)
    expect(() => parsePullStatusResponse({ data: { repository: null } }, REPO)).toThrow(GraphQLShapeError)
  })

  it('rejects malformed nodes', () => {
    expect(() => parsePullStatusResponse(body([node(1, { headRefOid: 'nothex' })]), REPO)).toThrow()
    expect(() => parsePullStatusResponse(body([node(1, { mergeable: 7 })]), REPO)).toThrow()
    expect(() => parsePullStatusResponse(body([node(1, {
      commits: { nodes: [{ commit: { oid: HEAD, statusCheckRollup: { state: 'not an enum' } } }] }
    })]), REPO)).toThrow()
  })

  it('an enum value GitHub adds later claims nothing instead of failing the read', () => {
    const read = parsePullStatusResponse(body([node(1, {
      mergeable: 'SOMETIMES',
      commits: { nodes: [{ commit: { oid: HEAD, statusCheckRollup: { state: 'NEW_STATE' } } }] }
    })]), REPO)
    expect(read.open[0]).toMatchObject({ mergeable: null, rollup: 'UNRECOGNIZED' })
    const status = pullStatusFrom(read.open[0], read.access)
    expect(status.ci).toBeUndefined()
    expect(status.merge).toBeUndefined()
  })
})

describe('parsePullChecksResponse', () => {
  const checks = (commitOid: string, rollup: unknown) => ({
    data: { repository: { pullRequest: {
      headRefOid: HEAD,
      commits: { nodes: [{ commit: { oid: commitOid, statusCheckRollup: rollup } }] }
    } } }
  })

  it('lists checks, failures first, with https links only', () => {
    const result = parsePullChecksResponse(checks(HEAD, { state: 'FAILURE', contexts: { totalCount: 3, nodes: [
      { __typename: 'CheckRun', name: 'quality', status: 'COMPLETED', conclusion: 'SUCCESS',
        detailsUrl: 'https://github.com/x/actions/runs/1' },
      { __typename: 'CheckRun', name: 'windows', status: 'COMPLETED', conclusion: 'FAILURE',
        detailsUrl: 'javascript:alert(1)' },
      { __typename: 'StatusContext', context: 'ci/legacy', state: 'PENDING', targetUrl: 'https://ci.example/1' }
    ] } }))
    expect(result).toEqual({
      status: 'ok', headRefOid: HEAD, truncated: false, checks: [
        { name: 'windows', state: 'failed' },
        { name: 'ci/legacy', state: 'pending', url: 'https://ci.example/1' },
        { name: 'quality', state: 'passed', url: 'https://github.com/x/actions/runs/1' }
      ]
    })
  })

  it('says "no checks" for a null rollup, "moved" for another commit, "hidden" when refused', () => {
    expect(parsePullChecksResponse(checks(HEAD, null))).toEqual({ status: 'no-checks' })
    expect(parsePullChecksResponse(checks('b'.repeat(40), { state: 'SUCCESS', contexts: { totalCount: 0, nodes: [] } })))
      .toEqual({ status: 'moved' })
    expect(parsePullChecksResponse({ ...checks(HEAD, null), errors: [{ type: 'FORBIDDEN', path: ['repository'] }] }))
      .toEqual({ status: 'hidden' })
  })
})

describe('GitHubIssuesClient GraphQL', () => {
  function json(value: unknown, headers: Record<string, string> = {}, status = 200): Response {
    return new Response(JSON.stringify(value), { status, headers: { 'content-type': 'application/json', ...headers } })
  }

  it('posts one query to the GraphQL endpoint and feeds its budget to the observer', async () => {
    const calls: Array<{ url: string; init: RequestInit }> = []
    const samples: unknown[] = []
    const client = new GitHubIssuesClient({
      token: 'secret',
      onRateLimit: (sample) => samples.push(sample),
      fetch: async (url, init) => {
        calls.push({ url: String(url), init: init ?? {} })
        return json(body([node(1)]), {
          'x-ratelimit-limit': '5000', 'x-ratelimit-remaining': '4819',
          'x-ratelimit-reset': '1790629263', 'x-ratelimit-resource': 'graphql'
        })
      }
    })
    const read = await client.pullRequestStatuses(REPO)
    expect(read.open).toHaveLength(1)
    expect(calls).toHaveLength(1)
    expect(calls[0].url).toBe('https://api.github.com/graphql')
    expect(calls[0].init.method).toBe('POST')
    expect(JSON.parse(String(calls[0].init.body))).toEqual({
      query: PULL_STATUS_QUERY, variables: { owner: 'eneskirca', name: 'nodeterm' }
    })
    expect(samples).toEqual([{ resource: 'graphql', limit: 5000, remaining: 4819, resetAt: 1790629263_000 }])
  })

  it('turns a RATE_LIMITED error inside a 200 into a graphql-scoped rate limit', async () => {
    const client = new GitHubIssuesClient({
      token: 'secret',
      fetch: async () => json({ data: null, errors: [{ type: 'RATE_LIMITED', message: 'API rate limit exceeded' }] },
        { 'x-ratelimit-reset': '1790629263', 'x-ratelimit-remaining': '0', 'x-ratelimit-limit': '5000' })
    })
    const error = await client.pullRequestStatuses(REPO).catch((caught: unknown) => caught)
    expect(error).toBeInstanceOf(GitHubClientError)
    expect(error).toMatchObject({ code: 'rate-limited', retryAt: 1790629263_000, resource: 'graphql' })
  })

  it('tags a primary 403 with the budget it belongs to', async () => {
    const client = new GitHubIssuesClient({
      token: 'secret',
      fetch: async () => json({ message: 'API rate limit exceeded' }, {
        'x-ratelimit-remaining': '0', 'x-ratelimit-limit': '5000',
        'x-ratelimit-reset': '1790629263', 'x-ratelimit-resource': 'graphql'
      }, 403)
    })
    await expect(client.pullRequestStatuses(REPO)).rejects.toMatchObject({
      code: 'rate-limited', resource: 'graphql'
    })
  })

  it('reports a malformed GraphQL body as malformed', async () => {
    const client = new GitHubIssuesClient({ token: 'secret', fetch: async () => json({ data: { repository: null } }) })
    await expect(client.pullRequestStatuses(REPO)).rejects.toMatchObject({ code: 'malformed-response' })
  })
})