mirror of
https://github.com/NanmiCoder/cc-haha
synced 2026-07-16 13:03:31 +08:00
Show ExitPlanMode approvals as a rendered plan preview in desktop chat, forward plan feedback and requested prompt permissions through the desktop WebSocket permission response, and keep permission-mode restoration owned by the CLI runtime. Tested: bun run verify Confidence: high Scope-risk: moderate
64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
import { describe, expect, it } from 'bun:test'
|
|
import * as fs from 'node:fs'
|
|
import * as os from 'node:os'
|
|
import * as path from 'node:path'
|
|
import {
|
|
clearWhatsAppAuth,
|
|
closeWhatsAppSocket,
|
|
getWhatsAppDisconnectStatus,
|
|
hasWhatsAppAuth,
|
|
isWhatsAppLoggedOut,
|
|
waitForWhatsAppCredsSave,
|
|
} from '../session.js'
|
|
import type { WhatsAppSocket } from '../session.js'
|
|
|
|
function makeTempAuthDir(): string {
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), 'wa-session-test-'))
|
|
}
|
|
|
|
describe('whatsapp session helpers', () => {
|
|
it('detects and clears persisted auth credentials', () => {
|
|
const authDir = makeTempAuthDir()
|
|
|
|
expect(hasWhatsAppAuth(authDir)).toBe(false)
|
|
|
|
fs.writeFileSync(path.join(authDir, 'creds.json'), '{}')
|
|
expect(hasWhatsAppAuth(authDir)).toBe(true)
|
|
|
|
clearWhatsAppAuth(authDir)
|
|
expect(fs.existsSync(authDir)).toBe(false)
|
|
})
|
|
|
|
it('extracts disconnect status from Baileys and direct error shapes', () => {
|
|
expect(getWhatsAppDisconnectStatus({ output: { statusCode: 401 } })).toBe(401)
|
|
expect(getWhatsAppDisconnectStatus({ statusCode: 515 })).toBe(515)
|
|
expect(getWhatsAppDisconnectStatus({ output: { statusCode: '401' } })).toBeUndefined()
|
|
expect(getWhatsAppDisconnectStatus(null)).toBeUndefined()
|
|
expect(isWhatsAppLoggedOut({ output: { statusCode: 401 } })).toBe(true)
|
|
expect(isWhatsAppLoggedOut({ output: { statusCode: 515 } })).toBe(false)
|
|
})
|
|
|
|
it('closes sockets best-effort with a reason', () => {
|
|
let received: Error | undefined
|
|
const sock = {
|
|
end: (error?: Error) => {
|
|
received = error
|
|
},
|
|
} as unknown as WhatsAppSocket
|
|
|
|
closeWhatsAppSocket(sock, 'test close')
|
|
expect(received?.message).toBe('test close')
|
|
|
|
expect(() => closeWhatsAppSocket({} as WhatsAppSocket)).not.toThrow()
|
|
expect(() => closeWhatsAppSocket({
|
|
end: () => {
|
|
throw new Error('already closed')
|
|
},
|
|
} as unknown as WhatsAppSocket)).not.toThrow()
|
|
})
|
|
|
|
it('returns immediately when no credential save is queued', async () => {
|
|
await expect(waitForWhatsAppCredsSave(makeTempAuthDir())).resolves.toBeUndefined()
|
|
})
|
|
})
|