import { describe, it, expect } from "@normalized:N&&&@ohos/hypium/index&1.0.25";
import { SignResultParser } from "@normalized:N&&&tencentqianwebview/src/main/ets/scheme/SignResultParser&1.0.0";
import type { SignResult } from '../main/ets/model/SignResult';
/**
 * SignResultParser 单元测试 - 全场景覆盖。
 *
 * 与 Android `SignResultParserTest` / iOS `SignResultParserTests` 共享 13 类用例,
 * 断言行为完全对齐(参考 docs/qianapp-protocol.md §8 示例)。
 */
export default function signResultParserTest() {
    describe('SignResultParser', () => {
        // ── Scheme 识别 ──
        it('isQianAppScheme_returnsTrue_forQianAppUrl', 0, () => {
            expect(SignResultParser.isQianAppScheme('qianapp://x')).assertTrue();
            expect(SignResultParser.isQianAppScheme('QIANAPP://X')).assertTrue();
        });
        it('isQianAppScheme_returnsFalse_forOtherSchemes', 0, () => {
            expect(SignResultParser.isQianAppScheme('https://x')).assertFalse();
            expect(SignResultParser.isQianAppScheme('weixin://x')).assertFalse();
            expect(SignResultParser.isQianAppScheme('')).assertFalse();
            expect(SignResultParser.isQianAppScheme(null)).assertFalse();
        });
        // ── 标准解析(qianapp-protocol §8.1) ──
        it('parse_completeUrl_extractsAllRecognizedFields', 0, () => {
            const url: string = 'qianapp://callback/sign?flowId=abc-123&action=sign&result=success&from=tencent_ess';
            const r: SignResult = SignResultParser.parse(url);
            expect(r.rawUrl).assertEqual(url);
            expect(r.customPath).assertEqual('callback/sign');
            expect(r.flowId).assertEqual('abc-123');
            expect(r.action).assertEqual('sign');
            expect(r.result).assertEqual('success');
            expect(r.from).assertEqual('tencent_ess');
            expect(r.extras.size).assertEqual(0);
        });
        // ── 缺失字段(qianapp-protocol §6) ──
        it('parse_missingOptionalFields_returnsEmptyDefaults', 0, () => {
            const url: string = 'qianapp://path?action=sign&from=tencent_ess';
            const r: SignResult = SignResultParser.parse(url);
            expect(r.flowId).assertEqual('');
            expect(r.result).assertEqual('');
            expect(r.action).assertEqual('sign');
            expect(r.from).assertEqual('tencent_ess');
        });
        // ── 未知 action / result 透传(qianapp-protocol §8.3) ──
        it('parse_unknownAction_passesThrough', 0, () => {
            const url: string = 'qianapp://x?action=futureBrandNewAction&result=mystery';
            const r: SignResult = SignResultParser.parse(url);
            expect(r.action).assertEqual('futureBrandNewAction');
            expect(r.result).assertEqual('mystery');
        });
        // ── 百分号解码(qianapp-protocol §8.4) ──
        it('parse_percentEncoded_decodesUtf8', 0, () => {
            const url: string = 'qianapp://x?action=sign&note=%E7%AD%BE%E7%BD%B2%E6%88%90%E5%8A%9F';
            const r: SignResult = SignResultParser.parse(url);
            expect(r.action).assertEqual('sign');
            expect(r.extras.get('note')).assertEqual('签署成功');
        });
        it('parse_plusSign_decodedAsSpace', 0, () => {
            const url: string = 'qianapp://x?action=sign&note=hello+world';
            const r: SignResult = SignResultParser.parse(url);
            expect(r.extras.get('note')).assertEqual('hello world');
        });
        // ── extras(qianapp-protocol §8.2 / §8.5) ──
        it('parse_extrasContainErrorCodeAndCustomFields', 0, () => {
            const url: string = 'qianapp://result?flowId=f2&action=reject_sign&result=fail&from=tencent_ess&errorCode=10086&errorMsg=user_canceled';
            const r: SignResult = SignResultParser.parse(url);
            expect(r.action).assertEqual('reject_sign');
            expect(r.result).assertEqual('fail');
            expect(r.extras.size).assertEqual(2);
            expect(r.extras.get('errorCode')).assertEqual('10086');
            expect(r.extras.get('errorMsg')).assertEqual('user_canceled');
        });
        // ── 重复 key(取首个) ──
        it('parse_duplicateKey_keepsFirstValue', 0, () => {
            const url: string = 'qianapp://x?action=sign&action=fill';
            const r: SignResult = SignResultParser.parse(url);
            expect(r.action).assertEqual('sign');
        });
        // ── 空 value 保留为空字符串 ──
        it('parse_emptyValue_keepsEmptyString', 0, () => {
            const url: string = 'qianapp://x?action=&result=';
            const r: SignResult = SignResultParser.parse(url);
            expect(r.action).assertEqual('');
            expect(r.result).assertEqual('');
        });
        // ── 畸形 URL fallback ──
        // 注意: SignResult.EMPTY 已改为 getter,每次返回新实例(详见 SignResult.ets),
        // 不能再用 `=== SignResult.EMPTY` 做引用比较,改用 rawUrl 为空判定空结果。
        it('parse_nullOrEmpty_returnsEmpty', 0, () => {
            expect(SignResultParser.parse(null).rawUrl === '').assertTrue();
            expect(SignResultParser.parse('').rawUrl === '').assertTrue();
        });
        it('parse_nonQianAppScheme_returnsEmpty', 0, () => {
            expect(SignResultParser.parse('https://x?a=1').rawUrl === '').assertTrue();
        });
        it('parse_oversizedUrl_returnsEmpty', 0, () => {
            // 构造超长 URL > 8KB(MAX_URL_LENGTH)
            // 单段 64 字符,需要 > 8192 / 64 = 128 段才能超过 8KB,这里取 200 段保守
            let padding: string = '';
            for (let i: number = 0; i < 200; i++) {
                padding = padding + '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
            }
            const longUrl: string = 'qianapp://x?action=sign&padding=' + padding;
            const r: SignResult = SignResultParser.parse(longUrl);
            expect(r.rawUrl === '').assertTrue();
        });
        // ── customPath 处理 ──
        it('parse_customPath_trimsSlashes', 0, () => {
            const r: SignResult = SignResultParser.parse('qianapp://callback/sign/?action=sign');
            expect(r.customPath).assertEqual('callback/sign');
        });
        it('parse_customPath_handlesNoPath', 0, () => {
            const r: SignResult = SignResultParser.parse('qianapp://?action=sign');
            expect(r.customPath).assertEqual('');
            expect(r.action).assertEqual('sign');
        });
        // ── 大小写不敏感 scheme ──
        it('parse_uppercaseScheme_isAccepted', 0, () => {
            const r: SignResult = SignResultParser.parse('QIANAPP://result?action=sign');
            expect(r.action).assertEqual('sign');
        });
    });
}
