===
navigate to network test page
%require
===
plwr -S plwr-test open "http://localhost:8599/network.html"
sleep 1
---

===
network: page load captures doc, css, js, img, and font resources
===
plwr -S plwr-test network
---
[
  {
    "duration": {{ doc_redirect_dur: int }},
    "method": "GET",
    "size": {{ doc_redirect_size: int }},
    "status": 200,
    "ts": {{ doc_redirect_ts: int }},
    "type": "doc",
    "url": "http://localhost:8599/network"
  },
  {
    "duration": {{ css_dur: int }},
    "method": null,
    "size": {{ css_size: int }},
    "status": 200,
    "ts": {{ css_ts: int }},
    "type": "css",
    "url": "http://localhost:8599/network.css"
  },
  {
    "duration": {{ js_dur: int }},
    "method": null,
    "size": {{ js_size: int }},
    "status": 200,
    "ts": {{ js_ts: int }},
    "type": "js",
    "url": "http://localhost:8599/network.js"
  },
  {
    "duration": {{ img_dur: int }},
    "method": null,
    "size": {{ img_size: int }},
    "status": 200,
    "ts": {{ img_ts: int }},
    "type": "img",
    "url": "http://localhost:8599/pixel.png"
  },
  {
    "duration": {{ doc_dur: int }},
    "method": "GET",
    "size": {{ doc_size: int }},
    "status": 200,
    "ts": {{ doc_ts: int }},
    "type": "doc",
    "url": "http://localhost:8599/network"
  },
  {
    "duration": {{ font_dur: int }},
    "method": null,
    "size": {{ font_size: int }},
    "status": 200,
    "ts": {{ font_ts: int }},
    "type": "font",
    "url": "http://localhost:8599/test-font.woff2"
  }
]

===
network: --type doc filters to document requests only
===
plwr -S plwr-test network --type doc
---
[
  {
    "duration": {{ doc1_dur: int }},
    "method": "GET",
    "size": {{ doc1_size: int }},
    "status": 200,
    "ts": {{ doc1_ts: int }},
    "type": "doc",
    "url": "http://localhost:8599/network"
  },
  {
    "duration": {{ doc2_dur: int }},
    "method": "GET",
    "size": {{ doc2_size: int }},
    "status": 200,
    "ts": {{ doc2_ts: int }},
    "type": "doc",
    "url": "http://localhost:8599/network"
  }
]

===
network: --type css filters to stylesheet requests
===
plwr -S plwr-test network --type css
---
[
  {
    "duration": {{ css_filt_dur: int }},
    "method": null,
    "size": {{ css_filt_size: int }},
    "status": 200,
    "ts": {{ css_filt_ts: int }},
    "type": "css",
    "url": "http://localhost:8599/network.css"
  }
]

===
network: --type js filters to script requests
===
plwr -S plwr-test network --type js
---
[
  {
    "duration": {{ js_filt_dur: int }},
    "method": null,
    "size": {{ js_filt_size: int }},
    "status": 200,
    "ts": {{ js_filt_ts: int }},
    "type": "js",
    "url": "http://localhost:8599/network.js"
  }
]

===
network: --type img filters to image requests
===
plwr -S plwr-test network --type img
---
[
  {
    "duration": {{ img_filt_dur: int }},
    "method": null,
    "size": {{ img_filt_size: int }},
    "status": 200,
    "ts": {{ img_filt_ts: int }},
    "type": "img",
    "url": "http://localhost:8599/pixel.png"
  }
]

===
network: --type font captures @font-face loads
===
plwr -S plwr-test network --type font
---
[
  {
    "duration": {{ font_filt_dur: int }},
    "method": null,
    "size": {{ font_filt_size: int }},
    "status": 200,
    "ts": {{ font_filt_ts: int }},
    "type": "font",
    "url": "http://localhost:8599/test-font.woff2"
  }
]

===
network: --clear empties the buffer
===
plwr -S plwr-test network --clear
plwr -S plwr-test network
---
[]

===
network: POST fetch captures method and status
===
plwr -S plwr-test click '#do-fetch'
plwr -S plwr-test wait '#fetch-result:has-text("fetch:")'
plwr -S plwr-test network --type fetch
---
[
  {
    "duration": {{ fetch_dur: int }},
    "method": "POST",
    "size": {{ fetch_size: int }},
    "status": 200,
    "ts": {{ fetch_ts: int }},
    "type": "fetch",
    "url": "http://localhost:8599/network.json"
  }
]

===
network: XHR captures method and status
===
plwr -S plwr-test network --clear
plwr -S plwr-test click '#do-xhr'
plwr -S plwr-test wait '#xhr-result:has-text("xhr:")'
plwr -S plwr-test network --type xhr
---
[
  {
    "duration": {{ xhr_dur: int }},
    "method": "PUT",
    "size": {{ xhr_size: int }},
    "status": 200,
    "ts": {{ xhr_ts: int }},
    "type": "xhr",
    "url": "http://localhost:8599/network.json"
  }
]

===
network: WebSocket shows status 101 on successful connection
===
plwr -S plwr-test network --clear
plwr -S plwr-test click '#do-ws'
plwr -S plwr-test wait '#ws-result:has-text("ws:hello")' -T 5000
plwr -S plwr-test network --type ws
---
[
  {
    "duration": {{ ws_dur: int }},
    "method": null,
    "size": 0,
    "status": 101,
    "ts": {{ ws_ts: int }},
    "type": "ws",
    "url": "ws://localhost:8597"
  }
]

===
network: --type with multiple comma-separated types
===
plwr -S plwr-test network --clear
plwr -S plwr-test open "http://localhost:8599/network.html"
sleep 1
plwr -S plwr-test network --type css,js
---
[
  {
    "duration": {{ multi_css_dur: int }},
    "method": null,
    "size": {{ multi_css_size: int }},
    "status": 200,
    "ts": {{ multi_css_ts: int }},
    "type": "css",
    "url": "http://localhost:8599/network.css"
  },
  {
    "duration": {{ multi_js_dur: int }},
    "method": null,
    "size": {{ multi_js_size: int }},
    "status": 200,
    "ts": {{ multi_js_ts: int }},
    "type": "js",
    "url": "http://localhost:8599/network.js"
  }
]

===
network: --type with no matches returns empty array
===
plwr -S plwr-test network --type ws
---
[]

===
network: trigger a 404 fetch
===
plwr -S plwr-test network --clear
plwr -S plwr-test eval "void fetch('/does-not-exist')"
sleep 0.5
---

===
network: 404 response shows correct status code
===
plwr -S plwr-test network --type fetch
---
[
  {
    "duration": {{ notfound_dur: int }},
    "method": "GET",
    "size": {{ notfound_size: int }},
    "status": 404,
    "ts": {{ notfound_ts: int }},
    "type": "fetch",
    "url": "http://localhost:8599/does-not-exist"
  }
]

===
network: multiple requests accumulate in buffer
===
plwr -S plwr-test network --clear
plwr -S plwr-test click '#do-fetch'
plwr -S plwr-test wait '#fetch-result:has-text("fetch:")'
plwr -S plwr-test click '#do-xhr'
plwr -S plwr-test wait '#xhr-result:has-text("xhr:")'
plwr -S plwr-test network
---
[
  {
    "duration": {{ accum_fetch_dur: int }},
    "method": "POST",
    "size": {{ accum_fetch_size: int }},
    "status": 200,
    "ts": {{ accum_fetch_ts: int }},
    "type": "fetch",
    "url": "http://localhost:8599/network.json"
  },
  {
    "duration": {{ accum_xhr_dur: int }},
    "method": "PUT",
    "size": {{ accum_xhr_size: int }},
    "status": 200,
    "ts": {{ accum_xhr_ts: int }},
    "type": "xhr",
    "url": "http://localhost:8599/network.json"
  }
]

===
network: --url filters by regex pattern
===
plwr -S plwr-test network --clear
plwr -S plwr-test open "http://localhost:8599/network.html"
sleep 1
plwr -S plwr-test network --url '\.css$'
---
[
  {
    "duration": {{ url_css_dur: int }},
    "method": null,
    "size": {{ url_css_size: int }},
    "status": 200,
    "ts": {{ url_css_ts: int }},
    "type": "css",
    "url": "http://localhost:8599/network.css"
  }
]

===
network: --url with partial match
===
plwr -S plwr-test network --url 'pixel'
---
[
  {
    "duration": {{ url_img_dur: int }},
    "method": null,
    "size": {{ url_img_size: int }},
    "status": 200,
    "ts": {{ url_img_ts: int }},
    "type": "img",
    "url": "http://localhost:8599/pixel.png"
  }
]

===
network: --type and --url combined
===
plwr -S plwr-test network --type doc --url 'network'
---
[
  {
    "duration": {{ combo_doc1_dur: int }},
    "method": "GET",
    "size": {{ combo_doc1_size: int }},
    "status": 200,
    "ts": {{ combo_doc1_ts: int }},
    "type": "doc",
    "url": "http://localhost:8599/network"
  },
  {
    "duration": {{ combo_doc2_dur: int }},
    "method": "GET",
    "size": {{ combo_doc2_size: int }},
    "status": 200,
    "ts": {{ combo_doc2_ts: int }},
    "type": "doc",
    "url": "http://localhost:8599/network"
  }
]

===
network: --url with no matches returns empty array
===
plwr -S plwr-test network --url 'zzz-no-match-zzz'
---
[]

===
network: persists across navigations
===
plwr -S plwr-test network --clear
plwr -S plwr-test open "http://localhost:8599/network.html"
sleep 1
plwr -S plwr-test network
---
{{ entries: json array }}
---
where
* len(entries) >= 5
