Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions server/resources/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
collectOwnedAttachmentObjectKeys,
countObjectKeyReferences,
effectiveOfficialStorageEnforcement,
reportedOfficialUsedBytes,
reservationCleanupKeys,
type UploadReservationManifestItem,
} from './service';
Expand Down Expand Up @@ -35,6 +36,19 @@ describe('resource cleanup helpers', () => {
expect(effectiveOfficialStorageEnforcement('enforce', 'complete')).toBe('enforce');
expect(effectiveOfficialStorageEnforcement('observe', 'complete')).toBe('observe');
});
it('does not report a placeholder zero before the user baseline is authoritative', () => {
expect(reportedOfficialUsedBytes(null, 'running')).toBeNull();
expect(
reportedOfficialUsedBytes({ usedBytes: 123n, reconciledAt: null }, 'running')
).toBeNull();
expect(
reportedOfficialUsedBytes(
{ usedBytes: 123n, reconciledAt: new Date('2026-08-14T00:00:00.000Z') },
'running'
)
).toBe('123');
expect(reportedOfficialUsedBytes(null, 'complete')).toBe('0');
});
it('collects staging keys and optionally final keys without duplicates', () => {
expect(reservationCleanupKeys(manifest, false)).toEqual([
'users/user/staging/session/original.jpg',
Expand Down
18 changes: 16 additions & 2 deletions server/resources/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ export function effectiveOfficialStorageEnforcement(
return requested === 'enforce' && reconciliationStatus === 'complete' ? 'enforce' : 'observe';
}

export function reportedOfficialUsedBytes(
account: Pick<typeof resourceStorageAccounts.$inferSelect, 'usedBytes' | 'reconciledAt'> | null,
reconciliationStatus: string | null | undefined
): string | null {
if (account?.reconciledAt || reconciliationStatus === 'complete') {
return (account?.usedBytes ?? BigInt(0)).toString();
}
return null;
}

function grantIsUsable(grant: typeof billingGrants.$inferSelect | null, now: Date): boolean {
return Boolean(
grant &&
Expand Down Expand Up @@ -207,13 +217,17 @@ async function resolveStateWithExecutor(
requestedOfficialStorageEnforcement(),
managementState?.reconciliationStatus
);
const reportedUsed = reportedOfficialUsedBytes(
account ?? null,
managementState?.reconciliationStatus
);
if (isRoleExempt(user.role)) {
return {
management: 'official',
source: 'role_exempt',
storage: {
enforcement,
usedBytes: (account?.usedBytes ?? BigInt(0)).toString(),
usedBytes: reportedUsed,
reservedBytes: (account?.reservedBytes ?? BigInt(0)).toString(),
limitBytes: null,
overLimit: false,
Expand All @@ -232,7 +246,7 @@ async function resolveStateWithExecutor(
source: pro ? 'official_pro' : 'official_free',
storage: {
enforcement,
usedBytes: used.toString(),
usedBytes: reportedUsed,
reservedBytes: reserved.toString(),
limitBytes: limit.toString(),
overLimit,
Expand Down
30 changes: 29 additions & 1 deletion server/resources/worker.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, expect, it } from 'bun:test';

process.env.POSTGRESQL_URL ||= 'postgres://test:test@localhost:5432/rote_test';
const { attachmentObjects, isolateReconciliationFailure } = await import('./worker');
const { attachmentObjects, inspectReconciledObjects, isolateReconciliationFailure } =
await import('./worker');

describe('resource maintenance isolation', () => {
it('counts a Live Photo original reused as its browser cover only once', () => {
Expand Down Expand Up @@ -36,4 +37,31 @@ describe('resource maintenance isolation', () => {
).resolves.toBeUndefined();
expect(observed).toBeInstanceOf(Error);
});

it('counts existing physical objects and skips stale missing legacy references', async () => {
const objects = [
{ key: 'present', role: 'original' as const, billable: true, references: 1 },
{ key: 'missing', role: 'compressed' as const, billable: false, references: 1 },
];
const result = await inspectReconciledObjects(
objects,
async (key) =>
key === 'present' ? { contentLength: 123, contentType: 'image/jpeg', etag: 'etag' } : null,
2
);

expect(result).toEqual({
inspected: [{ ...objects[0], actualBytes: 123n }],
missingCount: 1,
});
});

it('keeps unknown object sizes fail-closed', async () => {
await expect(
inspectReconciledObjects(
[{ key: 'unknown', role: 'original', billable: true, references: 1 }],
async () => ({ contentLength: null, contentType: null, etag: null })
)
).rejects.toThrow('storage_object_size_unknown');
});
});
Loading
Loading