remark_vars_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package sub
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/mhsanaei/3x-ui/v3/internal/database/model"
  6. "github.com/mhsanaei/3x-ui/v3/internal/xray"
  7. )
  8. const gb = int64(1024 * 1024 * 1024)
  9. // expandCtx builds a remarkContext from explicit pieces for token tests.
  10. func expandCtx(client model.Client, stats xray.ClientTraffic, inbound *model.Inbound) remarkContext {
  11. return remarkContext{client: client, stats: stats, inbound: inbound}
  12. }
  13. func TestExpandRemarkVars(t *testing.T) {
  14. inbound := &model.Inbound{Remark: "Germany"}
  15. client := model.Client{
  16. Email: "[email protected]",
  17. ID: "3f2a9c1b-aaaa-bbbb-cccc-1234567890ab",
  18. TgID: 123456789,
  19. SubID: "subABC",
  20. Comment: "vip",
  21. Reset: 30,
  22. CreatedAt: 1_700_000_000_000,
  23. }
  24. // 50GB total, 8GB used (5 up + 3 down), enabled, no expiry.
  25. stats := xray.ClientTraffic{
  26. Enable: true,
  27. Total: 50 * gb,
  28. Up: 5 * gb,
  29. Down: 3 * gb,
  30. }
  31. ctx := expandCtx(client, stats, inbound)
  32. cases := []struct{ tmpl, want string }{
  33. {"{{EMAIL}}", "[email protected]"},
  34. {"{{INBOUND}}", "Germany"}, // no host remark in ctx → inbound remark
  35. {"{{HOST}}", ""}, // no host remark in ctx → empty
  36. {"{{ID}}", client.ID},
  37. {"{{SHORT_ID}}", "3f2a9c1b"},
  38. {"{{TELEGRAM_ID}}", "123456789"},
  39. {"{{SUB_ID}}", "subABC"},
  40. {"{{COMMENT}}", "vip"},
  41. {"{{RESET_DAYS}}", "30"},
  42. {"{{CREATED_UNIX}}", "1700000000"},
  43. {"{{TRAFFIC_USED}}", "8.00GB"},
  44. {"{{TRAFFIC_LEFT}}", "42.00GB"},
  45. {"{{TRAFFIC_TOTAL}}", "50.00GB"},
  46. {"{{TRAFFIC_USED_BYTES}}", "8589934592"},
  47. {"{{TRAFFIC_TOTAL_BYTES}}", "53687091200"},
  48. {"{{UP}}", "5.00GB"},
  49. {"{{DOWN}}", "3.00GB"},
  50. {"{{STATUS}}", "active"},
  51. {"{{EXPIRE_UNIX}}", "0"}, // no expiry
  52. {"{{EXPIRE_DATE}}", ""}, // no fixed date
  53. {"{{UNKNOWN_TOKEN}}", ""}, // unknown → empty, never literal
  54. {"DE {{EMAIL}} ok", "DE [email protected] ok"},
  55. {"{{EMAIL}}-{{SHORT_ID}}", "[email protected]"},
  56. {"no tokens here", "no tokens here"},
  57. }
  58. for _, c := range cases {
  59. if got := expandRemarkVars(c.tmpl, ctx); got != c.want {
  60. t.Errorf("expandRemarkVars(%q) = %q, want %q", c.tmpl, got, c.want)
  61. }
  62. }
  63. // The unlimited tokens still render ∞ at the value layer; expandRemarkVars
  64. // is what drops an all-unlimited segment (see TestExpandRemarkVars_DropUnlimitedSegments).
  65. if got := remarkVarValue("DAYS_LEFT", ctx); got != "∞" {
  66. t.Errorf("remarkVarValue(DAYS_LEFT) = %q, want ∞", got)
  67. }
  68. }
  69. func TestExpandRemarkVars_EdgeCases(t *testing.T) {
  70. // Unlimited total → ∞ for human forms, 0 bytes for *_BYTES left. Checked at
  71. // the value layer: expandRemarkVars would drop a bare ∞ segment.
  72. unlimited := expandCtx(model.Client{}, xray.ClientTraffic{Enable: true, Total: 0, Up: gb}, nil)
  73. if got := remarkVarValue("TRAFFIC_TOTAL", unlimited); got != "∞" {
  74. t.Errorf("unlimited TRAFFIC_TOTAL = %q, want ∞", got)
  75. }
  76. if got := remarkVarValue("TRAFFIC_LEFT", unlimited); got != "∞" {
  77. t.Errorf("unlimited TRAFFIC_LEFT = %q, want ∞", got)
  78. }
  79. if got := expandRemarkVars("{{TRAFFIC_LEFT_BYTES}}", unlimited); got != "0" {
  80. t.Errorf("unlimited TRAFFIC_LEFT_BYTES = %q, want 0", got)
  81. }
  82. // TgID zero → empty.
  83. if got := expandRemarkVars("{{TELEGRAM_ID}}", unlimited); got != "" {
  84. t.Errorf("zero TgID = %q, want empty", got)
  85. }
  86. // Over-quota usage clamps left to 0, not negative.
  87. over := expandCtx(model.Client{}, xray.ClientTraffic{Enable: true, Total: gb, Up: 2 * gb}, nil)
  88. if got := expandRemarkVars("{{TRAFFIC_LEFT_BYTES}}", over); got != "0" {
  89. t.Errorf("over-quota TRAFFIC_LEFT_BYTES = %q, want 0", got)
  90. }
  91. // Delayed-start (negative expiry) gives deterministic whole days.
  92. delayed := expandCtx(model.Client{}, xray.ClientTraffic{Enable: true, ExpiryTime: -864_000_000}, nil)
  93. if got := expandRemarkVars("{{DAYS_LEFT}}", delayed); got != "10" {
  94. t.Errorf("delayed-start DAYS_LEFT = %q, want 10", got)
  95. }
  96. }
  97. // An unlimited client drops the quota/expiry segments whole — decoration and the
  98. // "|" separator included — instead of printing "📊∞|⏳∞D".
  99. func TestExpandRemarkVars_DropUnlimitedSegments(t *testing.T) {
  100. const tmpl = "{{INBOUND}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D"
  101. inbound := &model.Inbound{Remark: "host"}
  102. // No limit at all → only the name segment survives.
  103. unlimited := expandCtx(model.Client{}, xray.ClientTraffic{Enable: true}, inbound)
  104. if got := expandRemarkVars(tmpl, unlimited); got != "host" {
  105. t.Errorf("fully unlimited = %q, want %q", got, "host")
  106. }
  107. // Limited traffic but no expiry → traffic stays, the expiry segment drops.
  108. noExpiry := expandCtx(model.Client{}, xray.ClientTraffic{Enable: true, Total: 50 * gb, Up: 8 * gb}, inbound)
  109. if got := expandRemarkVars(tmpl, noExpiry); got != "host|📊42.00GB" {
  110. t.Errorf("no-expiry = %q, want %q", got, "host|📊42.00GB")
  111. }
  112. // A segment mixing an unlimited token with another value is kept whole,
  113. // decoration and ∞ included — only all-unlimited segments drop.
  114. mixed := expandCtx(model.Client{Email: "john"}, xray.ClientTraffic{Enable: true}, inbound)
  115. if got := expandRemarkVars("{{EMAIL}} 📊{{TRAFFIC_LEFT}}", mixed); got != "john 📊∞" {
  116. t.Errorf("mixed segment = %q, want %q", got, "john 📊∞")
  117. }
  118. }
  119. func TestClientStatus(t *testing.T) {
  120. cases := []struct {
  121. name string
  122. st xray.ClientTraffic
  123. want string
  124. }{
  125. {"disabled", xray.ClientTraffic{Enable: false}, "disabled"},
  126. {"active", xray.ClientTraffic{Enable: true}, "active"},
  127. {"expired", xray.ClientTraffic{Enable: true, ExpiryTime: 1000}, "expired"}, // 1s past epoch
  128. {"depleted", xray.ClientTraffic{Enable: true, Total: gb, Up: gb}, "depleted"},
  129. }
  130. for _, c := range cases {
  131. if got := clientStatus(c.st); got != c.want {
  132. t.Errorf("%s: clientStatus = %q, want %q", c.name, got, c.want)
  133. }
  134. }
  135. }
  136. // hostRemarkService builds a SubService + inbound + client/stats for remark tests.
  137. func hostRemarkService(template string) (*SubService, *model.Inbound, model.Client) {
  138. s := &SubService{remarkTemplate: template, subscriptionBody: true}
  139. inbound := &model.Inbound{
  140. Remark: "DE",
  141. ClientStats: []xray.ClientTraffic{{
  142. Email: "[email protected]",
  143. Enable: true,
  144. Total: 100 * gb,
  145. Up: 15 * gb,
  146. Down: 5 * gb,
  147. ExpiryTime: -864_000_000, // delayed-start: deterministic 10 days
  148. }},
  149. }
  150. client := model.Client{Email: "[email protected]"}
  151. return s, inbound, client
  152. }
  153. // With no template configured, genHostRemark falls back to the inbound remark,
  154. // host and email joined by "-".
  155. func TestGenHostRemark_NoTemplate_Fallback(t *testing.T) {
  156. s, inbound, client := hostRemarkService("")
  157. if got := s.genHostRemark(inbound, client, "Relay", ""); got != "[email protected]" {
  158. t.Fatalf("genHostRemark = %q, want %q", got, "[email protected]")
  159. }
  160. if got := s.genHostRemark(inbound, client, "", ""); got != "[email protected]" {
  161. t.Fatalf("genHostRemark (no host remark) = %q, want %q", got, "[email protected]")
  162. }
  163. }
  164. // In the body the template applies: {{INBOUND}} is always the inbound's remark
  165. // and {{HOST}} the host's own remark, so the two can be shown side by side.
  166. func TestGenHostRemark_GlobalTemplate(t *testing.T) {
  167. // {{INBOUND}} resolves to the inbound remark regardless of the host remark.
  168. s, inbound, client := hostRemarkService("{{INBOUND}} | {{TRAFFIC_LEFT}} | {{DAYS_LEFT}}d")
  169. if got := s.genHostRemark(inbound, client, "CDN", ""); got != "DE | 80.00GB | 10d" {
  170. t.Fatalf("global template ({{INBOUND}} = inbound) = %q", got)
  171. }
  172. // {{INBOUND}} and {{HOST}} side by side show both, distinctly (#5443).
  173. s2, inbound2, client2 := hostRemarkService("{{INBOUND}}|{{HOST}}|{{TRAFFIC_LEFT}}")
  174. if got := s2.genHostRemark(inbound2, client2, "CDN", ""); got != "DE|CDN|80.00GB" {
  175. t.Fatalf("global template (inbound + host) = %q, want %q", got, "DE|CDN|80.00GB")
  176. }
  177. // {{HOST}} is the host's own remark even when the inbound has one of its own.
  178. s3, inbound3, client3 := hostRemarkService("{{HOST}}")
  179. if got := s3.genHostRemark(inbound3, client3, "CDN", ""); got != "CDN" {
  180. t.Fatalf("{{HOST}} token = %q, want CDN", got)
  181. }
  182. }
  183. // A global template also drives non-host links via genRemark; {{HOST}} = the
  184. // legacy externalProxy remark passed as extra.
  185. func TestGenRemark_GlobalTemplate(t *testing.T) {
  186. s, inbound, _ := hostRemarkService("{{EMAIL}} | {{TRAFFIC_LEFT}}")
  187. got := s.genRemark(inbound, "[email protected]", "", "")
  188. if got != "[email protected] | 80.00GB" {
  189. t.Fatalf("global template (non-host) = %q", got)
  190. }
  191. }
  192. func TestGenRemark_NoTemplate_AppendsEmail(t *testing.T) {
  193. s, inbound, _ := hostRemarkService("")
  194. got := s.genRemark(inbound, "[email protected]", "Relay", "")
  195. if got != "[email protected]" {
  196. t.Fatalf("genRemark = %q, want %q", got, "[email protected]")
  197. }
  198. }
  199. // The per-client info part of the template renders only on a client's first
  200. // link of the request; later links show the name-only template.
  201. func TestUsageOnFirstLinkOnly(t *testing.T) {
  202. s, inbound, client := hostRemarkService("{{INBOUND}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D")
  203. first := s.genHostRemark(inbound, client, "", "")
  204. second := s.genHostRemark(inbound, client, "", "")
  205. if !strings.Contains(first, "📊") || !strings.Contains(first, "80.00GB") {
  206. t.Fatalf("first link should carry usage: %q", first)
  207. }
  208. if strings.ContainsAny(second, "📊⏳") {
  209. t.Fatalf("second link must not carry usage: %q", second)
  210. }
  211. if second != "DE" {
  212. t.Fatalf("second link = %q, want name-only %q", second, "DE")
  213. }
  214. }
  215. func TestRemarkInDisplayContext(t *testing.T) {
  216. s, inbound, client := hostRemarkService("{{INBOUND}}-{{EMAIL}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D")
  217. s.subscriptionBody = false
  218. const want = "[email protected]"
  219. if got := s.genHostRemark(inbound, client, "CDN", ""); got != want {
  220. t.Fatalf("display host link = %q, want %q", got, want)
  221. }
  222. if got := s.genHostRemark(inbound, client, "", ""); got != want {
  223. t.Fatalf("display host link (no host) = %q, want %q", got, want)
  224. }
  225. if got := s.genRemark(inbound, client.Email, "", ""); got != want {
  226. t.Fatalf("display genRemark = %q, want %q", got, want)
  227. }
  228. s2, inbound2, client2 := hostRemarkService("{{INBOUND}}-{{HOST}}|📊{{TRAFFIC_LEFT}}")
  229. s2.subscriptionBody = false
  230. if got := s2.genHostRemark(inbound2, client2, "CDN", ""); got != "DE-CDN" {
  231. t.Fatalf("display host link with HOST token = %q, want %q", got, "DE-CDN")
  232. }
  233. }
  234. // nameOnlyTemplate drops the info part (and its leading decoration), keeping name.
  235. func TestNameOnlyTemplate(t *testing.T) {
  236. cases := map[string]string{
  237. "{{INBOUND}}|📊{{TRAFFIC_LEFT}}|⏳{{DAYS_LEFT}}D": "{{INBOUND}}", // usage tail stripped
  238. "{{EMAIL}} {{INBOUND}} ⏳{{DAYS_LEFT}}": "{{EMAIL}} {{INBOUND}}", // multi-token name survives the trim
  239. "{{INBOUND}} | {{STATUS}}": "{{INBOUND}}",
  240. "{{INBOUND}}-{{EMAIL}}": "{{INBOUND}}-{{EMAIL}}", // no info tokens → unchanged
  241. "{{TRAFFIC_LEFT}}": "", // info only → empty
  242. }
  243. for tmpl, want := range cases {
  244. if got := nameOnlyTemplate(tmpl); got != want {
  245. t.Errorf("nameOnlyTemplate(%q) = %q, want %q", tmpl, got, want)
  246. }
  247. }
  248. }
  249. // statsForClient resolves usage from the per-request statsByEmail map when the
  250. // link's own inbound doesn't carry the client's (globally unique) traffic row —
  251. // the multi-inbound case that made {{TRAFFIC_LEFT}} show the full quota (#5443).
  252. func TestStatsForClient_CrossInboundFallback(t *testing.T) {
  253. s := &SubService{
  254. statsByEmail: map[string]xray.ClientTraffic{
  255. "[email protected]": {Email: "[email protected]", Total: 100 * gb, Up: 15 * gb, Down: 5 * gb},
  256. },
  257. }
  258. // Inbound B carries no ClientStats for john (his row is owned by inbound A).
  259. inboundB := &model.Inbound{Remark: "B"}
  260. st := s.statsForClient(inboundB, model.Client{Email: "[email protected]"})
  261. if used := st.Up + st.Down; used != 20*gb {
  262. t.Fatalf("statsForClient used = %d, want %d (cross-inbound fallback)", used, 20*gb)
  263. }
  264. if got := remarkVarValue("TRAFFIC_LEFT", remarkContext{stats: st}); got != "80.00GB" {
  265. t.Fatalf("TRAFFIC_LEFT = %q, want 80.00GB (remaining, not total)", got)
  266. }
  267. }
  268. // Two clients through the same global template get distinct, per-client remarks.
  269. func TestGenHostRemark_PerClient(t *testing.T) {
  270. s := &SubService{remarkTemplate: "{{EMAIL}}", subscriptionBody: true}
  271. inbound := &model.Inbound{}
  272. a := s.genHostRemark(inbound, model.Client{Email: "alice@x"}, "", "")
  273. b := s.genHostRemark(inbound, model.Client{Email: "bob@x"}, "", "")
  274. if a != "alice@x" || b != "bob@x" {
  275. t.Fatalf("per-client expansion failed: a=%q b=%q", a, b)
  276. }
  277. }
  278. func TestStatusEmoji(t *testing.T) {
  279. cases := []struct {
  280. stats xray.ClientTraffic
  281. want string
  282. }{
  283. {xray.ClientTraffic{Enable: true, Total: 10 * gb, Up: gb}, "✅"},
  284. {xray.ClientTraffic{Enable: true, Total: 10 * gb, Up: 10 * gb, Down: 1}, "🚫"},
  285. {xray.ClientTraffic{Enable: false}, "🚫"},
  286. {xray.ClientTraffic{Enable: true, ExpiryTime: 1000}, "⏳"},
  287. }
  288. for _, c := range cases {
  289. if got := statusEmoji(c.stats); got != c.want {
  290. t.Errorf("statusEmoji(%+v) = %q, want %q", c.stats, got, c.want)
  291. }
  292. }
  293. }
  294. func TestUsagePercentage(t *testing.T) {
  295. if got := usagePercentage(xray.ClientTraffic{Total: 100 * gb, Up: 25 * gb, Down: 25 * gb}); got != "50.0%" {
  296. t.Errorf("usagePercentage 50%% = %q", got)
  297. }
  298. if got := usagePercentage(xray.ClientTraffic{Total: 0}); got != "" {
  299. t.Errorf("usagePercentage unlimited = %q, want empty", got)
  300. }
  301. if got := usagePercentage(xray.ClientTraffic{Total: 10 * gb, Up: 10 * gb}); got != "100.0%" {
  302. t.Errorf("usagePercentage 100%% = %q", got)
  303. }
  304. // Over-quota usage clamps to 100%, consistent with TRAFFIC_LEFT.
  305. if got := usagePercentage(xray.ClientTraffic{Total: 10 * gb, Up: 25 * gb}); got != "100.0%" {
  306. t.Errorf("usagePercentage over-quota = %q, want 100.0%%", got)
  307. }
  308. }
  309. func TestTimeLeftLabel(t *testing.T) {
  310. if got := timeLeftLabel(0); got != "∞" {
  311. t.Errorf("timeLeftLabel(0) = %q, want ∞", got)
  312. }
  313. // Delayed-start: negative expiry = duration in ms. 1000ms = 1 second = "0m".
  314. if got := timeLeftLabel(-1000); got != "0m" {
  315. t.Errorf("timeLeftLabel(-1000) = %q, want 0m", got)
  316. }
  317. }
  318. func TestGregorianToJalali(t *testing.T) {
  319. cases := []struct {
  320. gy, gm, gd int
  321. jy, jm, jd int
  322. }{
  323. {2024, 1, 1, 1402, 10, 11},
  324. {2000, 3, 20, 1379, 1, 1},
  325. {1979, 2, 11, 1357, 11, 22},
  326. }
  327. for _, c := range cases {
  328. jy, jm, jd := gregorianToJalali(c.gy, c.gm, c.gd)
  329. if jy != c.jy || jm != c.jm || jd != c.jd {
  330. t.Errorf("gregorianToJalali(%d,%d,%d) = (%d,%d,%d), want (%d,%d,%d)",
  331. c.gy, c.gm, c.gd, jy, jm, jd, c.jy, c.jm, c.jd)
  332. }
  333. }
  334. }
  335. func TestJalaliExpireDateLabel(t *testing.T) {
  336. if got := jalaliExpireDateLabel(0); got != "" {
  337. t.Errorf("jalaliExpireDateLabel(0) = %q, want empty", got)
  338. }
  339. if got := jalaliExpireDateLabel(-1000); got != "" {
  340. t.Errorf("jalaliExpireDateLabel(-1000) = %q, want empty", got)
  341. }
  342. }
  343. func TestExpandNewTokensInTemplate(t *testing.T) {
  344. inbound := &model.Inbound{Remark: "DE", Protocol: "vless"}
  345. client := model.Client{Email: "[email protected]", ID: "abc-123"}
  346. stats := xray.ClientTraffic{Enable: true, Total: 100 * gb, Up: 50 * gb, Down: 0}
  347. ctx := remarkContext{
  348. client: client,
  349. stats: stats,
  350. inbound: inbound,
  351. transport: "ws",
  352. }
  353. cases := []struct{ tmpl, want string }{
  354. {"{{STATUS_EMOJI}}", "✅"},
  355. {"{{USAGE_PERCENTAGE}}", "50.0%"},
  356. {"{{PROTOCOL}}", "VLESS"},
  357. {"{{TRANSPORT}}", "ws"},
  358. {"{{STATUS_EMOJI}} {{INBOUND}}", "✅ DE"},
  359. }
  360. for _, c := range cases {
  361. if got := expandRemarkVars(c.tmpl, ctx); got != c.want {
  362. t.Errorf("expandRemarkVars(%q) = %q, want %q", c.tmpl, got, c.want)
  363. }
  364. }
  365. }
  366. func TestTranslateUISingleBrackets(t *testing.T) {
  367. cases := []struct{ in, want string }{
  368. {"{EMAIL}", "{{EMAIL}}"},
  369. {"{DATA_LEFT}", "{{TRAFFIC_LEFT}}"},
  370. {"{DATA_LEFT} of {DATA_LIMIT}", "{{TRAFFIC_LEFT}} of {{TRAFFIC_TOTAL}}"},
  371. {"{STATUS_EMOJI} {INBOUND}", "{{STATUS_EMOJI}} {INBOUND}"},
  372. {"{UNKNOWN_TOKEN}", "{UNKNOWN_TOKEN}"},
  373. {"no braces", "no braces"},
  374. {"{{TRAFFIC_LEFT}}", "{{TRAFFIC_LEFT}}"},
  375. {"{username}", "{username}"},
  376. }
  377. for _, c := range cases {
  378. if got := translateUISingleBrackets(c.in); got != c.want {
  379. t.Errorf("translateUISingleBrackets(%q) = %q, want %q", c.in, got, c.want)
  380. }
  381. }
  382. }
  383. func TestExpandRemarkVars_SingleBracketUI(t *testing.T) {
  384. inbound := &model.Inbound{Remark: "DE", Protocol: "vless"}
  385. stats := xray.ClientTraffic{Enable: true, Total: 100 * gb, Up: 50 * gb, Down: 0}
  386. ctx := remarkContext{
  387. client: model.Client{Email: "[email protected]"},
  388. stats: stats,
  389. inbound: inbound,
  390. transport: "ws",
  391. }
  392. cases := []struct{ tmpl, want string }{
  393. {"{EMAIL}", "[email protected]"},
  394. {"{DATA_LEFT}", "50.00GB"},
  395. {"{DATA_USAGE}", "50.00GB"},
  396. {"{DATA_LIMIT}", "100.00GB"},
  397. {"{STATUS_EMOJI}", "✅"},
  398. {"{USAGE_PERCENTAGE}", "50.0%"},
  399. {"{PROTOCOL}", "VLESS"},
  400. {"{TRANSPORT}", "ws"},
  401. }
  402. for _, c := range cases {
  403. if got := expandRemarkVars(c.tmpl, ctx); got != c.want {
  404. t.Errorf("expandRemarkVars(%q) = %q, want %q", c.tmpl, got, c.want)
  405. }
  406. }
  407. }
  408. func TestUsageOnFirstLinkOnly_SingleBracket(t *testing.T) {
  409. s := &SubService{
  410. remarkTemplate: "{STATUS_EMOJI} {{INBOUND}}|📊{{TRAFFIC_LEFT}}",
  411. subscriptionBody: true,
  412. usageShown: map[string]bool{},
  413. }
  414. inbound := &model.Inbound{
  415. Remark: "DE",
  416. ClientStats: []xray.ClientTraffic{{
  417. Email: "alice@x",
  418. Enable: true,
  419. Total: 100 * gb,
  420. Up: 20 * gb,
  421. Down: 10 * gb,
  422. }},
  423. }
  424. client := model.Client{Email: "alice@x"}
  425. first := s.genTemplatedRemark(inbound, client, "", "ws")
  426. s.usageShown["alice@x"] = true
  427. second := s.genTemplatedRemark(inbound, client, "", "ws")
  428. if !strings.Contains(first, "📊") {
  429. t.Fatalf("first link should carry usage: %q", first)
  430. }
  431. if strings.Contains(second, "📊") {
  432. t.Fatalf("second link must not carry usage: %q", second)
  433. }
  434. }