import assert from "node:assert/strict"
import { applicationName } from "./traffic-flow-apps.js"
import { classifyFlowDst, disableCatalogFetchForTests, resetFlowCatalogForTests, seedFlowCatalogForTests } from "./traffic-flow-classify.js"

disableCatalogFetchForTests()
resetFlowCatalogForTests()
seedFlowCatalogForTests({
  cidrs: [{ cidr: "192.0.2.0/24", purpose: "steam-gaming" }],
})

const hit = classifyFlowDst("192.0.2.10", 6, 443, 50000, null)
assert.equal(hit.category, "Игры")
assert.equal(hit.service, "steam-gaming")

const miss = classifyFlowDst("203.0.113.9", 17, 53, 53000, null)
assert.equal(miss.category, "DNS")

const cdn = classifyFlowDst("203.0.113.9", 6, 443, 1, { prefix: "203.0.113.0/24", asn: 13335, country: "US", lat: null, lng: null, holder: "CLOUDFLARENET", ok: true, fetchedAt: Date.now() })
assert.equal(cdn.category, "CDN")
assert.equal(cdn.service, "Cloudflare")

const amazonHolder = classifyFlowDst("203.0.113.50", 6, 443, 1, { prefix: "203.0.113.0/24", asn: 64500, country: "RU", lat: null, lng: null, holder: "AMAZON-AES - Amazon.com, Inc.", ok: true, fetchedAt: Date.now() })
assert.equal(amazonHolder.service, "Прочее")
assert.notEqual(amazonHolder.service, "AMAZON-AES - Amazon.com, Inc.")

const google = classifyFlowDst("173.194.160.163", 6, 443, 1, {
  prefix: "173.194.0.0/16",
  asn: 15169,
  country: "US",
  lat: null,
  lng: null,
  holder: "GOOGLE",
  ok: true,
  fetchedAt: Date.now(),
})
assert.equal(google.service, "Google")
assert.equal(google.category, "Веб")

const googleCidr = classifyFlowDst("173.194.151.65", 6, 57182, 443, null)
assert.equal(googleCidr.service, "Google")
assert.equal(googleCidr.category, "Веб")

const youtube = classifyFlowDst("173.194.160.163", 6, 443, 1, {
  prefix: "173.194.0.0/16",
  asn: 15169,
  country: "US",
  lat: null,
  lng: null,
  holder: "YouTube LLC",
  ok: true,
  fetchedAt: Date.now(),
})
assert.equal(youtube.service, "YouTube")
assert.equal(youtube.category, "Видео / стриминг")

const gre = classifyFlowDst("198.51.100.1", 47, 0, 0, null)
assert.equal(gre.service, "GRE")
assert.equal(gre.category, "Туннель")
const esp = classifyFlowDst("198.51.100.1", 50, 0, 0, null)
assert.equal(esp.category, "Туннель")
assert.equal(applicationName(17, 443, 50000), "QUIC")
assert.equal(applicationName(17, 853, 50000), "DNS")

console.log("traffic-flow-classify.test.ts: ok")
