import Foundationimport Testing@testable import CodexBarCore struct AlibabaCodingPlanSettingsReaderTests { @Test func `api token reads from environment`() { let token = AlibabaCodingPlanSettingsReader.apiToken(environment: ["ALIBABA_CODING_PLAN_API_KEY": "abc123"]) #expect(token == "abc123") } @Test func `api token reads qwen alias from environment`() { let token = AlibabaCodingPlanSettingsReader.apiToken(environment: ["ALIBABA_QWEN_API_KEY": "qwen123"]) #expect(token == "qwen123") } @Test func `api token reads dashscope alias from environment`() { let token = AlibabaCodingPlanSettingsReader.apiToken(environment: ["DASHSCOPE_API_KEY": "dashscope123"]) #expect(token == "dashscope123") } @Test func `api token prefers coding plan key over aliases`() { let token = AlibabaCodingPlanSettingsReader.apiToken(environment: [ "ALIBABA_CODING_PLAN_API_KEY": "coding-plan", "ALIBABA_QWEN_API_KEY": "qwen", "DASHSCOPE_API_KEY": "dashscope", ]) #expect(token == "coding-plan") } @Test func `api token strips quotes`() { let token = AlibabaCodingPlanSettingsReader .apiToken(environment: ["ALIBABA_CODING_PLAN_API_KEY": "\"token-xyz\""]) #expect(token == "token-xyz") } @Test func `quota URL infers scheme`() { let url = AlibabaCodingPlanSettingsReader .quotaURL(environment: [AlibabaCodingPlanSettingsReader .quotaURLKey: "modelstudio.console.alibabacloud.com/data/api.json"]) #expect(url?.absoluteString == "https://modelstudio.console.alibabacloud.com/data/api.json") } @Test func `endpoint overrides allow custom https hosts by default`() { let env = [ AlibabaCodingPlanSettingsReader.hostKey: "https://attacker.example", AlibabaCodingPlanSettingsReader.quotaURLKey: "https://attacker.example/data/api.json", ] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == "attacker.example") #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: env)?.host == "attacker.example") #expect(AlibabaCodingPlanSettingsReader.rejectedEndpointOverrideKey(environment: env) == nil) } @Test func `host endpoint overrides preserve explicit port`() { let env = [AlibabaCodingPlanSettingsReader.hostKey: "proxy.example.test:8443"] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == "proxy.example.test:8443") #expect( AlibabaCodingPlanUsageFetcher.resolveQuotaURL(region: .international, environment: env).absoluteString == "https://proxy.example.test:8443/data/api.json?action=zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2&product=broadscope-bailian&api=queryCodingPlanInstanceInfoV2¤tRegionId=ap-southeast-1") #expect( AlibabaCodingPlanUsageFetcher.resolveConsoleDashboardURL(region: .international, environment: env) .absoluteString .hasPrefix("https://proxy.example.test:8443/") == true) } @Test func `endpoint overrides reject encoded host delimiters before suffix matching`() { let encodedSlash = "https://attacker.example%2f.modelstudio.console.alibabacloud.com" let doubleEncodedSlash = "https://attacker.example%252f.modelstudio.console.alibabacloud.com" let env = [ AlibabaCodingPlanSettingsReader.hostKey: encodedSlash, AlibabaCodingPlanSettingsReader.quotaURLKey: "\(encodedSlash)/data/api.json", ] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: [ AlibabaCodingPlanSettingsReader.hostKey: doubleEncodedSlash, ]) == nil) } @Test func `endpoint overrides reject whitespace and control characters in hosts`() { for host in ["https://bad host", "https://bad%20host", "https://bad%09host"] { #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: [ AlibabaCodingPlanSettingsReader.hostKey: host, ]) == nil) #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: [ AlibabaCodingPlanSettingsReader.quotaURLKey: "\(host)/data/api.json", ]) == nil) } } @Test func `endpoint overrides require https and no userinfo`() { #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: [ AlibabaCodingPlanSettingsReader.hostKey: "http://modelstudio.console.alibabacloud.com", ]) == nil) #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: [ AlibabaCodingPlanSettingsReader.quotaURLKey: "https://user:pass@modelstudio.console.alibabacloud.com/data/api.json", ]) == nil) } @Test func `strict provider endpoint mode rejects custom hosts`() { let env = [ AlibabaCodingPlanSettingsReader.requireProviderEndpointOverridesKey: "true", AlibabaCodingPlanSettingsReader.hostKey: "proxy.example.test", AlibabaCodingPlanSettingsReader.quotaURLKey: "https://proxy.example.test/data/api.json", ] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader .rejectedEndpointOverrideKey(environment: env) == AlibabaCodingPlanSettingsReader.hostKey) } @Test func `strict provider endpoint mode rejects customer controlled Alibaba Cloud hosts`() { let env = [ AlibabaCodingPlanSettingsReader.requireProviderEndpointOverridesKey: "true", AlibabaCodingPlanSettingsReader.hostKey: "tenant.cn-beijing.fc.aliyuncs.com", ] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader .rejectedEndpointOverrideKey(environment: env) == AlibabaCodingPlanSettingsReader.hostKey) } @Test func `strict provider endpoint mode accepts known Coding Plan hosts`() { let env = [ AlibabaCodingPlanSettingsReader.requireProviderEndpointOverridesKey: "true", AlibabaCodingPlanSettingsReader.hostKey: "bailian-beijing-cs.aliyuncs.com", ] #expect( AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == "bailian-beijing-cs.aliyuncs.com") #expect(AlibabaCodingPlanSettingsReader.rejectedEndpointOverrideKey(environment: env) == nil) } @Test func `custom https compatibility mode still rejects http and userinfo`() { #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: [ AlibabaCodingPlanSettingsReader.hostKey: "http://proxy.example.test", ]) == nil) #expect(AlibabaCodingPlanSettingsReader.rejectedEndpointOverrideKey(environment: [ AlibabaCodingPlanSettingsReader.quotaURLKey: "https://user:pass@proxy.example.test/data/api.json", ]) == AlibabaCodingPlanSettingsReader.quotaURLKey) } @Test func `missing cookie error includes access hint when present`() { let error = AlibabaCodingPlanSettingsError .missingCookie(details: "Safari cookie file exists but is not readable.") #expect(error.errorDescription?.contains("Safari cookie file exists but is not readable.") == true) }} struct AlibabaCodingPlanUsageSnapshotTests { @Test func `maps usage snapshot windows`() { let now = Date(timeIntervalSince1970: 1_700_000_000) let reset5h = Date(timeIntervalSince1970: 1_700_000_300) let resetWeek = Date(timeIntervalSince1970: 1_700_010_000) let resetMonth = Date(timeIntervalSince1970: 1_700_100_000) let snapshot = AlibabaCodingPlanUsageSnapshot( planName: "Pro", fiveHourUsedQuota: 20, fiveHourTotalQuota: 100, fiveHourNextRefreshTime: reset5h, weeklyUsedQuota: 120, weeklyTotalQuota: 400, weeklyNextRefreshTime: resetWeek, monthlyUsedQuota: 500, monthlyTotalQuota: 2000, monthlyNextRefreshTime: resetMonth, updatedAt: now) let usage = snapshot.toUsageSnapshot() #expect(usage.primary?.usedPercent == 20) #expect(usage.primary?.windowMinutes == 300) #expect(usage.secondary?.usedPercent == 30) #expect(usage.secondary?.windowMinutes == 10080) #expect(usage.tertiary?.usedPercent == 25) #expect(usage.tertiary?.windowMinutes == 43200) #expect(usage.loginMethod(for: .alibaba) == "Pro") } @Test func `shifts primary reset forward when backend reset is not future`() { let now = Date(timeIntervalSince1970: 1_700_000_000) let stalePrimaryReset = Date(timeIntervalSince1970: 1_699_999_900) let snapshot = AlibabaCodingPlanUsageSnapshot( planName: "Lite", fiveHourUsedQuota: 70, fiveHourTotalQuota: 1200, fiveHourNextRefreshTime: stalePrimaryReset, weeklyUsedQuota: 80, weeklyTotalQuota: 9000, weeklyNextRefreshTime: Date(timeIntervalSince1970: 1_700_010_000), monthlyUsedQuota: 80, monthlyTotalQuota: 18000, monthlyNextRefreshTime: Date(timeIntervalSince1970: 1_700_100_000), updatedAt: now) let usage = snapshot.toUsageSnapshot() #expect(usage.primary?.resetsAt == stalePrimaryReset.addingTimeInterval(TimeInterval(5 * 60 * 60))) }} struct AlibabaCodingPlanUsageParsingTests { @Test func `parses quota payload`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Alibaba Coding Plan Pro" } ], "codingPlanQuotaInfo": { "per5HourUsedQuota": 52, "per5HourTotalQuota": 1000, "per5HourQuotaNextRefreshTime": 1700000300000, "perWeekUsedQuota": 800, "perWeekTotalQuota": 5000, "perWeekQuotaNextRefreshTime": 1700100000000, "perBillMonthUsedQuota": 1200, "perBillMonthTotalQuota": 20000, "perBillMonthQuotaNextRefreshTime": 1701000000000 } }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Alibaba Coding Plan Pro") #expect(snapshot.fiveHourUsedQuota == 52) #expect(snapshot.fiveHourTotalQuota == 1000) #expect(snapshot.weeklyTotalQuota == 5000) #expect(snapshot.monthlyTotalQuota == 20000) #expect(snapshot.fiveHourNextRefreshTime == Date(timeIntervalSince1970: 1_700_000_300)) } @Test func `multi instance quota payload uses selected active instance plan name`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Expired Starter", "status": "EXPIRED", "endTime": "2025-04-01 17:00", "codingPlanQuotaInfo": { "per5HourUsedQuota": 7, "per5HourTotalQuota": 100, "per5HourQuotaNextRefreshTime": 1700000100000 } }, { "planName": "Active Pro", "status": "VALID", "codingPlanQuotaInfo": { "per5HourUsedQuota": 52, "per5HourTotalQuota": 1000, "per5HourQuotaNextRefreshTime": 1700000300000 } } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Active Pro") #expect(snapshot.fiveHourUsedQuota == 52) #expect(snapshot.fiveHourTotalQuota == 1000) #expect(snapshot.fiveHourNextRefreshTime == Date(timeIntervalSince1970: 1_700_000_300)) } @Test func `missing quota data without positive active signal fails`() { let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Alibaba Coding Plan Pro" } ] }, "status_code": 0 } """ #expect(throws: AlibabaCodingPlanUsageError.self) { try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8)) } } @Test func `plan usage without positive active proof fails`() { let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Alibaba Coding Plan Pro", "planUsage": "18%" } ] }, "status_code": 0 } """ #expect(throws: AlibabaCodingPlanUsageError.self) { try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8)) } } @Test func `parses wrapped JSON string payload`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let inner = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Coding Plan Lite", "status": "VALID", "codingPlanQuotaInfo": { "per5HourUsedQuota": 0, "per5HourTotalQuota": 1000, "per5HourQuotaNextRefreshTime": 1700000300000 } } ] }, "statusCode": 200 } """ .replacingOccurrences(of: "\n", with: "") .replacingOccurrences(of: " ", with: "") .replacingOccurrences(of: "\"", with: "\\\"") let wrapped = """ { "successResponse": { "body": "\(inner)" } } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(wrapped.utf8), now: now) #expect(snapshot.planName == "Coding Plan Lite") #expect(snapshot.fiveHourTotalQuota == 1000) #expect(snapshot.fiveHourUsedQuota == 0) } @Test func `plan usage fallback stays visible but non quantitative`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Coding Plan Lite", "status": "VALID", "planUsage": "0%", "endTime": "2026-04-01 17:00" } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Coding Plan Lite") #expect(snapshot.fiveHourUsedQuota == nil) #expect(snapshot.fiveHourTotalQuota == nil) #expect(snapshot.fiveHourNextRefreshTime == nil) let usage = snapshot.toUsageSnapshot() #expect(usage.primary == nil) #expect(usage.loginMethod(for: .alibaba) == "Coding Plan Lite") } @Test func `falls back to active plan when quota and usage missing`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Coding Plan Lite", "status": "VALID", "endTime": "2026-04-01 17:00" } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Coding Plan Lite") #expect(snapshot.fiveHourUsedQuota == nil) #expect(snapshot.fiveHourTotalQuota == nil) let usage = snapshot.toUsageSnapshot() #expect(usage.primary == nil) #expect(usage.loginMethod(for: .alibaba) == "Coding Plan Lite") } @Test func `future end time counts as positive active signal`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Coding Plan Lite", "endTime": "2030-04-01 17:00" } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Coding Plan Lite") #expect(snapshot.fiveHourUsedQuota == nil) #expect(snapshot.weeklyTotalQuota == nil) let usage = snapshot.toUsageSnapshot() #expect(usage.primary == nil) #expect(usage.loginMethod(for: .alibaba) == "Coding Plan Lite") } @Test func `multi instance fallback uses selected active instance plan name`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Expired Starter", "status": "EXPIRED", "endTime": "2025-04-01 17:00" }, { "planName": "Active Pro", "status": "VALID", "planUsage": "42%" } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Active Pro") #expect(snapshot.fiveHourUsedQuota == nil) #expect(snapshot.fiveHourTotalQuota == nil) let usage = snapshot.toUsageSnapshot() #expect(usage.primary == nil) #expect(usage.loginMethod(for: .alibaba) == "Active Pro") } Lines 1–500 of 1145. Load more
import Foundation import Testing @testable import CodexBarCore struct AlibabaCodingPlanSettingsReaderTests { @Test func `api token reads from environment`() { let token = AlibabaCodingPlanSettingsReader.apiToken(environment: ["ALIBABA_CODING_PLAN_API_KEY": "abc123"]) #expect(token == "abc123") } @Test func `api token reads qwen alias from environment`() { let token = AlibabaCodingPlanSettingsReader.apiToken(environment: ["ALIBABA_QWEN_API_KEY": "qwen123"]) #expect(token == "qwen123") } @Test func `api token reads dashscope alias from environment`() { let token = AlibabaCodingPlanSettingsReader.apiToken(environment: ["DASHSCOPE_API_KEY": "dashscope123"]) #expect(token == "dashscope123") } @Test func `api token prefers coding plan key over aliases`() { let token = AlibabaCodingPlanSettingsReader.apiToken(environment: [ "ALIBABA_CODING_PLAN_API_KEY": "coding-plan", "ALIBABA_QWEN_API_KEY": "qwen", "DASHSCOPE_API_KEY": "dashscope", ]) #expect(token == "coding-plan") } @Test func `api token strips quotes`() { let token = AlibabaCodingPlanSettingsReader .apiToken(environment: ["ALIBABA_CODING_PLAN_API_KEY": "\"token-xyz\""]) #expect(token == "token-xyz") } @Test func `quota URL infers scheme`() { let url = AlibabaCodingPlanSettingsReader .quotaURL(environment: [AlibabaCodingPlanSettingsReader .quotaURLKey: "modelstudio.console.alibabacloud.com/data/api.json"]) #expect(url?.absoluteString == "https://modelstudio.console.alibabacloud.com/data/api.json") } @Test func `endpoint overrides allow custom https hosts by default`() { let env = [ AlibabaCodingPlanSettingsReader.hostKey: "https://attacker.example", AlibabaCodingPlanSettingsReader.quotaURLKey: "https://attacker.example/data/api.json", ] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == "attacker.example") #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: env)?.host == "attacker.example") #expect(AlibabaCodingPlanSettingsReader.rejectedEndpointOverrideKey(environment: env) == nil) } @Test func `host endpoint overrides preserve explicit port`() { let env = [AlibabaCodingPlanSettingsReader.hostKey: "proxy.example.test:8443"] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == "proxy.example.test:8443") #expect( AlibabaCodingPlanUsageFetcher.resolveQuotaURL(region: .international, environment: env).absoluteString == "https://proxy.example.test:8443/data/api.json?action=zeldaEasy.broadscope-bailian.codingPlan.queryCodingPlanInstanceInfoV2&product=broadscope-bailian&api=queryCodingPlanInstanceInfoV2¤tRegionId=ap-southeast-1") #expect( AlibabaCodingPlanUsageFetcher.resolveConsoleDashboardURL(region: .international, environment: env) .absoluteString .hasPrefix("https://proxy.example.test:8443/") == true) } @Test func `endpoint overrides reject encoded host delimiters before suffix matching`() { let encodedSlash = "https://attacker.example%2f.modelstudio.console.alibabacloud.com" let doubleEncodedSlash = "https://attacker.example%252f.modelstudio.console.alibabacloud.com" let env = [ AlibabaCodingPlanSettingsReader.hostKey: encodedSlash, AlibabaCodingPlanSettingsReader.quotaURLKey: "\(encodedSlash)/data/api.json", ] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: [ AlibabaCodingPlanSettingsReader.hostKey: doubleEncodedSlash, ]) == nil) } @Test func `endpoint overrides reject whitespace and control characters in hosts`() { for host in ["https://bad host", "https://bad%20host", "https://bad%09host"] { #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: [ AlibabaCodingPlanSettingsReader.hostKey: host, ]) == nil) #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: [ AlibabaCodingPlanSettingsReader.quotaURLKey: "\(host)/data/api.json", ]) == nil) } } @Test func `endpoint overrides require https and no userinfo`() { #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: [ AlibabaCodingPlanSettingsReader.hostKey: "http://modelstudio.console.alibabacloud.com", ]) == nil) #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: [ AlibabaCodingPlanSettingsReader.quotaURLKey: "https://user:pass@modelstudio.console.alibabacloud.com/data/api.json", ]) == nil) } @Test func `strict provider endpoint mode rejects custom hosts`() { let env = [ AlibabaCodingPlanSettingsReader.requireProviderEndpointOverridesKey: "true", AlibabaCodingPlanSettingsReader.hostKey: "proxy.example.test", AlibabaCodingPlanSettingsReader.quotaURLKey: "https://proxy.example.test/data/api.json", ] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader.quotaURL(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader .rejectedEndpointOverrideKey(environment: env) == AlibabaCodingPlanSettingsReader.hostKey) } @Test func `strict provider endpoint mode rejects customer controlled Alibaba Cloud hosts`() { let env = [ AlibabaCodingPlanSettingsReader.requireProviderEndpointOverridesKey: "true", AlibabaCodingPlanSettingsReader.hostKey: "tenant.cn-beijing.fc.aliyuncs.com", ] #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == nil) #expect(AlibabaCodingPlanSettingsReader .rejectedEndpointOverrideKey(environment: env) == AlibabaCodingPlanSettingsReader.hostKey) } @Test func `strict provider endpoint mode accepts known Coding Plan hosts`() { let env = [ AlibabaCodingPlanSettingsReader.requireProviderEndpointOverridesKey: "true", AlibabaCodingPlanSettingsReader.hostKey: "bailian-beijing-cs.aliyuncs.com", ] #expect( AlibabaCodingPlanSettingsReader.hostOverride(environment: env) == "bailian-beijing-cs.aliyuncs.com") #expect(AlibabaCodingPlanSettingsReader.rejectedEndpointOverrideKey(environment: env) == nil) } @Test func `custom https compatibility mode still rejects http and userinfo`() { #expect(AlibabaCodingPlanSettingsReader.hostOverride(environment: [ AlibabaCodingPlanSettingsReader.hostKey: "http://proxy.example.test", ]) == nil) #expect(AlibabaCodingPlanSettingsReader.rejectedEndpointOverrideKey(environment: [ AlibabaCodingPlanSettingsReader.quotaURLKey: "https://user:pass@proxy.example.test/data/api.json", ]) == AlibabaCodingPlanSettingsReader.quotaURLKey) } @Test func `missing cookie error includes access hint when present`() { let error = AlibabaCodingPlanSettingsError .missingCookie(details: "Safari cookie file exists but is not readable.") #expect(error.errorDescription?.contains("Safari cookie file exists but is not readable.") == true) } } struct AlibabaCodingPlanUsageSnapshotTests { @Test func `maps usage snapshot windows`() { let now = Date(timeIntervalSince1970: 1_700_000_000) let reset5h = Date(timeIntervalSince1970: 1_700_000_300) let resetWeek = Date(timeIntervalSince1970: 1_700_010_000) let resetMonth = Date(timeIntervalSince1970: 1_700_100_000) let snapshot = AlibabaCodingPlanUsageSnapshot( planName: "Pro", fiveHourUsedQuota: 20, fiveHourTotalQuota: 100, fiveHourNextRefreshTime: reset5h, weeklyUsedQuota: 120, weeklyTotalQuota: 400, weeklyNextRefreshTime: resetWeek, monthlyUsedQuota: 500, monthlyTotalQuota: 2000, monthlyNextRefreshTime: resetMonth, updatedAt: now) let usage = snapshot.toUsageSnapshot() #expect(usage.primary?.usedPercent == 20) #expect(usage.primary?.windowMinutes == 300) #expect(usage.secondary?.usedPercent == 30) #expect(usage.secondary?.windowMinutes == 10080) #expect(usage.tertiary?.usedPercent == 25) #expect(usage.tertiary?.windowMinutes == 43200) #expect(usage.loginMethod(for: .alibaba) == "Pro") } @Test func `shifts primary reset forward when backend reset is not future`() { let now = Date(timeIntervalSince1970: 1_700_000_000) let stalePrimaryReset = Date(timeIntervalSince1970: 1_699_999_900) let snapshot = AlibabaCodingPlanUsageSnapshot( planName: "Lite", fiveHourUsedQuota: 70, fiveHourTotalQuota: 1200, fiveHourNextRefreshTime: stalePrimaryReset, weeklyUsedQuota: 80, weeklyTotalQuota: 9000, weeklyNextRefreshTime: Date(timeIntervalSince1970: 1_700_010_000), monthlyUsedQuota: 80, monthlyTotalQuota: 18000, monthlyNextRefreshTime: Date(timeIntervalSince1970: 1_700_100_000), updatedAt: now) let usage = snapshot.toUsageSnapshot() #expect(usage.primary?.resetsAt == stalePrimaryReset.addingTimeInterval(TimeInterval(5 * 60 * 60))) } } struct AlibabaCodingPlanUsageParsingTests { @Test func `parses quota payload`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Alibaba Coding Plan Pro" } ], "codingPlanQuotaInfo": { "per5HourUsedQuota": 52, "per5HourTotalQuota": 1000, "per5HourQuotaNextRefreshTime": 1700000300000, "perWeekUsedQuota": 800, "perWeekTotalQuota": 5000, "perWeekQuotaNextRefreshTime": 1700100000000, "perBillMonthUsedQuota": 1200, "perBillMonthTotalQuota": 20000, "perBillMonthQuotaNextRefreshTime": 1701000000000 } }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Alibaba Coding Plan Pro") #expect(snapshot.fiveHourUsedQuota == 52) #expect(snapshot.fiveHourTotalQuota == 1000) #expect(snapshot.weeklyTotalQuota == 5000) #expect(snapshot.monthlyTotalQuota == 20000) #expect(snapshot.fiveHourNextRefreshTime == Date(timeIntervalSince1970: 1_700_000_300)) } @Test func `multi instance quota payload uses selected active instance plan name`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Expired Starter", "status": "EXPIRED", "endTime": "2025-04-01 17:00", "codingPlanQuotaInfo": { "per5HourUsedQuota": 7, "per5HourTotalQuota": 100, "per5HourQuotaNextRefreshTime": 1700000100000 } }, { "planName": "Active Pro", "status": "VALID", "codingPlanQuotaInfo": { "per5HourUsedQuota": 52, "per5HourTotalQuota": 1000, "per5HourQuotaNextRefreshTime": 1700000300000 } } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Active Pro") #expect(snapshot.fiveHourUsedQuota == 52) #expect(snapshot.fiveHourTotalQuota == 1000) #expect(snapshot.fiveHourNextRefreshTime == Date(timeIntervalSince1970: 1_700_000_300)) } @Test func `missing quota data without positive active signal fails`() { let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Alibaba Coding Plan Pro" } ] }, "status_code": 0 } """ #expect(throws: AlibabaCodingPlanUsageError.self) { try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8)) } } @Test func `plan usage without positive active proof fails`() { let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Alibaba Coding Plan Pro", "planUsage": "18%" } ] }, "status_code": 0 } """ #expect(throws: AlibabaCodingPlanUsageError.self) { try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8)) } } @Test func `parses wrapped JSON string payload`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let inner = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Coding Plan Lite", "status": "VALID", "codingPlanQuotaInfo": { "per5HourUsedQuota": 0, "per5HourTotalQuota": 1000, "per5HourQuotaNextRefreshTime": 1700000300000 } } ] }, "statusCode": 200 } """ .replacingOccurrences(of: "\n", with: "") .replacingOccurrences(of: " ", with: "") .replacingOccurrences(of: "\"", with: "\\\"") let wrapped = """ { "successResponse": { "body": "\(inner)" } } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(wrapped.utf8), now: now) #expect(snapshot.planName == "Coding Plan Lite") #expect(snapshot.fiveHourTotalQuota == 1000) #expect(snapshot.fiveHourUsedQuota == 0) } @Test func `plan usage fallback stays visible but non quantitative`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Coding Plan Lite", "status": "VALID", "planUsage": "0%", "endTime": "2026-04-01 17:00" } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Coding Plan Lite") #expect(snapshot.fiveHourUsedQuota == nil) #expect(snapshot.fiveHourTotalQuota == nil) #expect(snapshot.fiveHourNextRefreshTime == nil) let usage = snapshot.toUsageSnapshot() #expect(usage.primary == nil) #expect(usage.loginMethod(for: .alibaba) == "Coding Plan Lite") } @Test func `falls back to active plan when quota and usage missing`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Coding Plan Lite", "status": "VALID", "endTime": "2026-04-01 17:00" } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Coding Plan Lite") #expect(snapshot.fiveHourUsedQuota == nil) #expect(snapshot.fiveHourTotalQuota == nil) let usage = snapshot.toUsageSnapshot() #expect(usage.primary == nil) #expect(usage.loginMethod(for: .alibaba) == "Coding Plan Lite") } @Test func `future end time counts as positive active signal`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Coding Plan Lite", "endTime": "2030-04-01 17:00" } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Coding Plan Lite") #expect(snapshot.fiveHourUsedQuota == nil) #expect(snapshot.weeklyTotalQuota == nil) let usage = snapshot.toUsageSnapshot() #expect(usage.primary == nil) #expect(usage.loginMethod(for: .alibaba) == "Coding Plan Lite") } @Test func `multi instance fallback uses selected active instance plan name`() throws { let now = Date(timeIntervalSince1970: 1_700_000_000) let json = """ { "data": { "codingPlanInstanceInfos": [ { "planName": "Expired Starter", "status": "EXPIRED", "endTime": "2025-04-01 17:00" }, { "planName": "Active Pro", "status": "VALID", "planUsage": "42%" } ] }, "status_code": 0 } """ let snapshot = try AlibabaCodingPlanUsageFetcher.parseUsageSnapshot(from: Data(json.utf8), now: now) #expect(snapshot.planName == "Active Pro") #expect(snapshot.fiveHourUsedQuota == nil) #expect(snapshot.fiveHourTotalQuota == nil) let usage = snapshot.toUsageSnapshot() #expect(usage.primary == nil) #expect(usage.loginMethod(for: .alibaba) == "Active Pro") }