Ariver
2026-08-22 1ec08b1dedff313e385767edb0c2b815b6dd56f3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env bash
set -euo pipefail
 
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
DATA_LAYER="$ROOT_DIR/Apptag/DataLayer.swift"
QUICK_SEARCH="$ROOT_DIR/Apptag/QuickSearch.swift"
SUNLOGIN_APP="${1:-/Applications/贝锐向日葵被控.app}"
MODULE_CACHE_DIR="${MODULE_CACHE_DIR:-${TMPDIR:-/tmp}/taglauncher-quick-search-qa-cache}"
mkdir -p "$MODULE_CACHE_DIR"
 
rg -Fq 'AppDisplayNameResolver.displayName' "$DATA_LAYER"
rg -Fq 'systemDisplayNames' "$DATA_LAYER"
rg -Fq 'bundleDisplayNames' "$DATA_LAYER"
rg -Fq '.localizedNameKey' "$DATA_LAYER"
rg -Fq 'isNestedInsideAppBundle' "$DATA_LAYER"
rg -Fq 'isInternalHelperAppPath' "$DATA_LAYER"
rg -Fq '.skipsPackageDescendants' "$DATA_LAYER"
rg -Fq 'AppDisplayNameResolver.searchAliases' "$QUICK_SEARCH"
rg -Fq 'AppIndexer.isInternalHelperAppPath($0.path)' "$QUICK_SEARCH"
 
swift -module-cache-path "$MODULE_CACHE_DIR" - "$SUNLOGIN_APP" <<'SWIFT'
import Foundation
import CoreServices
 
let appPath = CommandLine.arguments[1]
let appURL = URL(fileURLWithPath: appPath)
let expectedDisplayName = "贝锐向日葵被控"
let nestedURL = appURL
    .appendingPathComponent("Wrapper")
    .appendingPathComponent("isunclient.app")
 
func fail(_ message: String) -> Never {
    FileHandle.standardError.write(Data("FAIL: \(message)\n".utf8))
    exit(1)
}
 
func trim(_ value: String?) -> String? {
    guard let value else { return nil }
    let trimmed = value
        .replacingOccurrences(of: ".app", with: "")
        .trimmingCharacters(in: .whitespacesAndNewlines)
    return trimmed.isEmpty ? nil : trimmed
}
 
func normalized(_ value: String) -> String {
    value
        .folding(options: [.caseInsensitive, .diacriticInsensitive], locale: .current)
        .lowercased()
        .trimmingCharacters(in: .whitespacesAndNewlines)
}
 
func uniqueLocalizedNames(_ values: [String?], excluding excludedValue: String) -> [String] {
    let excluded = normalized(excludedValue)
    var seen = Set<String>()
    var result: [String] = []
    for value in values {
        guard let trimmed = trim(value) else { continue }
        let key = normalized(trimmed)
        guard key != excluded, seen.insert(key).inserted else { continue }
        result.append(trimmed)
    }
    return result
}
 
func uniqueDisplayNames(_ values: [String], excluding excludedValue: String) -> [String] {
    let excluded = normalized(excludedValue)
    var seen = Set<String>()
    var result: [String] = []
    for value in values {
        guard let trimmed = trim(value) else { continue }
        let key = normalized(trimmed)
        guard key != excluded, seen.insert(key).inserted else { continue }
        result.append(trimmed)
    }
    return result
}
 
struct NameProfile {
    let name: String
    let localizedNames: [String]
    let localizedNamesByLanguage: [String: String]
    let systemDisplayNames: [String]
    let bundleDisplayNames: [String]
}
 
let supportedLanguages = [
    "en", "fr", "it", "de", "es", "pt-BR", "zh-Hans", "zh-Hant", "ko", "ja",
    "ru", "sr-Cyrl", "uk", "th", "vi", "ar", "ar-Najdi", "tr", "id", "cs",
    "da", "nl", "no", "nn", "nb", "ms", "pl", "ro", "sv"
]
 
func uniqueLanguageCodes(_ codes: [String]) -> [String] {
    var seen = Set<String>()
    return codes.filter { seen.insert($0).inserted }
}
 
func displayLanguageFallbacks(for languageCode: String, includeEnglish: Bool = false) -> [String] {
    var candidates = [
        languageCode,
        languageCode.replacingOccurrences(of: "_", with: "-")
    ]
 
    switch languageCode {
    case "zh-Hans":
        candidates.append("zh-Hant")
    case "zh-Hant":
        candidates.append("zh-Hans")
    case "pt-BR":
        candidates.append("pt")
    case "sr-Cyrl":
        candidates.append("sr")
    case "ar-Najdi":
        candidates.append("ar")
    case "nb":
        candidates.append(contentsOf: ["no", "nn"])
    case "nn":
        candidates.append(contentsOf: ["no", "nb"])
    case "no":
        candidates.append(contentsOf: ["nb", "nn"])
    default:
        if let base = languageCode.split(separator: "-").first.map(String.init) {
            candidates.append(base)
        }
    }
 
    if includeEnglish {
        candidates.append("en")
    }
    return uniqueLanguageCodes(candidates)
}
 
func firstDisplayCandidate(_ values: [String]) -> String? {
    values.lazy.compactMap(trim).first
}
 
func firstLocalizedName(for languageCodes: [String], in localizedNamesByLanguage: [String: String]) -> String? {
    firstDisplayCandidate(languageCodes.compactMap { localizedNamesByLanguage[$0] })
}
 
func displayName(_ profile: NameProfile, languageCode: String) -> String {
    let languageFallbacks = displayLanguageFallbacks(for: languageCode)
 
    if let localizedName = firstLocalizedName(
        for: languageFallbacks,
        in: profile.localizedNamesByLanguage
    ) {
        return localizedName
    }
 
    if languageCode == "en",
       let englishBaseName = firstDisplayCandidate([profile.name] + profile.bundleDisplayNames) {
        return englishBaseName
    }
 
    if let systemDisplayName = firstDisplayCandidate(profile.systemDisplayNames) {
        return systemDisplayName
    }
 
    if let englishName = firstLocalizedName(for: ["en"], in: profile.localizedNamesByLanguage) {
        return englishName
    }
 
    if let bundleDisplayName = firstDisplayCandidate(profile.bundleDisplayNames) {
        return bundleDisplayName
    }
 
    let skippedLanguageCodes = Set(languageFallbacks + ["en"])
    let remainingLocalizedNames = supportedLanguages.compactMap { languageCode -> String? in
        guard !skippedLanguageCodes.contains(languageCode) else { return nil }
        return profile.localizedNamesByLanguage[languageCode]
    }
    if let localizedName = firstDisplayCandidate(remainingLocalizedNames) {
        return localizedName
    }
 
    if let alias = firstDisplayCandidate(profile.localizedNames) {
        return alias
    }
 
    return profile.name
}
 
func searchAliases(_ profile: NameProfile) -> [String] {
    uniqueDisplayNames(
        supportedLanguages.compactMap { profile.localizedNamesByLanguage[$0] }
            + profile.systemDisplayNames
            + profile.bundleDisplayNames
            + profile.localizedNames
            + [displayName(profile, languageCode: "zh-Hans")],
        excluding: profile.name
    )
}
 
func isNestedInsideAppBundle(_ url: URL) -> Bool {
    isNestedInsideAppBundlePath(url.standardizedFileURL.path)
}
 
func isInternalHelperAppPath(_ url: URL) -> Bool {
    let path = url.standardizedFileURL.path
    let lowercasedPath = path.lowercased()
    return isNestedInsideAppBundlePath(path)
        || lowercasedPath.contains("/contents/helpers/")
        || lowercasedPath.contains("/contents/xpcservices/")
        || lowercasedPath.contains("/wrapper/")
}
 
func isNestedInsideAppBundlePath(_ path: String) -> Bool {
    let lowercasedPath = path.lowercased()
    guard lowercasedPath.hasSuffix(".app") else { return false }
    let components = lowercasedPath.split(separator: "/", omittingEmptySubsequences: true)
    guard let lastAppIndex = components.lastIndex(where: { $0.hasSuffix(".app") }) else {
        return false
    }
    if components[..<lastAppIndex].contains(where: { $0.hasSuffix(".app") }) {
        return true
    }
    return lowercasedPath.range(of: ".app/", options: .caseInsensitive) != nil
}
 
func spotlightDisplayName(for url: URL) -> String? {
    guard let item = MDItemCreate(nil, url.path as CFString),
          let value = MDItemCopyAttribute(item, kMDItemDisplayName) as? String
    else { return nil }
    return trim(value)
}
 
func resourceLocalizedName(for url: URL) -> String? {
    let values = try? url.resourceValues(forKeys: [.localizedNameKey])
    return trim(values?.localizedName)
}
 
func bundleDisplayName(for url: URL) -> String? {
    guard let bundle = Bundle(url: url) else { return nil }
    return trim(bundle.infoDictionary?["CFBundleDisplayName"] as? String)
}
 
func makeDocumentPaths(appPaths: [URL]) -> [String] {
    appPaths
        .filter { !isInternalHelperAppPath($0) }
        .map(\.path)
}
 
let multilingualProfile = NameProfile(
    name: "InternalRunner",
    localizedNames: [],
    localizedNamesByLanguage: [
        "en": "English Name",
        "de": "Deutscher Name",
        "ja": "日本語名",
        "zh-Hans": "简体名"
    ],
    systemDisplayNames: ["System Visible"],
    bundleDisplayNames: ["Bundle Visible"]
)
guard displayName(multilingualProfile, languageCode: "de") == "Deutscher Name" else {
    fail("German localized display name was not preferred")
}
guard displayName(multilingualProfile, languageCode: "ja") == "日本語名" else {
    fail("Japanese localized display name was not preferred")
}
guard displayName(multilingualProfile, languageCode: "zh-Hant") == "简体名" else {
    fail("Chinese language fallback did not use paired Chinese localization")
}
 
let systemFallbackProfile = NameProfile(
    name: "internal-helper",
    localizedNames: [],
    localizedNamesByLanguage: [:],
    systemDisplayNames: ["Nom système"],
    bundleDisplayNames: ["Bundle Visible"]
)
guard displayName(systemFallbackProfile, languageCode: "fr") == "Nom système" else {
    fail("system/Finder display name was not used before bundle/internal names")
}
 
let englishBaseProfile = NameProfile(
    name: "AweSun",
    localizedNames: [],
    localizedNamesByLanguage: [:],
    systemDisplayNames: ["贝锐向日葵"],
    bundleDisplayNames: []
)
guard displayName(englishBaseProfile, languageCode: "en") == "AweSun" else {
    fail("English users should keep a presentable English base app name")
}
guard displayName(englishBaseProfile, languageCode: "zh-Hans") == "贝锐向日葵" else {
    fail("non-English users should still get system-localized display fallback")
}
 
let englishFallbackProfile = NameProfile(
    name: "inner-name",
    localizedNames: [],
    localizedNamesByLanguage: ["en": "Official English"],
    systemDisplayNames: [],
    bundleDisplayNames: []
)
guard displayName(englishFallbackProfile, languageCode: "sv") == "Official English" else {
    fail("official English display name was not used as cross-language fallback")
}
 
guard FileManager.default.fileExists(atPath: appPath) else {
    print("SKIP Sunlogin wrapper app not installed at \(appPath)")
    exit(0)
}
 
guard appURL.deletingPathExtension().lastPathComponent == expectedDisplayName else {
    fail("outer wrapper file name is not the localized app name: \(appURL.lastPathComponent)")
}
 
if FileManager.default.fileExists(atPath: nestedURL.path) {
    guard isNestedInsideAppBundle(nestedURL) else {
        fail("nested helper app was not classified as nested: \(nestedURL.path)")
    }
    guard isInternalHelperAppPath(nestedURL) else {
        fail("nested helper app was not classified as internal helper: \(nestedURL.path)")
    }
    guard bundleDisplayName(for: nestedURL) == expectedDisplayName else {
        fail("nested helper bundle display name did not expose localized name")
    }
}
 
let helperPathFixtures = [
    "/Applications/Outer.app/Contents/Helpers/Inner.app",
    "/Applications/Outer.app/Contents/XPCServices/Inner.app",
    "/Applications/Outer.app/Wrapper/Inner.app"
]
for helperPath in helperPathFixtures {
    guard isInternalHelperAppPath(URL(fileURLWithPath: helperPath)) else {
        fail("helper path fixture was not filtered: \(helperPath)")
    }
}
 
var enumeratedHits: [String] = []
if let enumerator = FileManager.default.enumerator(
    at: URL(fileURLWithPath: "/Applications"),
    includingPropertiesForKeys: [.isDirectoryKey, .isSymbolicLinkKey],
    options: [.skipsHiddenFiles, .skipsPackageDescendants]
) {
    for case let url as URL in enumerator {
        if url.path.contains(expectedDisplayName) || url.path.contains("isunclient") {
            enumeratedHits.append(url.path)
        }
    }
}
 
guard enumeratedHits.contains(appPath) else {
    fail("outer wrapper app was not visible to the scanner")
}
guard !enumeratedHits.contains(nestedURL.path) else {
    fail("scanner enumerated nested helper app: \(nestedURL.path)")
}
 
let outerNames = uniqueLocalizedNames(
    [
        resourceLocalizedName(for: appURL),
        spotlightDisplayName(for: appURL),
        FileManager.default.displayName(atPath: appURL.path)
    ],
    excluding: appURL.deletingPathExtension().lastPathComponent
)
guard appURL.deletingPathExtension().lastPathComponent == expectedDisplayName ||
      outerNames.contains(expectedDisplayName) else {
    fail("outer wrapper localized display name was not discoverable")
}
 
let helperLocalizedNames = uniqueLocalizedNames(
    [
        bundleDisplayName(for: nestedURL),
        resourceLocalizedName(for: nestedURL),
        spotlightDisplayName(for: nestedURL),
        FileManager.default.displayName(atPath: nestedURL.path)
    ],
    excluding: "isunclient"
)
let helperProfile = NameProfile(
    name: "isunclient",
    localizedNames: helperLocalizedNames,
    localizedNamesByLanguage: [:],
    systemDisplayNames: helperLocalizedNames,
    bundleDisplayNames: []
)
let helperDisplayName = displayName(helperProfile, languageCode: "zh-Hans")
guard helperDisplayName == expectedDisplayName else {
    fail("displayName fallback returned \(helperDisplayName) instead of \(expectedDisplayName)")
}
 
let documentPaths = makeDocumentPaths(appPaths: [appURL, nestedURL])
guard documentPaths.contains(appPath) else {
    fail("Quick Search document fixture dropped outer wrapper app")
}
guard !documentPaths.contains(nestedURL.path) else {
    fail("Quick Search document fixture included nested helper app: \(nestedURL.path)")
}
guard searchAliases(helperProfile).contains(expectedDisplayName) else {
    fail("display aliases did not retain localized helper name for search")
}
 
print("PASS quick search app-name QA: \(expectedDisplayName) wrapper wins over nested isunclient")
SWIFT