From 5b3bbe5a8a6593d5fec4c32247f28694022afd59 Mon Sep 17 00:00:00 2001 From: tr00d Date: Mon, 23 Nov 2026 16:41:05 +0100 Subject: [PATCH] feat(storage): delete a vector index --- ...uldSerializeToExpectedPayload.verified.txt | 1 + .../DeleteVectorIndexApprovalTests.cs | 22 ++++ .../DeleteVectorIndexContractTests.cs | 118 ++++++++++++++++++ .../DeleteVectorIndexTests.cs | 59 +++++++++ .../Storage/Storage/Common/VectorService.cs | 32 +++++ .../DeleteVectorIndexBody.cs | 18 +++ .../Interfaces/IStorageVectorBucketApi.cs | 15 +++ .../Storage/Interfaces/IVectorBucket.cs | 31 +++++ .../Storage/Storage/PublicAPI.Unshipped.txt | 3 + .../Storage/Storage/StorageVectorBucketApi.cs | 31 ++--- packages/Storage/Storage/VectorBucket.cs | 26 ++++ 11 files changed, 336 insertions(+), 20 deletions(-) create mode 100644 packages/Storage/Storage.Tests/DeleteVectorIndex/Data/DeleteVectorIndexApprovalTests.DeleteIndexRequest_ShouldSerializeToExpectedPayload.verified.txt create mode 100644 packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexApprovalTests.cs create mode 100644 packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexContractTests.cs create mode 100644 packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexTests.cs create mode 100644 packages/Storage/Storage/Common/VectorService.cs create mode 100644 packages/Storage/Storage/DeleteVectorIndex/DeleteVectorIndexBody.cs create mode 100644 packages/Storage/Storage/Interfaces/IVectorBucket.cs create mode 100644 packages/Storage/Storage/VectorBucket.cs diff --git a/packages/Storage/Storage.Tests/DeleteVectorIndex/Data/DeleteVectorIndexApprovalTests.DeleteIndexRequest_ShouldSerializeToExpectedPayload.verified.txt b/packages/Storage/Storage.Tests/DeleteVectorIndex/Data/DeleteVectorIndexApprovalTests.DeleteIndexRequest_ShouldSerializeToExpectedPayload.verified.txt new file mode 100644 index 00000000..3923cc5e --- /dev/null +++ b/packages/Storage/Storage.Tests/DeleteVectorIndex/Data/DeleteVectorIndexApprovalTests.DeleteIndexRequest_ShouldSerializeToExpectedPayload.verified.txt @@ -0,0 +1 @@ +{"vectorBucketName":"embeddings-prod","indexName":"docs-v1"} \ No newline at end of file diff --git a/packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexApprovalTests.cs b/packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexApprovalTests.cs new file mode 100644 index 00000000..1ffeb6cb --- /dev/null +++ b/packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexApprovalTests.cs @@ -0,0 +1,22 @@ +using System.Threading.Tasks; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Storage.Tests.Support; + +namespace Storage.Tests.DeleteVectorIndex; + +/// +/// Pins the exact bytes Vectors.From(...).DeleteIndexAsync puts on the wire; the approved body must +/// match the service's DeleteIndex schema (vectorBucketName and indexName, camelCase). +/// +[TestClass] +[TestCategory("Contract")] +public class DeleteVectorIndexApprovalTests : RequestApprovalFixture +{ + [TestMethod] + public async Task DeleteIndexRequest_ShouldSerializeToExpectedPayload() + { + this.RespondWith(""); + await this.Client.Vectors.From("embeddings-prod").DeleteIndexAsync("docs-v1"); + await this.Verify(this.EmittedRequestBody).UseDirectory("Data"); + } +} diff --git a/packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexContractTests.cs b/packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexContractTests.cs new file mode 100644 index 00000000..dc640cf5 --- /dev/null +++ b/packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexContractTests.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using FluentAssertions; +using FluentAssertions.Execution; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Supabase.Storage; +using Supabase.Storage.Exceptions; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; +using WireMock.Server; + +namespace Storage.Tests.DeleteVectorIndex; + +/// +/// Contract tests for Vectors.From(bucket).DeleteIndexAsync: the request it sends to +/// POST /vector/DeleteIndex and how each service answer maps to the caller's result. +/// +[TestClass] +[TestCategory("Contract")] +public class DeleteVectorIndexContractTests +{ + private const string Path = "/storage/v1/vector/DeleteIndex"; + private WireMockServer server = null!; + private Client client = null!; + + [TestInitialize] + public void TestInitialize() + { + this.server = WireMockServer.Start(); + this.client = new Client($"{this.server.Url}/storage/v1", new Dictionary { { "Authorization", "Bearer service-role-key" } }); + } + + [TestCleanup] + public void TestCleanup() => this.server.Stop(); + + [TestMethod] + public async Task DeleteIndexAsync_ShouldPostTheBucketAndIndexNamesToTheVectorRoute() + { + this.Respond(200, string.Empty); + await this.client.Vectors.From("embeddings-prod").DeleteIndexAsync("docs-v1"); + var request = this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!; + using (new AssertionScope()) + { + request.Method.Should().Be("POST"); + request.Path.Should().Be(Path); + request.Body.Should().Be("{\"vectorBucketName\":\"embeddings-prod\",\"indexName\":\"docs-v1\"}"); + request.Headers!["Authorization"].Should().ContainSingle().Which.Should().Be("Bearer service-role-key"); + } + } + + [TestMethod] + public void From_ShouldNotSendARequest() + { + this.client.Vectors.From("embeddings-prod"); + this.server.LogEntries.Should().BeEmpty("scoping to a bucket is local; only an operation reaches the service"); + } + + [TestMethod] + [DataRow(400, "InvalidRequest", "the service rejects a malformed index name")] + [DataRow(404, "S3VectorNotFoundException", "the bucket or the index does not exist")] + [DataRow(500, "InternalError", "the service failed internally")] + public async Task DeleteIndexAsync_ShouldThrowStorageException_GivenAServiceError(int status, string code, string because) + { + this.Respond(status, $"{{\"statusCode\":\"{status}\",\"code\":\"{code}\",\"message\":\"refused\"}}"); + var thrown = await FluentActions.Awaiting(() => this.client.Vectors.From("embeddings-prod").DeleteIndexAsync("docs-v1")) + .Should().ThrowAsync(because); + using (new AssertionScope()) + { + thrown.Which.StatusCode.Should().Be(status); + thrown.Which.Code.Should().Be(code); + } + } + + [TestMethod] + public async Task DeleteIndexAsync_ShouldClassifyAsNotAuthorized_GivenANonServiceRoleCaller() + { + this.Respond(403, "{\"statusCode\":\"403\",\"error\":\"Unauthorized\",\"message\":\"Invalid compact JWS\"}"); + (await FluentActions.Awaiting(() => this.client.Vectors.From("embeddings-prod").DeleteIndexAsync("docs-v1")) + .Should().ThrowAsync()) + .Which.Reason.Should().Be(FailureHint.Reason.NotAuthorized); + } + + [TestMethod] + public async Task DeleteIndexAsync_ShouldThrowStorageException_GivenTheVectorServiceIsNotEnabled() + { + this.Respond(404, "{\"statusCode\":\"404\",\"error\":\"Not Found\",\"message\":\"Route POST:/vector/DeleteIndex not found\"}"); + (await FluentActions.Awaiting(() => this.client.Vectors.From("embeddings-prod").DeleteIndexAsync("docs-v1")) + .Should().ThrowAsync()) + .Which.StatusCode.Should().Be(404); + } + + [TestMethod] + public async Task DeleteIndexAsync_ShouldCancel_GivenACanceledToken() + { + this.Respond(200, string.Empty); + using var source = new CancellationTokenSource(); + source.Cancel(); + await FluentActions.Awaiting(() => this.client.Vectors.From("embeddings-prod").DeleteIndexAsync("docs-v1", source.Token)) + .Should().ThrowAsync("the token must reach the HTTP call"); + } + + [TestMethod] + public async Task DeleteIndexAsync_ShouldSendDynamicHeaders_GivenGetHeadersOnTheClient() + { + this.Respond(200, string.Empty); + this.client.GetHeaders = () => new Dictionary { { "X-Dynamic", "fresh" } }; + await this.client.Vectors.From("embeddings-prod").DeleteIndexAsync("docs-v1"); + this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Headers!["X-Dynamic"] + .Should().ContainSingle().Which.Should().Be("fresh"); + } + + private void Respond(int statusCode, string body) => + this.server.Given(Request.Create().WithPath(Path).UsingPost()) + .RespondWith(Response.Create().WithStatusCode(statusCode) + .WithHeader("Content-Type", "application/json").WithBody(body)); +} diff --git a/packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexTests.cs b/packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexTests.cs new file mode 100644 index 00000000..7c2ad566 --- /dev/null +++ b/packages/Storage/Storage.Tests/DeleteVectorIndex/DeleteVectorIndexTests.cs @@ -0,0 +1,59 @@ +using System; +using System.Net.Http; +using System.Threading.Tasks; +using FluentAssertions; +using FluentAssertions.Execution; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using NSubstitute; +using Supabase.Storage; + +namespace Storage.Tests.DeleteVectorIndex; + +/// +/// Unit tests for the checks Vectors.From(bucket).DeleteIndexAsync makes before it touches the wire. +/// +[TestClass] +[TestCategory("Unit")] +public class DeleteVectorIndexTests +{ + [TestMethod] + [DataRow(null)] + [DataRow("")] + [DataRow(" ")] + public void From_ShouldThrowArgumentException_GivenABlankBucketName(string? name) + { + var sut = new Client("http://localhost:1/storage/v1"); + var thrown = FluentActions.Invoking(() => sut.Vectors.From(name!)) + .Should().Throw("a blank bucket name can never address a bucket"); + using (new AssertionScope()) + { + thrown.Which.ParamName.Should().Be("vectorBucketName"); + thrown.Which.Message.Should().Contain("null, empty or whitespace", "the caller needs to know why the name was refused"); + } + } + + [TestMethod] + [DataRow(null)] + [DataRow("")] + [DataRow(" ")] + public async Task DeleteIndexAsync_ShouldThrowArgumentException_GivenABlankIndexName(string? name) + { + var sut = new Client("http://localhost:1/storage/v1"); + var thrown = await FluentActions.Awaiting(() => sut.Vectors.From("embeddings-prod").DeleteIndexAsync(name!)) + .Should().ThrowAsync("a blank index name can never address an index and must not reach the service"); + using (new AssertionScope()) + { + thrown.Which.ParamName.Should().Be("indexName"); + thrown.Which.Message.Should().Contain("null, empty or whitespace", "the caller needs to know why the name was refused"); + } + } + + [TestMethod] + public async Task DeleteIndexAsync_ShouldNotSendARequest_GivenABlankIndexName() + { + var handler = Substitute.For(); + var sut = new Client("http://localhost:1/storage/v1", new ClientOptions { HttpRequestClient = new HttpClient(handler) }); + await FluentActions.Awaiting(() => sut.Vectors.From("embeddings-prod").DeleteIndexAsync(" ")).Should().ThrowAsync(); + handler.ReceivedCalls().Should().BeEmpty("validation happens before any I/O"); + } +} diff --git a/packages/Storage/Storage/Common/VectorService.cs b/packages/Storage/Storage/Common/VectorService.cs new file mode 100644 index 00000000..222d2f04 --- /dev/null +++ b/packages/Storage/Storage/Common/VectorService.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace Supabase.Storage.Common; + +internal sealed class VectorService +{ + private readonly string url; + private readonly ClientOptions options; + private readonly HttpClient requestClient; + private readonly Func> headers; + + internal VectorService(string storageUrl, ClientOptions options, HttpClient requestClient, Func> headers) + { + this.url = $"{storageUrl.TrimEnd('/')}/vector"; + this.options = options; + this.requestClient = requestClient; + this.headers = headers; + } + + internal Task Post(string operation, object body, CancellationToken cancellationToken) => + Helpers.MakeRequestAsync(this.requestClient, this.options, HttpMethod.Post, $"{this.url}/{operation}", body, this.headers(), cancellationToken); + + internal static void RequireName(string value, string parameterName) + { + if (string.IsNullOrWhiteSpace(value)) + throw new ArgumentException("The value cannot be null, empty or whitespace.", parameterName); + } +} diff --git a/packages/Storage/Storage/DeleteVectorIndex/DeleteVectorIndexBody.cs b/packages/Storage/Storage/DeleteVectorIndex/DeleteVectorIndexBody.cs new file mode 100644 index 00000000..f61a43bb --- /dev/null +++ b/packages/Storage/Storage/DeleteVectorIndex/DeleteVectorIndexBody.cs @@ -0,0 +1,18 @@ +using System.Text.Json.Serialization; + +namespace Supabase.Storage.DeleteVectorIndex; + +internal sealed class DeleteVectorIndexBody +{ + internal DeleteVectorIndexBody(string vectorBucketName, string indexName) + { + this.VectorBucketName = vectorBucketName; + this.IndexName = indexName; + } + + [JsonPropertyName("vectorBucketName")] + public string VectorBucketName { get; } + + [JsonPropertyName("indexName")] + public string IndexName { get; } +} diff --git a/packages/Storage/Storage/Interfaces/IStorageVectorBucketApi.cs b/packages/Storage/Storage/Interfaces/IStorageVectorBucketApi.cs index e4dc901b..52023136 100644 --- a/packages/Storage/Storage/Interfaces/IStorageVectorBucketApi.cs +++ b/packages/Storage/Storage/Interfaces/IStorageVectorBucketApi.cs @@ -9,6 +9,21 @@ namespace Supabase.Storage.Interfaces; /// public interface IStorageVectorBucketApi { + /// + /// Scopes the following operations to one vector bucket. Nothing is sent to the service. + /// + /// The name of the vector bucket. + /// A client for the operations that act inside that bucket. + /// + /// is null, empty or whitespace. + /// + /// + /// + /// var bucket = storage.Vectors.From("embeddings-prod"); + /// + /// + IVectorBucket From(string vectorBucketName); + /// /// Deletes an empty vector bucket. The service rejects the call while the bucket still has /// indexes. Requires a service-role key. diff --git a/packages/Storage/Storage/Interfaces/IVectorBucket.cs b/packages/Storage/Storage/Interfaces/IVectorBucket.cs new file mode 100644 index 00000000..892cdb43 --- /dev/null +++ b/packages/Storage/Storage/Interfaces/IVectorBucket.cs @@ -0,0 +1,31 @@ +using System.Threading; +using System.Threading.Tasks; +using Supabase.Core.Attributes; + +namespace Supabase.Storage.Interfaces; + +/// +/// Operations scoped to one vector bucket, obtained from . +/// +public interface IVectorBucket +{ + /// + /// Deletes a vector index from this bucket, together with its vectors. Requires a service-role key. + /// + /// The name of the index to delete. + /// Token used to cancel the request. + /// + /// is null, empty or whitespace. + /// + /// + /// The service refused the call, for example because the bucket or the index does not exist + /// (Code S3VectorNotFoundException). + /// + /// + /// + /// await storage.Vectors.From("embeddings-prod").DeleteIndexAsync("docs-v1"); + /// + /// + [ImplementedCapability("storage.vector_buckets.delete_index")] + Task DeleteIndexAsync(string indexName, CancellationToken cancellationToken = default); +} diff --git a/packages/Storage/Storage/PublicAPI.Unshipped.txt b/packages/Storage/Storage/PublicAPI.Unshipped.txt index e8dcfd14..a6f28c2f 100644 --- a/packages/Storage/Storage/PublicAPI.Unshipped.txt +++ b/packages/Storage/Storage/PublicAPI.Unshipped.txt @@ -3,3 +3,6 @@ Supabase.Storage.Client.Vectors.get -> Supabase.Storage.Interfaces.IStorageVecto Supabase.Storage.Interfaces.IStorageClient.Vectors.get -> Supabase.Storage.Interfaces.IStorageVectorBucketApi! Supabase.Storage.Interfaces.IStorageVectorBucketApi Supabase.Storage.Interfaces.IStorageVectorBucketApi.DeleteVectorBucketAsync(string! vectorBucketName, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! +Supabase.Storage.Interfaces.IStorageVectorBucketApi.From(string! vectorBucketName) -> Supabase.Storage.Interfaces.IVectorBucket! +Supabase.Storage.Interfaces.IVectorBucket +Supabase.Storage.Interfaces.IVectorBucket.DeleteIndexAsync(string! indexName, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! diff --git a/packages/Storage/Storage/StorageVectorBucketApi.cs b/packages/Storage/Storage/StorageVectorBucketApi.cs index fd9453f4..6d4cd859 100644 --- a/packages/Storage/Storage/StorageVectorBucketApi.cs +++ b/packages/Storage/Storage/StorageVectorBucketApi.cs @@ -3,6 +3,7 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Supabase.Storage.Common; using Supabase.Storage.DeleteVectorBucket; using Supabase.Storage.Interfaces; @@ -10,32 +11,22 @@ namespace Supabase.Storage; internal sealed class StorageVectorBucketApi : IStorageVectorBucketApi { - private readonly string url; - private readonly ClientOptions options; - private readonly HttpClient requestClient; - private readonly Func> headers; + private readonly VectorService service; - internal StorageVectorBucketApi(string storageUrl, ClientOptions options, HttpClient requestClient, Func> headers) - { - this.url = $"{storageUrl.TrimEnd('/')}/vector"; - this.options = options; - this.requestClient = requestClient; - this.headers = headers; - } + internal StorageVectorBucketApi(string storageUrl, ClientOptions options, HttpClient requestClient, Func> headers) => + this.service = new VectorService(storageUrl, options, requestClient, headers); /// - public Task DeleteVectorBucketAsync(string vectorBucketName, CancellationToken cancellationToken = default) + public IVectorBucket From(string vectorBucketName) { - RequireName(vectorBucketName, nameof(vectorBucketName)); - return this.Post("DeleteVectorBucket", new DeleteVectorBucketBody(vectorBucketName), cancellationToken); + VectorService.RequireName(vectorBucketName, nameof(vectorBucketName)); + return new VectorBucket(vectorBucketName, this.service); } - private Task Post(string operation, object body, CancellationToken cancellationToken) => - Helpers.MakeRequestAsync(this.requestClient, this.options, HttpMethod.Post, $"{this.url}/{operation}", body, this.headers(), cancellationToken); - - private static void RequireName(string value, string parameterName) + /// + public Task DeleteVectorBucketAsync(string vectorBucketName, CancellationToken cancellationToken = default) { - if (string.IsNullOrWhiteSpace(value)) - throw new ArgumentException("The value cannot be null, empty or whitespace.", parameterName); + VectorService.RequireName(vectorBucketName, nameof(vectorBucketName)); + return this.service.Post("DeleteVectorBucket", new DeleteVectorBucketBody(vectorBucketName), cancellationToken); } } diff --git a/packages/Storage/Storage/VectorBucket.cs b/packages/Storage/Storage/VectorBucket.cs new file mode 100644 index 00000000..0da7b91e --- /dev/null +++ b/packages/Storage/Storage/VectorBucket.cs @@ -0,0 +1,26 @@ +using System.Threading; +using System.Threading.Tasks; +using Supabase.Storage.Common; +using Supabase.Storage.DeleteVectorIndex; +using Supabase.Storage.Interfaces; + +namespace Supabase.Storage; + +internal sealed class VectorBucket : IVectorBucket +{ + private readonly string vectorBucketName; + private readonly VectorService service; + + internal VectorBucket(string vectorBucketName, VectorService service) + { + this.vectorBucketName = vectorBucketName; + this.service = service; + } + + /// + public Task DeleteIndexAsync(string indexName, CancellationToken cancellationToken = default) + { + VectorService.RequireName(indexName, nameof(indexName)); + return this.service.Post("DeleteIndex", new DeleteVectorIndexBody(this.vectorBucketName, indexName), cancellationToken); + } +}