# Smoke test that the HTTP logging looks reasonable. The actual log messages
# are tested more fully in the httplog package itself.
# We only check common fields like time, level, and the user-agent header
# in the first test case, to avoid repeating ourselves.
env CUE_DEBUG=http

# Tidy, and then check that the resulting module evaluates as expected.
exec cue mod tidy
stderr '{"time":".*","level":"INFO","msg":"http client->","info":{"id":[0-9]+,"method":"GET","url":"http://[^/]+/v2/example.com/tags/list\?n=\d+","contentLength":0,"header":{"User-Agent":\["Cue/[^ ]+ \(cmd/cue; .*\) Go/[^ ]+ \(.*\)"\]}}}'
exec cue export .
cmp stdout export.stdout

# Update to the latest version of a dependency.
exec cue mod get example.com@latest
stderr '"msg":"http client->".*"method":"GET","url":"http://[^/]+/v2/example.com/tags/list\?n=\d+",'

# Mirror a module to a registry with auth; the auth credentials should be redacted.
memregistry -auth=foo:bar MEMREGISTRY
env OLD_CUE_REGISTRY=${CUE_REGISTRY}
env CUE_REGISTRY=$MEMREGISTRY+insecure
env DOCKER_CONFIG=$WORK/dockerconfig
env-fill $DOCKER_CONFIG/config.json
exec cue mod mirror --from $OLD_CUE_REGISTRY example.com
stderr '"msg":"http client->".*"method":"GET","url":"http://[^/]+/v2/example.com/tags/list\?n=\d+","contentLength":0,'
stderr '"msg":"http client->".*"method":"POST","url":"http://[^/]+/v2/example.com/blobs/uploads/","contentLength":0,"header":{"Authorization":\["Basic REDACTED"\],'

# Publish a module, again with redacted auth credentials.
exec cue mod publish v0.0.2
stderr '"msg":"http client->".*"method":"PUT","url":"http://[^/]+/v2/main\.org/blobs/uploads/[a-zA-Z0-9_-]+\?digest=sha256%3A[0-9a-f]+","contentLength":2,"header":{"Authorization":\["Basic REDACTED"\],"Content-Range":\["0-1"\],'

-- dockerconfig/config.json --
{
	"auths": {
		"${MEMREGISTRY}": {
			"username": "foo",
			"password": "bar"
		}
	}
}
-- export.stdout --
{
    "example.com@v0": "v0.0.1",
    "main": "main"
}
-- cue.mod/module.cue --
module: "main.org@v0"
language: version: "v0.8.0"

-- main.cue --
package main
import "example.com@v0:main"

main
"main": "main"

-- _registry/example.com_v0.0.1/cue.mod/module.cue --
module: "example.com@v0"
language: version: "v0.8.0"

-- _registry/example.com_v0.0.1/top.cue --
package main

"example.com@v0": "v0.0.1"
