Bugfixes and improved alignment with architecture blueprint

This commit is contained in:
2026-05-04 20:36:12 -05:00
parent c07320f9d0
commit fa070a296d
10 changed files with 158 additions and 34 deletions

View File

@@ -36,6 +36,12 @@ func NewCompositeReader() Reader {
}
func (c *CompositeReader) Read(ctx context.Context, ref domain.ArtifactRef) (*domain.Artifact, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
switch ref.Type {
case domain.ArtifactRefInline:
return c.inlineReader.Read(ctx, ref)
@@ -49,22 +55,35 @@ func (c *CompositeReader) Read(ctx context.Context, ref domain.ArtifactRef) (*do
type inlineReader struct{}
func (r *inlineReader) Read(ctx context.Context, ref domain.ArtifactRef) (*domain.Artifact, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if ref.Body == "" {
return nil, ErrMissingInlineBody
}
body := []byte(ref.Body)
return &domain.Artifact{
Body: body,
Size: int64(len(body)),
Hash: fmt.Sprintf("%x", sha256.Sum256(body)),
URI: ref.URI,
ContentType: "text/plain",
Body: body,
Size: int64(len(body)),
Hash: fmt.Sprintf("%x", sha256.Sum256(body)),
URI: ref.URI,
}, nil
}
type fileReader struct{}
func (r *fileReader) Read(ctx context.Context, ref domain.ArtifactRef) (*domain.Artifact, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if ref.URI == "" {
return nil, ErrMissingFilePath
}

View File

@@ -2,6 +2,7 @@ package artifact
import (
"context"
"errors"
"os"
"testing"
@@ -24,8 +25,11 @@ func TestCompositeReader_Read(t *testing.T) {
if string(art.Body) != "hello world" {
t.Errorf("expected 'hello world', got %s", string(art.Body))
}
if art.Hash == "" {
t.Error("expected hash to be computed")
if art.ContentType != "text/plain" {
t.Errorf("expected text/plain content type, got %q", art.ContentType)
}
if art.Hash != "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" {
t.Errorf("unexpected hash: %s", art.Hash)
}
})
@@ -35,7 +39,7 @@ func TestCompositeReader_Read(t *testing.T) {
Body: "",
}
_, err := reader.Read(ctx, ref)
if err == nil || err != ErrMissingInlineBody {
if !errors.Is(err, ErrMissingInlineBody) {
t.Errorf("expected ErrMissingInlineBody, got %v", err)
}
})
@@ -46,7 +50,7 @@ func TestCompositeReader_Read(t *testing.T) {
URI: "s3://bucket/key",
}
_, err := reader.Read(ctx, ref)
if err == nil {
if !errors.Is(err, ErrUnsupportedRefType) {
t.Error("expected error for unsupported type")
}
})
@@ -83,8 +87,8 @@ func TestFileReader_Read(t *testing.T) {
if art.Name == "" {
t.Error("expected name to be inferred from filename")
}
if art.Hash == "" {
t.Error("expected hash to be computed")
if art.Hash != "60f5237ed4049f0382661ef009d2bc42e48c3ceb3edb6600f7024e7ab3b838f3" {
t.Errorf("unexpected hash: %s", art.Hash)
}
})
@@ -94,7 +98,7 @@ func TestFileReader_Read(t *testing.T) {
URI: "",
}
_, err := reader.Read(ctx, ref)
if err == nil || err != ErrMissingFilePath {
if !errors.Is(err, ErrMissingFilePath) {
t.Errorf("expected ErrMissingFilePath, got %v", err)
}
})