Files
jackwener__opencli/clis/boss/utils.test.js
Benjamin Liu 1239798d04 fix(boss): map code=24 (identity mismatch) to AuthRequiredError (#1573)
Recruiter-only BOSS commands (recommend, joblist, stats, resume, mark,
exchange, invite, greet, batchgreet) returned a generic
`COMMAND_EXEC: 请切换身份后再试 (code=24)` when called from a job-seeker
account. The original error hid the actionable bit: this command set
needs a recruiter (BOSS-side) account.

chatlist / chatmsg already special-case code=24 by falling back to the
geek-side fetch when --side=auto. Recruiter-only commands have no
geek-side equivalent and were just leaking the raw API code.

Fix: add a `checkRecruiterSide` step inside `assertOk` that maps
code=24 to AuthRequiredError with a clear message. All 9 recruiter-only
commands inherit it through their existing `bossFetch` calls; no
adapter-level changes needed. chatlist / chatmsg are unaffected because
they use `allowNonZero: true` and never hit the auto-error path.

Closes #1572.
2026-05-14 22:26:28 +08:00

35 lines
1.4 KiB
JavaScript

import { describe, expect, it } from 'vitest';
import { AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { assertOk } from './utils.js';
describe('assertOk', () => {
it('returns silently on code 0', () => {
expect(() => assertOk({ code: 0 })).not.toThrow();
});
it('maps expired cookie codes (7, 37) to AuthRequiredError', () => {
expect(() => assertOk({ code: 7, message: 'expired' })).toThrow(AuthRequiredError);
expect(() => assertOk({ code: 37, message: 'expired' })).toThrow(AuthRequiredError);
});
it('maps code 24 (identity mismatch) to AuthRequiredError with recruiter-only hint', () => {
try {
assertOk({ code: 24, message: '请切换身份后再试' });
throw new Error('assertOk should have thrown');
} catch (err) {
expect(err).toBeInstanceOf(AuthRequiredError);
expect(String(err.message)).toContain('招聘端');
}
});
it('falls through to CommandExecutionError for other non-zero codes', () => {
expect(() => assertOk({ code: 99, message: 'something else' }))
.toThrow(CommandExecutionError);
});
it('throws CommandExecutionError on malformed (non-object) response', () => {
expect(() => assertOk(null)).toThrow(CommandExecutionError);
expect(() => assertOk('not-an-object')).toThrow(CommandExecutionError);
});
});