|
| 1 | +/** |
| 2 | + * [#5638] `DeleteDataResult` is a CLAIM about the server's delete body, and it |
| 3 | + * has to be the claim its own comment makes: `Spec: DeleteDataResponseSchema`. |
| 4 | + * |
| 5 | + * It declared `{ object, id, deleted: boolean }` while |
| 6 | + * `DeleteDataResponseSchema` declares `{ object, id, success: boolean }`. Both |
| 7 | + * `delete` surfaces (`client.data.delete` and the project-scoped |
| 8 | + * `client.project(id).data.delete`) are pure `unwrapResponse` / `_unwrap` |
| 9 | + * passthroughs — no runtime rewriting anywhere — so a consumer writing |
| 10 | + * `r.deleted` compiled fine and read `undefined` against every server path |
| 11 | + * that exists (#5581 / PR #5641 brought the ObjectQL fallback to `success` |
| 12 | + * too, so both producer paths now answer the same shape). |
| 13 | + * |
| 14 | + * WHICH LAYER ACTUALLY MOVES, and why that is not the usual one: the fix is a |
| 15 | + * type declaration, and types are erased before vitest ever runs. Reverting |
| 16 | + * `success` back to `deleted` therefore leaves every runtime assertion below |
| 17 | + * GREEN — they assert on a body the mock/server produced, which the client |
| 18 | + * never touched either way. The assertion that goes red on revert is the |
| 19 | + * type-level one (`SpecAndSdkAgree`), plus the `r.success` reads, and both are |
| 20 | + * enforced by `pnpm --filter @objectstack/client typecheck` → `check:test-typecheck` |
| 21 | + * (#5449/#5546), which compiles this file with the package's full strictness |
| 22 | + * and no debt-ledger entry. The runtime assertions below are shape pins: they |
| 23 | + * pin that the passthrough stays a passthrough, which is what makes the type |
| 24 | + * the only thing standing between a consumer and the wire. |
| 25 | + */ |
| 26 | +import { describe, it, expect, vi } from 'vitest'; |
| 27 | +import { DeleteDataResponseSchema, type DeleteDataResponse } from '@objectstack/spec/api'; |
| 28 | +import { ObjectStackClient, type DeleteDataResult } from './index'; |
| 29 | + |
| 30 | +/** Mutual assignability — `A extends B` alone would pass on a strict subset. */ |
| 31 | +type Exact<A, B> = [A] extends [B] ? ([B] extends [A] ? true : false) : false; |
| 32 | + |
| 33 | +/** |
| 34 | + * The pin. `DeleteDataResult` must be structurally the spec type, field for |
| 35 | + * field: rename either side, add a key to one, or make one optional, and this |
| 36 | + * stops being `true` — a type error at `check:test-typecheck`, not a runtime |
| 37 | + * failure. (This is deliberately an equality, not `extends`: the old `deleted` |
| 38 | + * declaration was neither a subset nor a superset of the schema, and a |
| 39 | + * one-directional check would still have caught it — but a future |
| 40 | + * `success: boolean; deleted?: boolean` "transition" shape would slip past |
| 41 | + * one, and that shape is exactly what #5638's triage ruled out.) |
| 42 | + */ |
| 43 | +const SpecAndSdkAgree: Exact<DeleteDataResult, DeleteDataResponse> = true; |
| 44 | + |
| 45 | +/** Helper: a client whose fetch answers `body` with `status`. */ |
| 46 | +function createMockClient(body: any, status = 200) { |
| 47 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 48 | + ok: status >= 200 && status < 300, |
| 49 | + status, |
| 50 | + statusText: status === 200 ? 'OK' : 'Error', |
| 51 | + json: async () => body, |
| 52 | + headers: new Headers(), |
| 53 | + }); |
| 54 | + const client = new ObjectStackClient({ baseUrl: 'http://localhost:3000', fetch: fetchMock }); |
| 55 | + return { client, fetchMock }; |
| 56 | +} |
| 57 | + |
| 58 | +/** The body every server path answers a successful delete with (#5581). */ |
| 59 | +const SPEC_BODY = { object: 'task', id: 't1', success: true }; |
| 60 | + |
| 61 | +describe('[#5638] DeleteDataResult === DeleteDataResponseSchema', () => { |
| 62 | + it('declares the schema\'s shape, field for field (type-level)', () => { |
| 63 | + // Reading the binding is what keeps `noUnusedLocals` satisfied; the |
| 64 | + // assertion that matters already happened in tsc, above. |
| 65 | + expect(SpecAndSdkAgree).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + it('the schema still declares `success` and has never declared `deleted`', () => { |
| 69 | + const parsed = DeleteDataResponseSchema.safeParse(SPEC_BODY); |
| 70 | + expect(parsed.success).toBe(true); |
| 71 | + expect(DeleteDataResponseSchema.safeParse({ object: 'task', id: 't1', deleted: true }).success) |
| 72 | + .toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + for (const [surface, del] of [ |
| 76 | + ['client.data.delete', (c: ObjectStackClient) => c.data.delete('task', 't1')], |
| 77 | + ['client.project(id).data.delete', (c: ObjectStackClient) => c.project('proj-xyz').data.delete('task', 't1')], |
| 78 | + ] as const) { |
| 79 | + describe(surface, () => { |
| 80 | + it('hands back the spec body, and `success` is reachable through the declared type', async () => { |
| 81 | + const { client } = createMockClient(SPEC_BODY); |
| 82 | + const r: DeleteDataResult = await del(client); |
| 83 | + |
| 84 | + // The typed read. Under the pre-#5638 declaration this line is |
| 85 | + // TS2339 (`success` does not exist) — and the line a consumer WOULD |
| 86 | + // have written, `r.deleted`, compiled and evaluated to `undefined`. |
| 87 | + expect(r.success).toBe(true); |
| 88 | + expect(r.object).toBe('task'); |
| 89 | + expect(r.id).toBe('t1'); |
| 90 | + |
| 91 | + // `z.object` strips unknown keys, so a passing `safeParse` cannot by |
| 92 | + // itself prove no stray `deleted` came along (the #5641 caveat). |
| 93 | + // Assert the key set literally as well. |
| 94 | + expect(DeleteDataResponseSchema.safeParse(r).success).toBe(true); |
| 95 | + expect(Object.keys(r).sort()).toEqual(['id', 'object', 'success']); |
| 96 | + expect('deleted' in (r as object)).toBe(false); |
| 97 | + }); |
| 98 | + |
| 99 | + it('does not manufacture `success` — an off-spec body passes through verbatim', async () => { |
| 100 | + // Contract-first: the fix belongs in the type (and, for the server, |
| 101 | + // in #5581), NOT in a tolerant consumer that reads |
| 102 | + // `body.success ?? body.deleted`. If someone ever adds that alias, |
| 103 | + // this test is where it shows up. |
| 104 | + const { client } = createMockClient({ object: 'task', id: 't1', deleted: true }); |
| 105 | + const r = await del(client) as unknown as Record<string, unknown>; |
| 106 | + |
| 107 | + expect(r.success).toBeUndefined(); |
| 108 | + expect(r.deleted).toBe(true); |
| 109 | + }); |
| 110 | + }); |
| 111 | + } |
| 112 | +}); |
0 commit comments