package nextcloud

import (
	"io"
	"net/http"
	"strings"
	"testing"
)

func TestFileModifiedTimeRFC3339(t *testing.T) {
	raw := "2024-06-01T12:00:00Z"
	got := fileModifiedTime(raw)
	if got.IsZero() {
		t.Fatalf("expected parsed time for %q", raw)
	}
}

func TestFileModifiedTimeHTTPDate(t *testing.T) {
	raw := "Wed, 01 Jun 2024 12:00:00 GMT"
	got := fileModifiedTime(raw)
	if got.IsZero() {
		t.Fatalf("expected parsed time for %q", raw)
	}
}

func TestDecodeOCSQuotaResponse(t *testing.T) {
	body := strings.NewReader(`{
		"ocs": {
			"meta": { "statuscode": 100 },
			"data": {
				"quota": {
					"free": 900,
					"used": 100,
					"total": 1000,
					"relative": 10
				}
			}
		}
	}`)
	resp := &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(body)}
	q, err := decodeOCSQuotaResponse(resp, "test")
	if err != nil {
		t.Fatal(err)
	}
	if q.Total != 1000 || q.Used != 100 || q.Free != 900 {
		t.Fatalf("unexpected quota: %+v", q)
	}
}

func TestParseQuotaPropfind(t *testing.T) {
	raw := `<?xml version="1.0"?>
<d:multistatus xmlns:d="DAV:">
  <d:response>
    <d:propstat>
      <d:prop>
        <d:quota-available-bytes>900</d:quota-available-bytes>
        <d:quota-used-bytes>100</d:quota-used-bytes>
      </d:prop>
    </d:propstat>
  </d:response>
</d:multistatus>`
	q, err := parseQuotaPropfind(strings.NewReader(raw))
	if err != nil {
		t.Fatal(err)
	}
	if q.Total != 1000 || q.Used != 100 || q.Free != 900 {
		t.Fatalf("unexpected quota: %+v", q)
	}
}
