From 8ad990906555d843d1212c5eb8a1ac194031426c Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sat, 3 Jan 2026 21:24:50 +0900 Subject: internal/pkg: compute identifier from deps This provides infrastructure for computing a deterministic identifier based on current artifact kind, opaque parameters data, and optional dependency kind and identifiers. Signed-off-by: Ophestra --- internal/pkg/pkg_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'internal/pkg/pkg_test.go') diff --git a/internal/pkg/pkg_test.go b/internal/pkg/pkg_test.go index 79544430..1151a681 100644 --- a/internal/pkg/pkg_test.go +++ b/internal/pkg/pkg_test.go @@ -18,6 +18,46 @@ import ( "hakurei.app/internal/pkg" ) +// A stubArtifact implements [Artifact] with hardcoded kind and identifier. +type stubArtifact struct { + kind pkg.Kind + id pkg.ID +} + +func (a stubArtifact) Kind() pkg.Kind { return a.kind } +func (a stubArtifact) ID() pkg.ID { return a.id } +func (a stubArtifact) Hash() (pkg.Checksum, error) { panic("unreachable") } +func (a stubArtifact) Pathname() (*check.Absolute, error) { panic("unreachable") } + +func TestIdent(t *testing.T) { + t.Parallel() + + testCases := []struct { + name string + kind pkg.Kind + params []byte + deps []pkg.Artifact + want pkg.ID + }{ + {"tar", pkg.KindTar, []byte{ + 1, 0, 0, 0, 0, 0, 0, 0, + }, []pkg.Artifact{ + stubArtifact{pkg.KindHTTP, pkg.ID{}}, + }, pkg.MustDecode("HnySzeLQvSBZuTUcvfmLEX_OmH4yJWWH788NxuLuv7kVn8_uPM6Ks4rqFWM2NZJY")}, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + if got := tc.kind.Ident(tc.params, tc.deps...); got != tc.want { + t.Errorf("Ident: %s, want %s", + base64.URLEncoding.EncodeToString(got[:]), + base64.URLEncoding.EncodeToString(tc.want[:])) + } + }) + } +} + // cacheTestCase is a test case passed to checkWithCache where a new instance // of [pkg.Cache] is prepared for the test case, and is validated and removed // on test completion. -- cgit v1.3.1